2 * "$Id: util.c 7014 2007-10-10 21:57:43Z mike $"
4 * Printing utilities for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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.
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 * cupsLastError() - Return the last IPP status code.
41 * cupsLastErrorString() - Return the last IPP status-message.
42 * cupsPrintFile() - Print a file to a printer or class on the default
44 * cupsPrintFile2() - Print a file to a printer or class on the
46 * cupsPrintFiles() - Print one or more files to a printer or class on
48 * cupsPrintFiles2() - Print one or more files to a printer or class on
49 * the specified server.
50 * cupsStartDocument() - Add a document to a job created with
52 * _cupsConnect() - Get the default server connection...
53 * cups_get_printer_uri() - Get the printer-uri-supported attribute for the
54 * first printer in a class.
58 * Include necessary headers...
67 #if defined(WIN32) || defined(__EMX__)
71 #endif /* WIN32 || __EMX__ */
78 static int cups_get_printer_uri(http_t
*http
, const char *name
,
79 char *host
, int hostsize
, int *port
,
80 char *resource
, int resourcesize
,
85 * 'cupsCancelJob()' - Cancel a print job on the default server.
87 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
88 * to cancel the current job on the named destination.
90 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
91 * the cause of any failure.
94 int /* O - 1 on success, 0 on failure */
95 cupsCancelJob(const char *name
, /* I - Name of printer or class */
96 int job_id
) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
98 return (cupsCancelJob2(CUPS_HTTP_DEFAULT
, name
, job_id
, 0)
99 < IPP_REDIRECTION_OTHER_SITE
);
104 * 'cupsCancelJob2()' - Cancel or purge a print job.
106 * Canceled jobs remain in the job history while purged jobs are removed
107 * from the job history.
109 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
110 * to cancel the current job on the named destination.
112 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
113 * the cause of any failure.
118 ipp_status_t
/* O - IPP status */
119 cupsCancelJob2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
120 const char *name
, /* I - Name of printer or class */
121 int job_id
, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
122 int purge
) /* I - 1 to purge, 0 to cancel */
124 char uri
[HTTP_MAX_URI
]; /* Job/printer URI */
125 ipp_t
*request
; /* IPP request */
129 * Range check input...
132 if (job_id
< -1 || (!name
&& job_id
== 0))
134 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
));
139 * Connect to the default server as needed...
143 if ((http
= _cupsConnect()) == NULL
)
144 return (IPP_SERVICE_UNAVAILABLE
);
147 * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following
151 * attributes-natural-language
152 * job-uri or printer-uri + job-id
153 * requesting-user-name
154 * [purge-job] or [purge-jobs]
157 request
= ippNewRequest(job_id
< 0 ? IPP_PURGE_JOBS
: IPP_CANCEL_JOB
);
161 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
162 "localhost", ippPort(), "/printers/%s", name
);
164 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
166 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
171 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", job_id
);
173 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
176 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
179 if (purge
&& job_id
>= 0)
180 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-job", 1);
181 else if (!purge
&& job_id
< 0)
182 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", 0);
188 ippDelete(cupsDoRequest(http
, request
, "/jobs/"));
190 return (cupsLastError());
195 * 'cupsCreateJob()' - Create an empty job for streaming.
197 * Use this function when you want to stream print data using the
198 * @link cupsStartDocument@, @link cupsWriteRequestData@, and
199 * @link cupsFinishDocument@ functions. If you have one or more files to
200 * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function
206 int /* O - Job ID or 0 on error */
208 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
209 const char *name
, /* I - Destination name */
210 const char *title
, /* I - Title of job */
211 int num_options
, /* I - Number of options */
212 cups_option_t
*options
) /* I - Options */
214 char printer_uri
[1024], /* Printer URI */
215 resource
[1024]; /* Printer resource */
216 ipp_t
*request
, /* Create-Job request */
217 *response
; /* Create-Job response */
218 ipp_attribute_t
*attr
; /* job-id attribute */
219 int job_id
= 0; /* job-id value */
222 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", "
223 "num_options=%d, options=%p)\n",
224 http
, name
, title
, num_options
, options
));
227 * Range check input...
232 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
237 * Build a Create-Job request...
240 if ((request
= ippNewRequest(IPP_CREATE_JOB
)) == NULL
)
242 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
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 cupsEncodeOptions(request
, num_options
, options
);
260 * Send the request and get the job-id...
263 response
= cupsDoRequest(http
, request
, resource
);
265 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) != NULL
)
266 job_id
= attr
->values
[0].integer
;
279 * 'cupsFinishDocument()' - Finish sending a document.
281 * The document must have been started using @link cupsStartDocument@.
286 ipp_status_t
/* O - Status of document submission */
287 cupsFinishDocument(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
288 const char *name
) /* I - Destination name */
290 char resource
[1024]; /* Printer resource */
293 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
295 ippDelete(cupsGetResponse(http
, resource
));
297 return (cupsLastError());
302 * 'cupsFreeJobs()' - Free memory used by job data.
306 cupsFreeJobs(int num_jobs
, /* I - Number of jobs */
307 cups_job_t
*jobs
) /* I - Jobs */
309 int i
; /* Looping var */
310 cups_job_t
*job
; /* Current job */
313 if (num_jobs
<= 0 || !jobs
)
316 for (i
= num_jobs
, job
= jobs
; i
> 0; i
--, job
++)
318 _cupsStrFree(job
->dest
);
319 _cupsStrFree(job
->user
);
320 _cupsStrFree(job
->format
);
321 _cupsStrFree(job
->title
);
329 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
331 * This function is deprecated - use @link cupsGetDests@ instead.
336 int /* O - Number of classes */
337 cupsGetClasses(char ***classes
) /* O - Classes */
339 int n
; /* Number of classes */
340 ipp_t
*request
, /* IPP Request */
341 *response
; /* IPP Response */
342 ipp_attribute_t
*attr
; /* Current attribute */
343 char **temp
; /* Temporary pointer */
344 http_t
*http
; /* Connection to server */
349 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
356 if ((http
= _cupsConnect()) == NULL
)
360 * Build a CUPS_GET_CLASSES request, which requires the following
364 * attributes-natural-language
365 * requested-attributes
368 request
= ippNewRequest(CUPS_GET_CLASSES
);
370 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
371 "requested-attributes", NULL
, "printer-name");
374 * Do the request and get back a response...
379 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
381 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
382 if (attr
->name
!= NULL
&&
383 strcasecmp(attr
->name
, "printer-name") == 0 &&
384 attr
->value_tag
== IPP_TAG_NAME
)
387 temp
= malloc(sizeof(char *));
389 temp
= realloc(*classes
, sizeof(char *) * (n
+ 1));
409 temp
[n
] = strdup(attr
->values
[0].string
.text
);
421 * 'cupsGetDefault()' - Get the default printer or class for the default server.
423 * This function returns the default printer or class as defined by
424 * the LPDEST or PRINTER environment variables. If these environment
425 * variables are not set, the server default destination is returned.
426 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
427 * functions to get the user-defined default printer, as this function does
428 * not support the lpoptions-defined default printer.
431 const char * /* O - Default printer or @code NULL@ */
435 * Return the default printer...
438 return (cupsGetDefault2(CUPS_HTTP_DEFAULT
));
443 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
445 * This function returns the default printer or class as defined by
446 * the LPDEST or PRINTER environment variables. If these environment
447 * variables are not set, the server default destination is returned.
448 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
449 * functions to get the user-defined default printer, as this function does
450 * not support the lpoptions-defined default printer.
452 * @since CUPS 1.1.21@
455 const char * /* O - Default printer or @code NULL@ */
456 cupsGetDefault2(http_t
*http
) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
458 ipp_t
*request
, /* IPP Request */
459 *response
; /* IPP Response */
460 ipp_attribute_t
*attr
; /* Current attribute */
461 const char *var
; /* Environment variable */
462 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
466 * First see if the LPDEST or PRINTER environment variables are
467 * set... However, if PRINTER is set to "lp", ignore it to work
468 * around a "feature" in most Linux distributions - the default
469 * user login scripts set PRINTER to "lp"...
472 if ((var
= getenv("LPDEST")) != NULL
)
474 else if ((var
= getenv("PRINTER")) != NULL
&& strcmp(var
, "lp") != 0)
478 * Connect to the server as needed...
482 if ((http
= _cupsConnect()) == NULL
)
486 * Build a CUPS_GET_DEFAULT request, which requires the following
490 * attributes-natural-language
493 request
= ippNewRequest(CUPS_GET_DEFAULT
);
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@
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 */
585 "time-at-processing",
589 "job-originating-user-name"
594 * Range check input...
599 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
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
, NULL
);
619 strcpy(uri
, "ipp://localhost/jobs");
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 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
641 "printer-uri", NULL
, uri
);
643 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
644 "requesting-user-name", NULL
, cupsUser());
647 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
649 if (whichjobs
== CUPS_WHICHJOBS_COMPLETED
)
650 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
651 "which-jobs", NULL
, "completed");
652 else if (whichjobs
== CUPS_WHICHJOBS_ALL
)
653 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
654 "which-jobs", NULL
, "all");
656 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
657 "requested-attributes", sizeof(attrs
) / sizeof(attrs
[0]),
661 * Do the request and get back a response...
667 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
669 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
672 * Skip leading attributes until we hit a job...
675 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
682 * Pull the needed attributes from this job...
688 state
= IPP_JOB_PENDING
;
691 format
= "application/octet-stream";
697 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
699 if (!strcmp(attr
->name
, "job-id") &&
700 attr
->value_tag
== IPP_TAG_INTEGER
)
701 id
= attr
->values
[0].integer
;
702 else if (!strcmp(attr
->name
, "job-state") &&
703 attr
->value_tag
== IPP_TAG_ENUM
)
704 state
= (ipp_jstate_t
)attr
->values
[0].integer
;
705 else if (!strcmp(attr
->name
, "job-priority") &&
706 attr
->value_tag
== IPP_TAG_INTEGER
)
707 priority
= attr
->values
[0].integer
;
708 else if (!strcmp(attr
->name
, "job-k-octets") &&
709 attr
->value_tag
== IPP_TAG_INTEGER
)
710 size
= attr
->values
[0].integer
;
711 else if (!strcmp(attr
->name
, "time-at-completed") &&
712 attr
->value_tag
== IPP_TAG_INTEGER
)
713 completed_time
= attr
->values
[0].integer
;
714 else if (!strcmp(attr
->name
, "time-at-creation") &&
715 attr
->value_tag
== IPP_TAG_INTEGER
)
716 creation_time
= attr
->values
[0].integer
;
717 else if (!strcmp(attr
->name
, "time-at-processing") &&
718 attr
->value_tag
== IPP_TAG_INTEGER
)
719 processing_time
= attr
->values
[0].integer
;
720 else if (!strcmp(attr
->name
, "job-printer-uri") &&
721 attr
->value_tag
== IPP_TAG_URI
)
723 if ((dest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
726 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
727 attr
->value_tag
== IPP_TAG_NAME
)
728 user
= attr
->values
[0].string
.text
;
729 else if (!strcmp(attr
->name
, "document-format") &&
730 attr
->value_tag
== IPP_TAG_MIMETYPE
)
731 format
= attr
->values
[0].string
.text
;
732 else if (!strcmp(attr
->name
, "job-name") &&
733 (attr
->value_tag
== IPP_TAG_TEXT
||
734 attr
->value_tag
== IPP_TAG_NAME
))
735 title
= attr
->values
[0].string
.text
;
741 * See if we have everything needed...
753 * Allocate memory for the job...
757 temp
= malloc(sizeof(cups_job_t
));
759 temp
= realloc(*jobs
, sizeof(cups_job_t
) * (n
+ 1));
767 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
769 cupsFreeJobs(n
, *jobs
);
782 * Copy the data over...
785 temp
->dest
= _cupsStrAlloc(dest
);
786 temp
->user
= _cupsStrAlloc(user
);
787 temp
->format
= _cupsStrAlloc(format
);
788 temp
->title
= _cupsStrAlloc(title
);
790 temp
->priority
= priority
;
793 temp
->completed_time
= completed_time
;
794 temp
->creation_time
= creation_time
;
795 temp
->processing_time
= processing_time
;
804 if (n
== 0 && cg
->last_error
>= IPP_BAD_REQUEST
)
812 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
814 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
817 * The returned filename is stored in a static buffer and is overwritten with
818 * each call to @code cupsGetPPD@ or @link cupsGetPPD2@.
821 const char * /* O - Filename for PPD file */
822 cupsGetPPD(const char *name
) /* I - Destination name */
824 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
825 time_t modtime
= 0; /* Modification time */
829 * Return the PPD file...
832 cg
->ppd_filename
[0] = '\0';
834 if (cupsGetPPD3(CUPS_HTTP_DEFAULT
, name
, &modtime
, cg
->ppd_filename
,
835 sizeof(cg
->ppd_filename
)) == HTTP_OK
)
836 return (cg
->ppd_filename
);
843 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
845 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
848 * The returned filename is stored in a static buffer and is overwritten with
849 * each call to @link cupsGetPPD@ or @code cupsGetPPD2@.
851 * @since CUPS 1.1.21@
854 const char * /* O - Filename for PPD file */
855 cupsGetPPD2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
856 const char *name
) /* I - Destination name */
858 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
859 time_t modtime
= 0; /* Modification time */
862 cg
->ppd_filename
[0] = '\0';
864 if (cupsGetPPD3(http
, name
, &modtime
, cg
->ppd_filename
,
865 sizeof(cg
->ppd_filename
)) == HTTP_OK
)
866 return (cg
->ppd_filename
);
873 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
874 * server if it has changed.
876 * The "modtime" parameter contains the modification time of any
877 * locally-cached content and is updated with the time from the PPD file on
880 * The "buffer" parameter contains the local PPD filename. If it contains
881 * the empty string, a new temporary file is created, otherwise the existing
882 * file will be overwritten as needed.
884 * On success, @code HTTP_OK@ is returned for a new PPD file and
885 * @code HTTP_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other
886 * status is an error.
888 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
894 http_status_t
/* O - HTTP status */
895 cupsGetPPD3(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
896 const char *name
, /* I - Destination name */
897 time_t *modtime
, /* IO - Modification time */
898 char *buffer
, /* I - Filename buffer */
899 size_t bufsize
) /* I - Size of filename buffer */
901 int http_port
; /* Port number */
902 char http_hostname
[HTTP_MAX_HOST
];
903 /* Hostname associated with connection */
904 http_t
*http2
; /* Alternate HTTP connection */
905 int fd
; /* PPD file */
906 char localhost
[HTTP_MAX_URI
],/* Local hostname */
907 hostname
[HTTP_MAX_URI
], /* Hostname */
908 resource
[HTTP_MAX_URI
]; /* Resource name */
909 int port
; /* Port number */
910 http_status_t status
; /* HTTP status from server */
911 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
915 * Range check input...
918 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
919 "bufsize=%d)\n", http
, name
? name
: "(null)", modtime
,
920 modtime
? (int)*modtime
: 0, buffer
, (int)bufsize
));
924 _cupsSetError(IPP_INTERNAL_ERROR
, "No printer name!");
925 return (HTTP_NOT_ACCEPTABLE
);
930 _cupsSetError(IPP_INTERNAL_ERROR
, "No modification time!");
931 return (HTTP_NOT_ACCEPTABLE
);
934 if (!buffer
|| bufsize
<= 1)
936 _cupsSetError(IPP_INTERNAL_ERROR
, "Bad filename buffer!");
937 return (HTTP_NOT_ACCEPTABLE
);
941 * Try finding a printer URI for this printer...
945 if ((http
= _cupsConnect()) == NULL
)
946 return (HTTP_SERVICE_UNAVAILABLE
);
948 if (!cups_get_printer_uri(http
, name
, hostname
, sizeof(hostname
), &port
,
949 resource
, sizeof(resource
), 0))
950 return (HTTP_NOT_FOUND
);
952 DEBUG_printf(("Printer hostname=\"%s\", port=%d\n", hostname
, port
));
955 * Remap local hostname to localhost...
958 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
960 DEBUG_printf(("Local hostname=\"%s\"\n", localhost
));
962 if (!strcasecmp(localhost
, hostname
))
963 strcpy(hostname
, "localhost");
966 * Get the hostname and port number we are connected to...
969 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
972 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
973 http_port
= ntohs(http
->hostaddr
->ipv6
.sin6_port
);
975 #endif /* AF_INET6 */
976 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
977 http_port
= ntohs(http
->hostaddr
->ipv4
.sin_port
);
979 http_port
= ippPort();
981 DEBUG_printf(("Connection hostname=\"%s\", port=%d\n", http_hostname
,
985 * Reconnect to the correct server as needed...
988 if (!strcasecmp(http_hostname
, hostname
) && port
== http_port
)
990 else if ((http2
= httpConnectEncrypt(hostname
, port
,
991 cupsEncryption())) == NULL
)
993 DEBUG_puts("Unable to connect to server!");
995 return (HTTP_SERVICE_UNAVAILABLE
);
1003 fd
= open(buffer
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
1005 fd
= cupsTempFd(buffer
, bufsize
);
1010 * Can't open file; close the server connection and return NULL...
1013 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1018 return (HTTP_SERVER_ERROR
);
1022 * And send a request to the HTTP server...
1025 strlcat(resource
, ".ppd", sizeof(resource
));
1028 httpSetField(http2
, HTTP_FIELD_IF_MODIFIED_SINCE
,
1029 httpGetDateString(*modtime
));
1031 status
= cupsGetFd(http2
, resource
, fd
);
1036 * See if we actually got the file or an error...
1039 if (status
== HTTP_OK
)
1040 *modtime
= httpGetDateTime(httpGetField(http2
, HTTP_FIELD_DATE
));
1041 else if (status
!= HTTP_NOT_MODIFIED
)
1045 case HTTP_NOT_FOUND
:
1046 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
1049 case HTTP_UNAUTHORIZED
:
1050 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
1054 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
1056 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
1060 unlink(cg
->ppd_filename
);
1067 * Return the PPD file...
1075 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1077 * This function is deprecated - use @link cupsGetDests@ instead.
1082 int /* O - Number of printers */
1083 cupsGetPrinters(char ***printers
) /* O - Printers */
1085 int n
; /* Number of printers */
1086 ipp_t
*request
, /* IPP Request */
1087 *response
; /* IPP Response */
1088 ipp_attribute_t
*attr
; /* Current attribute */
1089 char **temp
; /* Temporary pointer */
1090 http_t
*http
; /* Connection to server */
1094 * Range check input...
1099 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1107 * Try to connect to the server...
1110 if ((http
= _cupsConnect()) == NULL
)
1114 * Build a CUPS_GET_PRINTERS request, which requires the following
1117 * attributes-charset
1118 * attributes-natural-language
1119 * requested-attributes
1122 request
= ippNewRequest(CUPS_GET_PRINTERS
);
1124 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1125 "requested-attributes", NULL
, "printer-name");
1127 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1130 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1131 "printer-type-mask", CUPS_PRINTER_CLASS
);
1134 * Do the request and get back a response...
1139 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1141 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1142 if (attr
->name
!= NULL
&&
1143 strcasecmp(attr
->name
, "printer-name") == 0 &&
1144 attr
->value_tag
== IPP_TAG_NAME
)
1147 temp
= malloc(sizeof(char *));
1149 temp
= realloc(*printers
, sizeof(char *) * (n
+ 1));
1154 * Ran out of memory!
1160 free((*printers
)[n
]);
1164 ippDelete(response
);
1169 temp
[n
] = strdup(attr
->values
[0].string
.text
);
1173 ippDelete(response
);
1181 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1183 * This function returns the named PPD file from the server. The
1184 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
1187 * You must remove (unlink) the PPD file when you are finished with
1188 * it. The PPD filename is stored in a static location that will be
1189 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1190 * or @link cupsGetServerPPD@.
1195 char * /* O - Name of PPD file or @code NULL@ on error */
1196 cupsGetServerPPD(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1197 const char *name
) /* I - Name of PPD file ("ppd-name") */
1199 int fd
; /* PPD file descriptor */
1200 ipp_t
*request
; /* IPP request */
1201 _cups_globals_t
*cg
= _cupsGlobals();
1202 /* Pointer to library globals */
1206 * Range check input...
1211 _cupsSetError(IPP_INTERNAL_ERROR
, "No PPD name!");
1217 if ((http
= _cupsConnect()) == NULL
)
1221 * Get a temp file...
1224 if ((fd
= cupsTempFd(cg
->ppd_filename
, sizeof(cg
->ppd_filename
))) < 0)
1227 * Can't open file; close the server connection and return NULL...
1230 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1236 * Get the PPD file...
1239 request
= ippNewRequest(CUPS_GET_PPD
);
1240 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "ppd-name", NULL
,
1243 ippDelete(cupsDoIORequest(http
, request
, "/", -1, fd
));
1247 if (cupsLastError() != IPP_OK
)
1249 unlink(cg
->ppd_filename
);
1253 return (cg
->ppd_filename
);
1258 * 'cupsLastError()' - Return the last IPP status code.
1261 ipp_status_t
/* O - IPP status code from last request */
1264 return (_cupsGlobals()->last_error
);
1269 * 'cupsLastErrorString()' - Return the last IPP status-message.
1274 const char * /* O - status-message text from last request */
1275 cupsLastErrorString(void)
1277 return (_cupsGlobals()->last_status_message
);
1282 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1285 int /* O - Job ID or 0 on error */
1286 cupsPrintFile(const char *name
, /* I - Destination name */
1287 const char *filename
, /* I - File to print */
1288 const char *title
, /* I - Title of job */
1289 int num_options
,/* I - Number of options */
1290 cups_option_t
*options
) /* I - Options */
1292 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
1293 "title=\"%s\", num_options=%d, options=%p)\n",
1294 name
, filename
, title
, num_options
, options
));
1296 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, 1, &filename
, title
,
1297 num_options
, options
));
1302 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1305 * @since CUPS 1.1.21@
1308 int /* O - Job ID or 0 on error */
1310 http_t
*http
, /* I - Connection to server */
1311 const char *name
, /* I - Destination name */
1312 const char *filename
, /* I - File to print */
1313 const char *title
, /* I - Title of job */
1314 int num_options
, /* I - Number of options */
1315 cups_option_t
*options
) /* I - Options */
1317 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
1318 "title=\"%s\", num_options=%d, options=%p)\n",
1319 http
, name
, filename
, title
, num_options
, options
));
1321 return (cupsPrintFiles2(http
, name
, 1, &filename
, title
, num_options
,
1327 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1331 int /* O - Job ID or 0 on error */
1333 const char *name
, /* I - Destination name */
1334 int num_files
, /* I - Number of files */
1335 const char **files
, /* I - File(s) to print */
1336 const char *title
, /* I - Title of job */
1337 int num_options
, /* I - Number of options */
1338 cups_option_t
*options
) /* I - Options */
1340 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
1341 "files=%p, title=\"%s\", num_options=%d, options=%p)\n",
1342 name
, num_files
, files
, title
, num_options
, options
));
1346 * Print the file(s)...
1349 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, num_files
, files
, title
,
1350 num_options
, options
));
1355 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1358 * @since CUPS 1.1.21@
1361 int /* O - Job ID or 0 on error */
1363 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1364 const char *name
, /* I - Destination name */
1365 int num_files
, /* I - Number of files */
1366 const char **files
, /* I - File(s) to print */
1367 const char *title
, /* I - Title of job */
1368 int num_options
, /* I - Number of options */
1369 cups_option_t
*options
) /* I - Options */
1371 int i
; /* Looping var */
1372 int job_id
; /* New job ID */
1373 const char *docname
; /* Basename of current filename */
1374 const char *format
; /* Document format */
1375 cups_file_t
*fp
; /* Current file */
1376 char buffer
[8192]; /* Copy buffer */
1377 ssize_t bytes
; /* Bytes in buffer */
1378 http_status_t status
; /* Status of write */
1381 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
1382 "files=%p, title=\"%s\", num_options=%d, options=%p)\n",
1383 http
, name
, num_files
, files
, title
, num_options
, options
));
1386 * Range check input...
1389 if (!name
|| num_files
< 1 || !files
)
1391 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1397 * Create the print job...
1400 if ((job_id
= cupsCreateJob(http
, name
, title
, num_options
, options
)) == 0)
1404 * Send each of the files...
1407 if (cupsGetOption("raw", num_options
, options
))
1408 format
= CUPS_FORMAT_RAW
;
1409 else if ((format
= cupsGetOption("document-format", num_options
,
1411 format
= CUPS_FORMAT_AUTO
;
1413 for (i
= 0; i
< num_files
; i
++)
1416 * Start the next file...
1419 if ((docname
= strrchr(files
[i
], '/')) != NULL
)
1424 if ((fp
= cupsFileOpen(files
[i
], "rb")) == NULL
)
1427 * Unable to open print file, cancel the job and return...
1430 cupsCancelJob2(http
, name
, job_id
, 0);
1434 status
= cupsStartDocument(http
, name
, job_id
, docname
, format
,
1435 i
== (num_files
- 1));
1437 while (status
== HTTP_CONTINUE
&&
1438 (bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1439 status
= cupsWriteRequestData(http
, buffer
, bytes
);
1443 if (status
!= HTTP_CONTINUE
|| cupsFinishDocument(http
, name
) != IPP_OK
)
1446 * Unable to queue, cancel the job and return...
1449 cupsCancelJob2(http
, name
, job_id
, 0);
1459 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1461 * Use @link cupsWriteRequestData@ to write data for the document and
1462 * @link cupsFinishDocument@ to finish the document and get the submission status.
1464 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1465 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1466 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1467 * any supported MIME type string can be supplied.
1472 http_status_t
/* O - HTTP status of request */
1474 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1475 const char *name
, /* I - Destination name */
1476 int job_id
, /* I - Job ID from @link cupsCreateJob@ */
1477 const char *docname
, /* I - Name of document */
1478 const char *format
, /* I - MIME type or @code CUPS_FORMAT_foo@ */
1479 int last_document
) /* I - 1 for last document in job, 0 otherwise */
1481 char resource
[1024], /* Resource for destinatio */
1482 printer_uri
[1024]; /* Printer URI */
1483 ipp_t
*request
; /* Send-Document request */
1484 http_status_t status
; /* HTTP status */
1488 * Create a Send-Document request...
1491 if ((request
= ippNewRequest(IPP_SEND_DOCUMENT
)) == NULL
)
1493 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1497 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
), "ipp",
1498 NULL
, "localhost", ippPort(), "/printers/%s", name
);
1499 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
1501 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1503 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", job_id
);
1504 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1507 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
1510 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1511 "document-format", NULL
, format
);
1512 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", last_document
);
1515 * Send and delete the request, then return the status...
1518 status
= cupsSendRequest(http
, request
, resource
, CUPS_LENGTH_VARIABLE
);
1527 * '_cupsConnect()' - Get the default server connection...
1530 http_t
* /* O - HTTP connection */
1533 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1537 * See if we are connected to the same server...
1542 int port
; /* Port for connection */
1546 * Get the port associated with the current connection...
1550 if (cg
->http
->hostaddr
->addr
.sa_family
== AF_INET6
)
1551 port
= ntohs(cg
->http
->hostaddr
->ipv6
.sin6_port
);
1553 #endif /* AF_INET6 */
1554 if (cg
->http
->hostaddr
->addr
.sa_family
== AF_INET
)
1555 port
= ntohs(cg
->http
->hostaddr
->ipv4
.sin_port
);
1557 port
= cg
->ipp_port
;
1560 * Compare the connection hostname, port, and encryption settings to
1561 * the cached defaults; these were initialized the first time we
1565 if (strcmp(cg
->http
->hostname
, cg
->server
) || cg
->ipp_port
!= port
||
1566 (cg
->http
->encryption
!= cg
->encryption
&&
1567 cg
->http
->encryption
== HTTP_ENCRYPT_NEVER
))
1570 * Need to close the current connection because something has changed...
1573 httpClose(cg
->http
);
1579 * (Re)connect as needed...
1584 if ((cg
->http
= httpConnectEncrypt(cupsServer(), ippPort(),
1585 cupsEncryption())) == NULL
)
1586 _cupsSetError(IPP_SERVICE_UNAVAILABLE
,
1587 errno
? strerror(errno
) : "Unable to connect to host.");
1591 * Return the cached connection...
1599 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the first printer in a class.
1602 static int /* O - 1 on success, 0 on failure */
1603 cups_get_printer_uri(
1604 http_t
*http
, /* I - Connection to server */
1605 const char *name
, /* I - Name of printer or class */
1606 char *host
, /* I - Hostname buffer */
1607 int hostsize
, /* I - Size of hostname buffer */
1608 int *port
, /* O - Port number */
1609 char *resource
, /* I - Resource buffer */
1610 int resourcesize
, /* I - Size of resource buffer */
1611 int depth
) /* I - Depth of query */
1613 int i
; /* Looping var */
1614 int http_port
; /* Port number */
1615 http_t
*http2
; /* Alternate HTTP connection */
1616 ipp_t
*request
, /* IPP request */
1617 *response
; /* IPP response */
1618 ipp_attribute_t
*attr
; /* Current attribute */
1619 char uri
[HTTP_MAX_URI
], /* printer-uri attribute */
1620 scheme
[HTTP_MAX_URI
], /* Scheme name */
1621 username
[HTTP_MAX_URI
], /* Username:password */
1622 classname
[255], /* Temporary class name */
1623 http_hostname
[HTTP_MAX_HOST
];
1624 /* Hostname associated with connection */
1625 static const char * const requested_attrs
[] =
1626 { /* Requested attributes */
1627 "printer-uri-supported",
1633 DEBUG_printf(("cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
1634 "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)\n",
1635 http
, name
? name
: "(null)", host
, hostsize
,
1636 resource
, resourcesize
, depth
));
1639 * Setup the printer URI...
1642 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1643 "localhost", 0, "/printers/%s", name
) != HTTP_URI_OK
)
1645 _cupsSetError(IPP_INTERNAL_ERROR
, "Unable to create printer-uri!");
1653 DEBUG_printf(("cups_get_printer_uri: printer-uri=\"%s\"\n", uri
));
1656 * Get the hostname and port number we are connected to...
1659 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
1662 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
1663 http_port
= ntohs(http
->hostaddr
->ipv6
.sin6_port
);
1665 #endif /* AF_INET6 */
1666 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
1667 http_port
= ntohs(http
->hostaddr
->ipv4
.sin_port
);
1669 http_port
= ippPort();
1672 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1675 * attributes-charset
1676 * attributes-natural-language
1678 * requested-attributes
1681 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1683 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1686 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1687 "requested-attributes",
1688 sizeof(requested_attrs
) / sizeof(requested_attrs
[0]),
1689 NULL
, requested_attrs
);
1692 * Do the request and get back a response...
1695 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1697 if ((attr
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
)) != NULL
)
1700 * Get the first actual printer name in the class...
1703 for (i
= 0; i
< attr
->num_values
; i
++)
1705 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
,
1706 scheme
, sizeof(scheme
), username
, sizeof(username
),
1707 host
, hostsize
, port
, resource
, resourcesize
);
1708 if (!strncmp(resource
, "/printers/", 10))
1714 ippDelete(response
);
1721 * No printers in this class - try recursively looking for a printer,
1722 * but not more than 3 levels deep...
1727 for (i
= 0; i
< attr
->num_values
; i
++)
1729 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
,
1730 scheme
, sizeof(scheme
), username
, sizeof(username
),
1731 host
, hostsize
, port
, resource
, resourcesize
);
1732 if (!strncmp(resource
, "/classes/", 9))
1735 * Found a class! Connect to the right server...
1738 if (!strcasecmp(http_hostname
, host
) && *port
== http_port
)
1740 else if ((http2
= httpConnectEncrypt(host
, *port
,
1741 cupsEncryption())) == NULL
)
1743 DEBUG_puts("Unable to connect to server!");
1749 * Look up printers on that server...
1752 strlcpy(classname
, resource
+ 9, sizeof(classname
));
1754 cups_get_printer_uri(http2
, classname
, host
, hostsize
, port
,
1755 resource
, resourcesize
, depth
+ 1);
1758 * Close the connection as needed...
1770 else if ((attr
= ippFindAttribute(response
, "printer-uri-supported",
1771 IPP_TAG_URI
)) != NULL
)
1773 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
,
1774 scheme
, sizeof(scheme
), username
, sizeof(username
),
1775 host
, hostsize
, port
, resource
, resourcesize
);
1776 ippDelete(response
);
1781 ippDelete(response
);
1784 if (cupsLastError() != IPP_NOT_FOUND
)
1785 _cupsSetError(IPP_INTERNAL_ERROR
, "No printer-uri found!");
1795 * End of "$Id: util.c 7014 2007-10-10 21:57:43Z mike $".