2 * "$Id: util.c 7850 2008-08-20 00:07:25Z mike $"
4 * Printing utilities for CUPS.
6 * Copyright 2007-2012 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 * cupsCancelJob() - Cancel a print job on the default server.
20 * cupsCancelJob2() - Cancel or purge a print job.
21 * cupsCreateJob() - Create an empty job for streaming.
22 * cupsFinishDocument() - Finish sending a document.
23 * cupsFreeJobs() - Free memory used by job data.
24 * cupsGetClasses() - Get a list of printer classes from the default
26 * cupsGetDefault() - Get the default printer or class for the default
28 * cupsGetDefault2() - Get the default printer or class for the specified
30 * cupsGetJobs() - Get the jobs from the default server.
31 * cupsGetJobs2() - Get the jobs from the specified server.
32 * cupsGetPPD() - Get the PPD file for a printer on the default
34 * cupsGetPPD2() - Get the PPD file for a printer from the specified
36 * cupsGetPPD3() - Get the PPD file for a printer on the specified
37 * server if it has changed.
38 * cupsGetPrinters() - Get a list of printers from the default server.
39 * cupsGetServerPPD() - Get an available PPD file from the server.
40 * cupsPrintFile() - Print a file to a printer or class on the default
42 * cupsPrintFile2() - Print a file to a printer or class on the
44 * cupsPrintFiles() - Print one or more files to a printer or class on
46 * cupsPrintFiles2() - Print one or more files to a printer or class on
47 * the specified server.
48 * cupsStartDocument() - Add a document to a job created with
50 * cups_get_printer_uri() - Get the printer-uri-supported attribute for the
51 * first printer in a class.
55 * Include necessary headers...
58 #include "cups-private.h"
61 #if defined(WIN32) || defined(__EMX__)
65 #endif /* WIN32 || __EMX__ */
72 static int cups_get_printer_uri(http_t
*http
, const char *name
,
73 char *host
, int hostsize
, int *port
,
74 char *resource
, int resourcesize
,
79 * 'cupsCancelJob()' - Cancel a print job on the default server.
81 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
82 * to cancel the current job on the named destination.
84 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
85 * the cause of any failure.
88 int /* O - 1 on success, 0 on failure */
89 cupsCancelJob(const char *name
, /* I - Name of printer or class */
90 int job_id
) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
92 return (cupsCancelJob2(CUPS_HTTP_DEFAULT
, name
, job_id
, 0)
93 < IPP_REDIRECTION_OTHER_SITE
);
98 * 'cupsCancelJob2()' - Cancel or purge a print job.
100 * Canceled jobs remain in the job history while purged jobs are removed
101 * from the job history.
103 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
104 * to cancel the current job on the named destination.
106 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
107 * the cause of any failure.
109 * @since CUPS 1.4/OS X 10.6@
112 ipp_status_t
/* O - IPP status */
113 cupsCancelJob2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
114 const char *name
, /* I - Name of printer or class */
115 int job_id
, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
116 int purge
) /* I - 1 to purge, 0 to cancel */
118 char uri
[HTTP_MAX_URI
]; /* Job/printer URI */
119 ipp_t
*request
; /* IPP request */
120 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread global data */
124 * Range check input...
127 if (job_id
< -1 || (!name
&& job_id
== 0))
129 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
134 * Connect to the default server as needed...
138 if ((http
= _cupsConnect()) == NULL
)
139 return (IPP_SERVICE_UNAVAILABLE
);
142 * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following
146 * attributes-natural-language
147 * job-uri or printer-uri + job-id
148 * requesting-user-name
149 * [purge-job] or [purge-jobs]
152 request
= ippNewRequest(job_id
< 0 ? IPP_PURGE_JOBS
: IPP_CANCEL_JOB
);
154 ippSetVersion(request
, cg
->server_version
/ 10, cg
->server_version
% 10);
158 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
159 "localhost", ippPort(), "/printers/%s", name
);
161 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
163 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
168 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
170 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
173 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
176 if (purge
&& job_id
>= 0)
177 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-job", 1);
178 else if (!purge
&& job_id
< 0)
179 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", 0);
185 ippDelete(cupsDoRequest(http
, request
, "/jobs/"));
187 return (cupsLastError());
192 * 'cupsCreateJob()' - Create an empty job for streaming.
194 * Use this function when you want to stream print data using the
195 * @link cupsStartDocument@, @link cupsWriteRequestData@, and
196 * @link cupsFinishDocument@ functions. If you have one or more files to
197 * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function
200 * @since CUPS 1.4/OS X 10.6@
203 int /* O - Job ID or 0 on error */
205 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
206 const char *name
, /* I - Destination name */
207 const char *title
, /* I - Title of job */
208 int num_options
, /* I - Number of options */
209 cups_option_t
*options
) /* I - Options */
211 char printer_uri
[1024], /* Printer URI */
212 resource
[1024]; /* Printer resource */
213 ipp_t
*request
, /* Create-Job request */
214 *response
; /* Create-Job response */
215 ipp_attribute_t
*attr
; /* job-id attribute */
216 int job_id
= 0; /* job-id value */
217 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread global data */
220 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", "
221 "num_options=%d, options=%p)",
222 http
, name
, title
, num_options
, options
));
225 * Range check input...
230 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
235 * Build a Create-Job request...
238 if ((request
= ippNewRequest(IPP_CREATE_JOB
)) == NULL
)
240 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(ENOMEM
), 0);
244 ippSetVersion(request
, cg
->server_version
/ 10, cg
->server_version
% 10);
246 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
), "ipp",
247 NULL
, "localhost", ippPort(), "/printers/%s", name
);
248 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
250 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
252 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
255 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
257 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_OPERATION
);
258 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_JOB
);
259 cupsEncodeOptions2(request
, num_options
, options
, IPP_TAG_SUBSCRIPTION
);
262 * Send the request and get the job-id...
265 response
= cupsDoRequest(http
, request
, resource
);
267 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) != NULL
)
268 job_id
= attr
->values
[0].integer
;
281 * 'cupsFinishDocument()' - Finish sending a document.
283 * The document must have been started using @link cupsStartDocument@.
285 * @since CUPS 1.4/OS X 10.6@
288 ipp_status_t
/* O - Status of document submission */
289 cupsFinishDocument(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
290 const char *name
) /* I - Destination name */
292 char resource
[1024]; /* Printer resource */
295 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
297 ippDelete(cupsGetResponse(http
, resource
));
299 return (cupsLastError());
304 * 'cupsFreeJobs()' - Free memory used by job data.
308 cupsFreeJobs(int num_jobs
, /* I - Number of jobs */
309 cups_job_t
*jobs
) /* I - Jobs */
311 int i
; /* Looping var */
312 cups_job_t
*job
; /* Current job */
315 if (num_jobs
<= 0 || !jobs
)
318 for (i
= num_jobs
, job
= jobs
; i
> 0; i
--, job
++)
320 _cupsStrFree(job
->dest
);
321 _cupsStrFree(job
->user
);
322 _cupsStrFree(job
->format
);
323 _cupsStrFree(job
->title
);
331 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
333 * This function is deprecated - use @link cupsGetDests@ instead.
338 int /* O - Number of classes */
339 cupsGetClasses(char ***classes
) /* O - Classes */
341 int n
; /* Number of classes */
342 ipp_t
*request
, /* IPP Request */
343 *response
; /* IPP Response */
344 ipp_attribute_t
*attr
; /* Current attribute */
345 char **temp
; /* Temporary pointer */
346 http_t
*http
; /* Connection to server */
351 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
358 if ((http
= _cupsConnect()) == NULL
)
362 * Build a CUPS_GET_CLASSES request, which requires the following
366 * attributes-natural-language
367 * requested-attributes
370 request
= ippNewRequest(CUPS_GET_CLASSES
);
372 ippSetVersion(request
, 1, 1);
374 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
375 "requested-attributes", NULL
, "printer-name");
378 * Do the request and get back a response...
383 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
385 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
386 if (attr
->name
!= NULL
&&
387 _cups_strcasecmp(attr
->name
, "printer-name") == 0 &&
388 attr
->value_tag
== IPP_TAG_NAME
)
391 temp
= malloc(sizeof(char *));
393 temp
= realloc(*classes
, sizeof(char *) * (n
+ 1));
413 temp
[n
] = strdup(attr
->values
[0].string
.text
);
425 * 'cupsGetDefault()' - Get the default printer or class for the default server.
427 * This function returns the default printer or class as defined by
428 * the LPDEST or PRINTER environment variables. If these environment
429 * variables are not set, the server default destination is returned.
430 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
431 * functions to get the user-defined default printer, as this function does
432 * not support the lpoptions-defined default printer.
435 const char * /* O - Default printer or @code NULL@ */
439 * Return the default printer...
442 return (cupsGetDefault2(CUPS_HTTP_DEFAULT
));
447 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
449 * This function returns the default printer or class as defined by
450 * the LPDEST or PRINTER environment variables. If these environment
451 * variables are not set, the server default destination is returned.
452 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
453 * functions to get the user-defined default printer, as this function does
454 * not support the lpoptions-defined default printer.
456 * @since CUPS 1.1.21/OS X 10.4@
459 const char * /* O - Default printer or @code NULL@ */
460 cupsGetDefault2(http_t
*http
) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
462 ipp_t
*request
, /* IPP Request */
463 *response
; /* IPP Response */
464 ipp_attribute_t
*attr
; /* Current attribute */
465 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
469 * See if we have a user default printer set...
472 if (_cupsUserDefault(cg
->def_printer
, sizeof(cg
->def_printer
)))
473 return (cg
->def_printer
);
476 * Connect to the server as needed...
480 if ((http
= _cupsConnect()) == NULL
)
484 * Build a CUPS_GET_DEFAULT request, which requires the following
488 * attributes-natural-language
491 request
= ippNewRequest(CUPS_GET_DEFAULT
);
493 ippSetVersion(request
, 1, 1);
496 * Do the request and get back a response...
499 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
501 if ((attr
= ippFindAttribute(response
, "printer-name",
502 IPP_TAG_NAME
)) != NULL
)
504 strlcpy(cg
->def_printer
, attr
->values
[0].string
.text
,
505 sizeof(cg
->def_printer
));
507 return (cg
->def_printer
);
518 * 'cupsGetJobs()' - Get the jobs from the default server.
520 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
521 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
522 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
523 * jobs that are stopped, canceled, aborted, or completed.
526 int /* O - Number of jobs */
527 cupsGetJobs(cups_job_t
**jobs
, /* O - Job data */
528 const char *name
, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
529 int myjobs
, /* I - 0 = all users, 1 = mine */
530 int whichjobs
) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
536 return (cupsGetJobs2(CUPS_HTTP_DEFAULT
, jobs
, name
, myjobs
, whichjobs
));
542 * 'cupsGetJobs2()' - Get the jobs from the specified server.
544 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
545 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
546 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
547 * jobs that are stopped, canceled, aborted, or completed.
549 * @since CUPS 1.1.21/OS X 10.4@
552 int /* O - Number of jobs */
553 cupsGetJobs2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
554 cups_job_t
**jobs
, /* O - Job data */
555 const char *name
, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
556 int myjobs
, /* I - 0 = all users, 1 = mine */
557 int whichjobs
) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
559 int n
; /* Number of jobs */
560 ipp_t
*request
, /* IPP Request */
561 *response
; /* IPP Response */
562 ipp_attribute_t
*attr
; /* Current attribute */
563 cups_job_t
*temp
; /* Temporary pointer */
565 priority
, /* job-priority */
566 size
; /* job-k-octets */
567 ipp_jstate_t state
; /* job-state */
568 time_t completed_time
, /* time-at-completed */
569 creation_time
, /* time-at-creation */
570 processing_time
; /* time-at-processing */
571 const char *dest
, /* job-printer-uri */
572 *format
, /* document-format */
573 *title
, /* job-name */
574 *user
; /* job-originating-user-name */
575 char uri
[HTTP_MAX_URI
]; /* URI for jobs */
576 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
577 static const char * const attrs
[] = /* Requested attributes */
583 "job-originating-user-name",
594 * Range check input...
599 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
605 * Get the right URI...
610 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
611 "localhost", 0, "/printers/%s", name
) != HTTP_URI_OK
)
613 _cupsSetError(IPP_INTERNAL_ERROR
, _("Unable to create printer-uri"), 1);
619 strlcpy(uri
, "ipp://localhost/", sizeof(uri
));
622 if ((http
= _cupsConnect()) == NULL
)
626 * Build an IPP_GET_JOBS request, which requires the following
630 * attributes-natural-language
632 * requesting-user-name
635 * requested-attributes
638 request
= ippNewRequest(IPP_GET_JOBS
);
640 ippSetVersion(request
, cg
->server_version
/ 10, cg
->server_version
% 10);
642 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
643 "printer-uri", NULL
, uri
);
645 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
646 "requesting-user-name", NULL
, cupsUser());
649 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
651 if (whichjobs
== CUPS_WHICHJOBS_COMPLETED
)
652 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
653 "which-jobs", NULL
, "completed");
654 else if (whichjobs
== CUPS_WHICHJOBS_ALL
)
655 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
656 "which-jobs", NULL
, "all");
658 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
659 "requested-attributes", sizeof(attrs
) / sizeof(attrs
[0]),
663 * Do the request and get back a response...
669 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
671 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
674 * Skip leading attributes until we hit a job...
677 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
684 * Pull the needed attributes from this job...
690 state
= IPP_JOB_PENDING
;
693 format
= "application/octet-stream";
699 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
701 if (!strcmp(attr
->name
, "job-id") &&
702 attr
->value_tag
== IPP_TAG_INTEGER
)
703 id
= attr
->values
[0].integer
;
704 else if (!strcmp(attr
->name
, "job-state") &&
705 attr
->value_tag
== IPP_TAG_ENUM
)
706 state
= (ipp_jstate_t
)attr
->values
[0].integer
;
707 else if (!strcmp(attr
->name
, "job-priority") &&
708 attr
->value_tag
== IPP_TAG_INTEGER
)
709 priority
= attr
->values
[0].integer
;
710 else if (!strcmp(attr
->name
, "job-k-octets") &&
711 attr
->value_tag
== IPP_TAG_INTEGER
)
712 size
= attr
->values
[0].integer
;
713 else if (!strcmp(attr
->name
, "time-at-completed") &&
714 attr
->value_tag
== IPP_TAG_INTEGER
)
715 completed_time
= attr
->values
[0].integer
;
716 else if (!strcmp(attr
->name
, "time-at-creation") &&
717 attr
->value_tag
== IPP_TAG_INTEGER
)
718 creation_time
= attr
->values
[0].integer
;
719 else if (!strcmp(attr
->name
, "time-at-processing") &&
720 attr
->value_tag
== IPP_TAG_INTEGER
)
721 processing_time
= attr
->values
[0].integer
;
722 else if (!strcmp(attr
->name
, "job-printer-uri") &&
723 attr
->value_tag
== IPP_TAG_URI
)
725 if ((dest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
728 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
729 attr
->value_tag
== IPP_TAG_NAME
)
730 user
= attr
->values
[0].string
.text
;
731 else if (!strcmp(attr
->name
, "document-format") &&
732 attr
->value_tag
== IPP_TAG_MIMETYPE
)
733 format
= attr
->values
[0].string
.text
;
734 else if (!strcmp(attr
->name
, "job-name") &&
735 (attr
->value_tag
== IPP_TAG_TEXT
||
736 attr
->value_tag
== IPP_TAG_NAME
))
737 title
= attr
->values
[0].string
.text
;
743 * See if we have everything needed...
755 * Allocate memory for the job...
759 temp
= malloc(sizeof(cups_job_t
));
761 temp
= realloc(*jobs
, sizeof(cups_job_t
) * (n
+ 1));
769 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
771 cupsFreeJobs(n
, *jobs
);
784 * Copy the data over...
787 temp
->dest
= _cupsStrAlloc(dest
);
788 temp
->user
= _cupsStrAlloc(user
);
789 temp
->format
= _cupsStrAlloc(format
);
790 temp
->title
= _cupsStrAlloc(title
);
792 temp
->priority
= priority
;
795 temp
->completed_time
= completed_time
;
796 temp
->creation_time
= creation_time
;
797 temp
->processing_time
= processing_time
;
806 if (n
== 0 && cg
->last_error
>= IPP_BAD_REQUEST
)
814 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
816 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
819 * The returned filename is stored in a static buffer and is overwritten with
820 * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. The caller "owns" the
821 * file that is created and must @code unlink@ the returned filename.
824 const char * /* O - Filename for PPD file */
825 cupsGetPPD(const char *name
) /* I - Destination name */
827 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
828 time_t modtime
= 0; /* Modification time */
832 * Return the PPD file...
835 cg
->ppd_filename
[0] = '\0';
837 if (cupsGetPPD3(CUPS_HTTP_DEFAULT
, name
, &modtime
, cg
->ppd_filename
,
838 sizeof(cg
->ppd_filename
)) == HTTP_OK
)
839 return (cg
->ppd_filename
);
846 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
848 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
851 * The returned filename is stored in a static buffer and is overwritten with
852 * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. The caller "owns" the
853 * file that is created and must @code unlink@ the returned filename.
855 * @since CUPS 1.1.21/OS X 10.4@
858 const char * /* O - Filename for PPD file */
859 cupsGetPPD2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
860 const char *name
) /* I - Destination name */
862 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
863 time_t modtime
= 0; /* Modification time */
866 cg
->ppd_filename
[0] = '\0';
868 if (cupsGetPPD3(http
, name
, &modtime
, cg
->ppd_filename
,
869 sizeof(cg
->ppd_filename
)) == HTTP_OK
)
870 return (cg
->ppd_filename
);
877 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
878 * server if it has changed.
880 * The "modtime" parameter contains the modification time of any
881 * locally-cached content and is updated with the time from the PPD file on
884 * The "buffer" parameter contains the local PPD filename. If it contains
885 * the empty string, a new temporary file is created, otherwise the existing
886 * file will be overwritten as needed. The caller "owns" the file that is
887 * created and must @code unlink@ the returned filename.
889 * On success, @code HTTP_OK@ is returned for a new PPD file and
890 * @code HTTP_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other
891 * status is an error.
893 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
896 * @since CUPS 1.4/OS X 10.6@
899 http_status_t
/* O - HTTP status */
900 cupsGetPPD3(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
901 const char *name
, /* I - Destination name */
902 time_t *modtime
, /* IO - Modification time */
903 char *buffer
, /* I - Filename buffer */
904 size_t bufsize
) /* I - Size of filename buffer */
906 int http_port
; /* Port number */
907 char http_hostname
[HTTP_MAX_HOST
];
908 /* Hostname associated with connection */
909 http_t
*http2
; /* Alternate HTTP connection */
910 int fd
; /* PPD file */
911 char localhost
[HTTP_MAX_URI
],/* Local hostname */
912 hostname
[HTTP_MAX_URI
], /* Hostname */
913 resource
[HTTP_MAX_URI
]; /* Resource name */
914 int port
; /* Port number */
915 http_status_t status
; /* HTTP status from server */
916 char tempfile
[1024] = ""; /* Temporary filename */
917 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
921 * Range check input...
924 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
925 "bufsize=%d)", http
, name
, modtime
,
926 modtime
? (int)*modtime
: 0, buffer
, (int)bufsize
));
930 _cupsSetError(IPP_INTERNAL_ERROR
, _("No printer name"), 1);
931 return (HTTP_NOT_ACCEPTABLE
);
936 _cupsSetError(IPP_INTERNAL_ERROR
, _("No modification time"), 1);
937 return (HTTP_NOT_ACCEPTABLE
);
940 if (!buffer
|| bufsize
<= 1)
942 _cupsSetError(IPP_INTERNAL_ERROR
, _("Bad filename buffer"), 1);
943 return (HTTP_NOT_ACCEPTABLE
);
948 * See if the PPD file is available locally...
952 httpGetHostname(http
, hostname
, sizeof(hostname
));
955 strlcpy(hostname
, cupsServer(), sizeof(hostname
));
956 if (hostname
[0] == '/')
957 strlcpy(hostname
, "localhost", sizeof(hostname
));
960 if (!_cups_strcasecmp(hostname
, "localhost"))
962 char ppdname
[1024]; /* PPD filename */
963 struct stat ppdinfo
; /* PPD file information */
966 snprintf(ppdname
, sizeof(ppdname
), "%s/ppd/%s.ppd", cg
->cups_serverroot
,
968 if (!stat(ppdname
, &ppdinfo
))
971 * OK, the file exists, use it!
978 if (symlink(ppdname
, buffer
) && errno
!= EEXIST
)
980 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
982 return (HTTP_SERVER_ERROR
);
987 int tries
; /* Number of tries */
988 const char *tmpdir
; /* TMPDIR environment variable */
989 struct timeval curtime
; /* Current time */
992 * Previously we put root temporary files in the default CUPS temporary
993 * directory under /var/spool/cups. However, since the scheduler cleans
994 * out temporary files there and runs independently of the user apps, we
995 * don't want to use it unless specifically told to by cupsd.
998 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
1000 tmpdir
= "/private/tmp"; /* /tmp is a symlink to /private/tmp */
1003 # endif /* __APPLE__ */
1006 * Make the temporary name using the specified directory...
1014 * Get the current time of day...
1017 gettimeofday(&curtime
, NULL
);
1020 * Format a string using the hex time values...
1023 snprintf(buffer
, bufsize
, "%s/%08lx%05lx", tmpdir
,
1024 (unsigned long)curtime
.tv_sec
,
1025 (unsigned long)curtime
.tv_usec
);
1028 * Try to make a symlink...
1031 if (!symlink(ppdname
, buffer
))
1036 while (tries
< 1000);
1040 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1042 return (HTTP_SERVER_ERROR
);
1046 if (*modtime
>= ppdinfo
.st_mtime
)
1047 return (HTTP_NOT_MODIFIED
);
1050 *modtime
= ppdinfo
.st_mtime
;
1058 * Try finding a printer URI for this printer...
1062 if ((http
= _cupsConnect()) == NULL
)
1063 return (HTTP_SERVICE_UNAVAILABLE
);
1065 if (!cups_get_printer_uri(http
, name
, hostname
, sizeof(hostname
), &port
,
1066 resource
, sizeof(resource
), 0))
1067 return (HTTP_NOT_FOUND
);
1069 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname
,
1073 * Remap local hostname to localhost...
1076 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
1078 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost
));
1080 if (!_cups_strcasecmp(localhost
, hostname
))
1081 strlcpy(hostname
, "localhost", sizeof(hostname
));
1084 * Get the hostname and port number we are connected to...
1087 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
1088 http_port
= httpAddrPort(http
->hostaddr
);
1090 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
1091 http_hostname
, http_port
));
1094 * Reconnect to the correct server as needed...
1097 if (!_cups_strcasecmp(http_hostname
, hostname
) && port
== http_port
)
1099 else if ((http2
= httpConnectEncrypt(hostname
, port
,
1100 cupsEncryption())) == NULL
)
1102 DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
1104 return (HTTP_SERVICE_UNAVAILABLE
);
1108 * Get a temp file...
1112 fd
= open(buffer
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
1114 fd
= cupsTempFd(tempfile
, sizeof(tempfile
));
1119 * Can't open file; close the server connection and return NULL...
1122 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1127 return (HTTP_SERVER_ERROR
);
1131 * And send a request to the HTTP server...
1134 strlcat(resource
, ".ppd", sizeof(resource
));
1137 httpSetField(http2
, HTTP_FIELD_IF_MODIFIED_SINCE
,
1138 httpGetDateString(*modtime
));
1140 status
= cupsGetFd(http2
, resource
, fd
);
1145 * See if we actually got the file or an error...
1148 if (status
== HTTP_OK
)
1150 *modtime
= httpGetDateTime(httpGetField(http2
, HTTP_FIELD_DATE
));
1153 strlcpy(buffer
, tempfile
, bufsize
);
1155 else if (status
!= HTTP_NOT_MODIFIED
)
1157 _cupsSetHTTPError(status
);
1161 else if (tempfile
[0])
1164 else if (tempfile
[0])
1171 * Return the PPD file...
1174 DEBUG_printf(("1cupsGetPPD3: Returning status %d", status
));
1181 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1183 * This function is deprecated - use @link cupsGetDests@ instead.
1188 int /* O - Number of printers */
1189 cupsGetPrinters(char ***printers
) /* O - Printers */
1191 int n
; /* Number of printers */
1192 ipp_t
*request
, /* IPP Request */
1193 *response
; /* IPP Response */
1194 ipp_attribute_t
*attr
; /* Current attribute */
1195 char **temp
; /* Temporary pointer */
1196 http_t
*http
; /* Connection to server */
1200 * Range check input...
1205 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
1213 * Try to connect to the server...
1216 if ((http
= _cupsConnect()) == NULL
)
1220 * Build a CUPS_GET_PRINTERS request, which requires the following
1223 * attributes-charset
1224 * attributes-natural-language
1225 * requested-attributes
1228 request
= ippNewRequest(CUPS_GET_PRINTERS
);
1230 ippSetVersion(request
, 1, 1);
1232 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1233 "requested-attributes", NULL
, "printer-name");
1235 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1238 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1239 "printer-type-mask", CUPS_PRINTER_CLASS
);
1242 * Do the request and get back a response...
1247 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1249 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1250 if (attr
->name
!= NULL
&&
1251 _cups_strcasecmp(attr
->name
, "printer-name") == 0 &&
1252 attr
->value_tag
== IPP_TAG_NAME
)
1255 temp
= malloc(sizeof(char *));
1257 temp
= realloc(*printers
, sizeof(char *) * (n
+ 1));
1262 * Ran out of memory!
1268 free((*printers
)[n
]);
1272 ippDelete(response
);
1277 temp
[n
] = strdup(attr
->values
[0].string
.text
);
1281 ippDelete(response
);
1289 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1291 * This function returns the named PPD file from the server. The
1292 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
1295 * You must remove (unlink) the PPD file when you are finished with
1296 * it. The PPD filename is stored in a static location that will be
1297 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1298 * or @link cupsGetServerPPD@.
1300 * @since CUPS 1.3/OS X 10.5@
1303 char * /* O - Name of PPD file or @code NULL@ on error */
1304 cupsGetServerPPD(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1305 const char *name
) /* I - Name of PPD file ("ppd-name") */
1307 int fd
; /* PPD file descriptor */
1308 ipp_t
*request
; /* IPP request */
1309 _cups_globals_t
*cg
= _cupsGlobals();
1310 /* Pointer to library globals */
1314 * Range check input...
1319 _cupsSetError(IPP_INTERNAL_ERROR
, _("No PPD name"), 1);
1325 if ((http
= _cupsConnect()) == NULL
)
1329 * Get a temp file...
1332 if ((fd
= cupsTempFd(cg
->ppd_filename
, sizeof(cg
->ppd_filename
))) < 0)
1335 * Can't open file; close the server connection and return NULL...
1338 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
, 0);
1344 * Get the PPD file...
1347 request
= ippNewRequest(CUPS_GET_PPD
);
1348 ippSetVersion(request
, cg
->server_version
/ 10, cg
->server_version
% 10);
1350 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "ppd-name", NULL
,
1353 ippDelete(cupsDoIORequest(http
, request
, "/", -1, fd
));
1357 if (cupsLastError() != IPP_OK
)
1359 unlink(cg
->ppd_filename
);
1363 return (cg
->ppd_filename
);
1368 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1371 int /* O - Job ID or 0 on error */
1372 cupsPrintFile(const char *name
, /* I - Destination name */
1373 const char *filename
, /* I - File to print */
1374 const char *title
, /* I - Title of job */
1375 int num_options
,/* I - Number of options */
1376 cups_option_t
*options
) /* I - Options */
1378 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
1379 "title=\"%s\", num_options=%d, options=%p)",
1380 name
, filename
, title
, num_options
, options
));
1382 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, 1, &filename
, title
,
1383 num_options
, options
));
1388 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1391 * @since CUPS 1.1.21/OS X 10.4@
1394 int /* O - Job ID or 0 on error */
1396 http_t
*http
, /* I - Connection to server */
1397 const char *name
, /* I - Destination name */
1398 const char *filename
, /* I - File to print */
1399 const char *title
, /* I - Title of job */
1400 int num_options
, /* I - Number of options */
1401 cups_option_t
*options
) /* I - Options */
1403 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
1404 "title=\"%s\", num_options=%d, options=%p)",
1405 http
, name
, filename
, title
, num_options
, options
));
1407 return (cupsPrintFiles2(http
, name
, 1, &filename
, title
, num_options
,
1413 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1417 int /* O - Job ID or 0 on error */
1419 const char *name
, /* I - Destination name */
1420 int num_files
, /* I - Number of files */
1421 const char **files
, /* I - File(s) to print */
1422 const char *title
, /* I - Title of job */
1423 int num_options
, /* I - Number of options */
1424 cups_option_t
*options
) /* I - Options */
1426 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
1427 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1428 name
, num_files
, files
, title
, num_options
, options
));
1431 * Print the file(s)...
1434 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, num_files
, files
, title
,
1435 num_options
, options
));
1440 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1443 * @since CUPS 1.1.21/OS X 10.4@
1446 int /* O - Job ID or 0 on error */
1448 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1449 const char *name
, /* I - Destination name */
1450 int num_files
, /* I - Number of files */
1451 const char **files
, /* I - File(s) to print */
1452 const char *title
, /* I - Title of job */
1453 int num_options
, /* I - Number of options */
1454 cups_option_t
*options
) /* I - Options */
1456 int i
; /* Looping var */
1457 int job_id
; /* New job ID */
1458 const char *docname
; /* Basename of current filename */
1459 const char *format
; /* Document format */
1460 cups_file_t
*fp
; /* Current file */
1461 char buffer
[8192]; /* Copy buffer */
1462 ssize_t bytes
; /* Bytes in buffer */
1463 http_status_t status
; /* Status of write */
1464 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1465 ipp_status_t cancel_status
; /* Status code to preserve */
1466 char *cancel_message
; /* Error message to preserve */
1469 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
1470 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1471 http
, name
, num_files
, files
, title
, num_options
, options
));
1474 * Range check input...
1477 if (!name
|| num_files
< 1 || !files
)
1479 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
1485 * Create the print job...
1488 if ((job_id
= cupsCreateJob(http
, name
, title
, num_options
, options
)) == 0)
1492 * Send each of the files...
1495 if (cupsGetOption("raw", num_options
, options
))
1496 format
= CUPS_FORMAT_RAW
;
1497 else if ((format
= cupsGetOption("document-format", num_options
,
1499 format
= CUPS_FORMAT_AUTO
;
1501 for (i
= 0; i
< num_files
; i
++)
1504 * Start the next file...
1507 if ((docname
= strrchr(files
[i
], '/')) != NULL
)
1512 if ((fp
= cupsFileOpen(files
[i
], "rb")) == NULL
)
1515 * Unable to open print file, cancel the job and return...
1518 _cupsSetError(IPP_DOCUMENT_ACCESS_ERROR
, NULL
, 0);
1522 status
= cupsStartDocument(http
, name
, job_id
, docname
, format
,
1523 i
== (num_files
- 1));
1525 while (status
== HTTP_CONTINUE
&&
1526 (bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1527 status
= cupsWriteRequestData(http
, buffer
, bytes
);
1531 if (status
!= HTTP_CONTINUE
|| cupsFinishDocument(http
, name
) != IPP_OK
)
1534 * Unable to queue, cancel the job and return...
1544 * If we get here, something happened while sending the print job so we need
1545 * to cancel the job without setting the last error (since we need to preserve
1546 * the current error...
1551 cancel_status
= cg
->last_error
;
1552 cancel_message
= cg
->last_status_message
?
1553 _cupsStrRetain(cg
->last_status_message
) : NULL
;
1555 cupsCancelJob2(http
, name
, job_id
, 0);
1557 cg
->last_error
= cancel_status
;
1558 cg
->last_status_message
= cancel_message
;
1565 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1567 * Use @link cupsWriteRequestData@ to write data for the document and
1568 * @link cupsFinishDocument@ to finish the document and get the submission status.
1570 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1571 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1572 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1573 * any supported MIME type string can be supplied.
1575 * @since CUPS 1.4/OS X 10.6@
1578 http_status_t
/* O - HTTP status of request */
1580 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1581 const char *name
, /* I - Destination name */
1582 int job_id
, /* I - Job ID from @link cupsCreateJob@ */
1583 const char *docname
, /* I - Name of document */
1584 const char *format
, /* I - MIME type or @code CUPS_FORMAT_foo@ */
1585 int last_document
) /* I - 1 for last document in job, 0 otherwise */
1587 char resource
[1024], /* Resource for destinatio */
1588 printer_uri
[1024]; /* Printer URI */
1589 ipp_t
*request
; /* Send-Document request */
1590 http_status_t status
; /* HTTP status */
1591 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread global data */
1595 * Create a Send-Document request...
1598 if ((request
= ippNewRequest(IPP_SEND_DOCUMENT
)) == NULL
)
1600 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(ENOMEM
), 0);
1601 return (HTTP_ERROR
);
1604 ippSetVersion(request
, cg
->server_version
/ 10, cg
->server_version
% 10);
1606 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
), "ipp",
1607 NULL
, "localhost", ippPort(), "/printers/%s", name
);
1608 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
1610 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1612 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", job_id
);
1613 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1616 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
1619 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1620 "document-format", NULL
, format
);
1621 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", last_document
);
1624 * Send and delete the request, then return the status...
1627 status
= cupsSendRequest(http
, request
, resource
, CUPS_LENGTH_VARIABLE
);
1636 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
1637 * first printer in a class.
1640 static int /* O - 1 on success, 0 on failure */
1641 cups_get_printer_uri(
1642 http_t
*http
, /* I - Connection to server */
1643 const char *name
, /* I - Name of printer or class */
1644 char *host
, /* I - Hostname buffer */
1645 int hostsize
, /* I - Size of hostname buffer */
1646 int *port
, /* O - Port number */
1647 char *resource
, /* I - Resource buffer */
1648 int resourcesize
, /* I - Size of resource buffer */
1649 int depth
) /* I - Depth of query */
1651 int i
; /* Looping var */
1652 int http_port
; /* Port number */
1653 http_t
*http2
; /* Alternate HTTP connection */
1654 ipp_t
*request
, /* IPP request */
1655 *response
; /* IPP response */
1656 ipp_attribute_t
*attr
; /* Current attribute */
1657 char uri
[HTTP_MAX_URI
], /* printer-uri attribute */
1658 scheme
[HTTP_MAX_URI
], /* Scheme name */
1659 username
[HTTP_MAX_URI
], /* Username:password */
1660 classname
[255], /* Temporary class name */
1661 http_hostname
[HTTP_MAX_HOST
];
1662 /* Hostname associated with connection */
1663 _cups_globals_t
*cg
= _cupsGlobals(); /* Thread global data */
1664 static const char * const requested_attrs
[] =
1665 { /* Requested attributes */
1668 "printer-uri-supported",
1673 DEBUG_printf(("7cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
1674 "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)",
1675 http
, name
, host
, hostsize
, resource
, resourcesize
, depth
));
1678 * Setup the printer URI...
1681 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1682 "localhost", 0, "/printers/%s", name
) != HTTP_URI_OK
)
1684 _cupsSetError(IPP_INTERNAL_ERROR
, _("Unable to create printer-uri"), 1);
1692 DEBUG_printf(("9cups_get_printer_uri: printer-uri=\"%s\"", uri
));
1695 * Get the hostname and port number we are connected to...
1698 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
1699 http_port
= httpAddrPort(http
->hostaddr
);
1702 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1705 * attributes-charset
1706 * attributes-natural-language
1708 * requested-attributes
1711 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1713 ippSetVersion(request
, cg
->server_version
/ 10, cg
->server_version
% 10);
1715 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1718 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1719 "requested-attributes",
1720 sizeof(requested_attrs
) / sizeof(requested_attrs
[0]),
1721 NULL
, requested_attrs
);
1724 * Do the request and get back a response...
1727 snprintf(resource
, resourcesize
, "/printers/%s", name
);
1729 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
1731 const char *device_uri
= NULL
; /* device-uri value */
1733 if ((attr
= ippFindAttribute(response
, "device-uri",
1734 IPP_TAG_URI
)) != NULL
)
1735 device_uri
= attr
->values
[0].string
.text
;
1738 ((strstr(device_uri
, "._ipp.") != NULL
||
1739 strstr(device_uri
, "._ipps.") != NULL
) &&
1740 !strcmp(device_uri
+ strlen(device_uri
) - 5, "/cups")))
1743 * Statically-configured Bonjour shared printer.
1746 httpSeparateURI(HTTP_URI_CODING_ALL
,
1747 _httpResolveURI(device_uri
, uri
, sizeof(uri
),
1748 _HTTP_RESOLVE_DEFAULT
, NULL
, NULL
),
1749 scheme
, sizeof(scheme
), username
, sizeof(username
),
1750 host
, hostsize
, port
, resource
, resourcesize
);
1751 ippDelete(response
);
1755 else if ((attr
= ippFindAttribute(response
, "member-uris",
1756 IPP_TAG_URI
)) != NULL
)
1759 * Get the first actual printer name in the class...
1762 for (i
= 0; i
< attr
->num_values
; i
++)
1764 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
,
1765 scheme
, sizeof(scheme
), username
, sizeof(username
),
1766 host
, hostsize
, port
, resource
, resourcesize
);
1767 if (!strncmp(resource
, "/printers/", 10))
1773 ippDelete(response
);
1780 * No printers in this class - try recursively looking for a printer,
1781 * but not more than 3 levels deep...
1786 for (i
= 0; i
< attr
->num_values
; i
++)
1788 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
,
1789 scheme
, sizeof(scheme
), username
, sizeof(username
),
1790 host
, hostsize
, port
, resource
, resourcesize
);
1791 if (!strncmp(resource
, "/classes/", 9))
1794 * Found a class! Connect to the right server...
1797 if (!_cups_strcasecmp(http_hostname
, host
) && *port
== http_port
)
1799 else if ((http2
= httpConnectEncrypt(host
, *port
,
1800 cupsEncryption())) == NULL
)
1802 DEBUG_puts("8cups_get_printer_uri: Unable to connect to server");
1808 * Look up printers on that server...
1811 strlcpy(classname
, resource
+ 9, sizeof(classname
));
1813 cups_get_printer_uri(http2
, classname
, host
, hostsize
, port
,
1814 resource
, resourcesize
, depth
+ 1);
1817 * Close the connection as needed...
1829 else if ((attr
= ippFindAttribute(response
, "printer-uri-supported",
1830 IPP_TAG_URI
)) != NULL
)
1832 httpSeparateURI(HTTP_URI_CODING_ALL
,
1833 _httpResolveURI(attr
->values
[0].string
.text
, uri
,
1834 sizeof(uri
), _HTTP_RESOLVE_DEFAULT
,
1836 scheme
, sizeof(scheme
), username
, sizeof(username
),
1837 host
, hostsize
, port
, resource
, resourcesize
);
1838 ippDelete(response
);
1840 if (!strncmp(resource
, "/classes/", 9))
1842 _cupsSetError(IPP_INTERNAL_ERROR
,
1843 _("No printer-uri found for class"), 1);
1854 ippDelete(response
);
1857 if (cupsLastError() != IPP_NOT_FOUND
)
1858 _cupsSetError(IPP_INTERNAL_ERROR
, _("No printer-uri found"), 1);
1868 * End of "$Id: util.c 7850 2008-08-20 00:07:25Z mike $".