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 */
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 - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
120 const char *name
, /* I - Name of printer or class */
121 int job_id
, /* I - Job ID or 0 for the current job, -1 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.
197 * Submit files for printing to the job using the @link cupsStartDocument@,
198 * @link cupsWriteRequestData@, and @link cupsFinishDocument@ functions.
203 int /* O - Job ID or 0 on error */
205 http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
206 const char *name
, /* I - Printer or class 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 */
219 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", "
220 "num_options=%d, options=%p)\n",
221 http
, name
, title
, num_options
, options
));
224 * Range check input...
229 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
234 * Build a Create-Job request...
237 if ((request
= ippNewRequest(IPP_CREATE_JOB
)) == NULL
)
239 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
243 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
), "ipp",
244 NULL
, "localhost", ippPort(), "/printers/%s", name
);
245 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
247 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
249 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
252 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
,
254 cupsEncodeOptions(request
, num_options
, options
);
257 * Send the request and get the job-id...
260 response
= cupsDoRequest(http
, request
, resource
);
262 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) != NULL
)
263 job_id
= attr
->values
[0].integer
;
276 * 'cupsFinishDocument()' - Finish sending a document.
278 * The document must have been started using @link cupsStartDocument@.
283 ipp_status_t
/* O - Status of document submission */
284 cupsFinishDocument(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
285 const char *name
) /* I - Printer or class name */
287 char resource
[1024]; /* Printer resource */
290 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
292 ippDelete(cupsGetResponse(http
, resource
));
294 return (cupsLastError());
299 * 'cupsFreeJobs()' - Free memory used by job data.
303 cupsFreeJobs(int num_jobs
, /* I - Number of jobs */
304 cups_job_t
*jobs
) /* I - Jobs */
306 int i
; /* Looping var */
307 cups_job_t
*job
; /* Current job */
310 if (num_jobs
<= 0 || !jobs
)
313 for (i
= num_jobs
, job
= jobs
; i
> 0; i
--, job
++)
315 _cupsStrFree(job
->dest
);
316 _cupsStrFree(job
->user
);
317 _cupsStrFree(job
->format
);
318 _cupsStrFree(job
->title
);
326 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
328 * This function is deprecated - use @link cupsGetDests@ instead.
333 int /* O - Number of classes */
334 cupsGetClasses(char ***classes
) /* O - Classes */
336 int n
; /* Number of classes */
337 ipp_t
*request
, /* IPP Request */
338 *response
; /* IPP Response */
339 ipp_attribute_t
*attr
; /* Current attribute */
340 char **temp
; /* Temporary pointer */
341 http_t
*http
; /* Connection to server */
346 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
353 if ((http
= _cupsConnect()) == NULL
)
357 * Build a CUPS_GET_CLASSES request, which requires the following
361 * attributes-natural-language
362 * requested-attributes
365 request
= ippNewRequest(CUPS_GET_CLASSES
);
367 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
368 "requested-attributes", NULL
, "printer-name");
371 * Do the request and get back a response...
376 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
378 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
379 if (attr
->name
!= NULL
&&
380 strcasecmp(attr
->name
, "printer-name") == 0 &&
381 attr
->value_tag
== IPP_TAG_NAME
)
384 temp
= malloc(sizeof(char *));
386 temp
= realloc(*classes
, sizeof(char *) * (n
+ 1));
406 temp
[n
] = strdup(attr
->values
[0].string
.text
);
418 * 'cupsGetDefault()' - Get the default printer or class for the default server.
420 * This function returns the default printer or class as defined by
421 * the LPDEST or PRINTER environment variables. If these environment
422 * variables are not set, the server default destination is returned.
423 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
424 * functions to get the user-defined default printer, as this function does
425 * not support the lpoptions-defined default printer.
428 const char * /* O - Default printer or @code NULL@ */
432 * Return the default printer...
435 return (cupsGetDefault2(CUPS_HTTP_DEFAULT
));
440 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
442 * This function returns the default printer or class as defined by
443 * the LPDEST or PRINTER environment variables. If these environment
444 * variables are not set, the server default destination is returned.
445 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
446 * functions to get the user-defined default printer, as this function does
447 * not support the lpoptions-defined default printer.
449 * @since CUPS 1.1.21@
452 const char * /* O - Default printer or @code NULL@ */
453 cupsGetDefault2(http_t
*http
) /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
455 ipp_t
*request
, /* IPP Request */
456 *response
; /* IPP Response */
457 ipp_attribute_t
*attr
; /* Current attribute */
458 const char *var
; /* Environment variable */
459 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
463 * First see if the LPDEST or PRINTER environment variables are
464 * set... However, if PRINTER is set to "lp", ignore it to work
465 * around a "feature" in most Linux distributions - the default
466 * user login scripts set PRINTER to "lp"...
469 if ((var
= getenv("LPDEST")) != NULL
)
471 else if ((var
= getenv("PRINTER")) != NULL
&& strcmp(var
, "lp") != 0)
475 * Connect to the server as needed...
479 if ((http
= _cupsConnect()) == NULL
)
483 * Build a CUPS_GET_DEFAULT request, which requires the following
487 * attributes-natural-language
490 request
= ippNewRequest(CUPS_GET_DEFAULT
);
493 * Do the request and get back a response...
496 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
498 if ((attr
= ippFindAttribute(response
, "printer-name",
499 IPP_TAG_NAME
)) != NULL
)
501 strlcpy(cg
->def_printer
, attr
->values
[0].string
.text
,
502 sizeof(cg
->def_printer
));
504 return (cg
->def_printer
);
515 * 'cupsGetJobs()' - Get the jobs from the default server.
517 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
518 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
519 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
520 * jobs that are stopped, canceled, aborted, or completed.
523 int /* O - Number of jobs */
524 cupsGetJobs(cups_job_t
**jobs
, /* O - Job data */
525 const char *name
, /* I - @code NULL@ = all destinations, otherwise show jobs for mydest */
526 int myjobs
, /* I - 0 = all users, 1 = mine */
527 int whichjobs
) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
533 return (cupsGetJobs2(CUPS_HTTP_DEFAULT
, jobs
, name
, myjobs
, whichjobs
));
539 * 'cupsGetJobs2()' - Get the jobs from the specified server.
541 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
542 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
543 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
544 * jobs that are stopped, canceled, aborted, or completed.
546 * @since CUPS 1.1.21@
549 int /* O - Number of jobs */
550 cupsGetJobs2(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
551 cups_job_t
**jobs
, /* O - Job data */
552 const char *name
, /* I - @code NULL@ = all destinations, otherwise show jobs for mydest */
553 int myjobs
, /* I - 0 = all users, 1 = mine */
554 int whichjobs
) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
556 int n
; /* Number of jobs */
557 ipp_t
*request
, /* IPP Request */
558 *response
; /* IPP Response */
559 ipp_attribute_t
*attr
; /* Current attribute */
560 cups_job_t
*temp
; /* Temporary pointer */
562 priority
, /* job-priority */
563 size
; /* job-k-octets */
564 ipp_jstate_t state
; /* job-state */
565 time_t completed_time
, /* time-at-completed */
566 creation_time
, /* time-at-creation */
567 processing_time
; /* time-at-processing */
568 const char *dest
, /* job-printer-uri */
569 *format
, /* document-format */
570 *title
, /* job-name */
571 *user
; /* job-originating-user-name */
572 char uri
[HTTP_MAX_URI
]; /* URI for jobs */
573 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
574 static const char * const attrs
[] = /* Requested attributes */
582 "time-at-processing",
586 "job-originating-user-name"
591 * Range check input...
596 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
602 * Get the right URI...
607 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
608 "localhost", 0, "/printers/%s", name
) != HTTP_URI_OK
)
610 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
616 strcpy(uri
, "ipp://localhost/jobs");
619 if ((http
= _cupsConnect()) == NULL
)
623 * Build an IPP_GET_JOBS request, which requires the following
627 * attributes-natural-language
629 * requesting-user-name
632 * requested-attributes
635 request
= ippNewRequest(IPP_GET_JOBS
);
637 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
638 "printer-uri", NULL
, uri
);
640 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
641 "requesting-user-name", NULL
, cupsUser());
644 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
646 if (whichjobs
== CUPS_WHICHJOBS_COMPLETED
)
647 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
648 "which-jobs", NULL
, "completed");
649 else if (whichjobs
== CUPS_WHICHJOBS_ALL
)
650 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
651 "which-jobs", NULL
, "all");
653 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
654 "requested-attributes", sizeof(attrs
) / sizeof(attrs
[0]),
658 * Do the request and get back a response...
664 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
666 for (attr
= response
->attrs
; attr
; attr
= attr
->next
)
669 * Skip leading attributes until we hit a job...
672 while (attr
&& attr
->group_tag
!= IPP_TAG_JOB
)
679 * Pull the needed attributes from this job...
685 state
= IPP_JOB_PENDING
;
688 format
= "application/octet-stream";
694 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
696 if (!strcmp(attr
->name
, "job-id") &&
697 attr
->value_tag
== IPP_TAG_INTEGER
)
698 id
= attr
->values
[0].integer
;
699 else if (!strcmp(attr
->name
, "job-state") &&
700 attr
->value_tag
== IPP_TAG_ENUM
)
701 state
= (ipp_jstate_t
)attr
->values
[0].integer
;
702 else if (!strcmp(attr
->name
, "job-priority") &&
703 attr
->value_tag
== IPP_TAG_INTEGER
)
704 priority
= attr
->values
[0].integer
;
705 else if (!strcmp(attr
->name
, "job-k-octets") &&
706 attr
->value_tag
== IPP_TAG_INTEGER
)
707 size
= attr
->values
[0].integer
;
708 else if (!strcmp(attr
->name
, "time-at-completed") &&
709 attr
->value_tag
== IPP_TAG_INTEGER
)
710 completed_time
= attr
->values
[0].integer
;
711 else if (!strcmp(attr
->name
, "time-at-creation") &&
712 attr
->value_tag
== IPP_TAG_INTEGER
)
713 creation_time
= attr
->values
[0].integer
;
714 else if (!strcmp(attr
->name
, "time-at-processing") &&
715 attr
->value_tag
== IPP_TAG_INTEGER
)
716 processing_time
= attr
->values
[0].integer
;
717 else if (!strcmp(attr
->name
, "job-printer-uri") &&
718 attr
->value_tag
== IPP_TAG_URI
)
720 if ((dest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
723 else if (!strcmp(attr
->name
, "job-originating-user-name") &&
724 attr
->value_tag
== IPP_TAG_NAME
)
725 user
= attr
->values
[0].string
.text
;
726 else if (!strcmp(attr
->name
, "document-format") &&
727 attr
->value_tag
== IPP_TAG_MIMETYPE
)
728 format
= attr
->values
[0].string
.text
;
729 else if (!strcmp(attr
->name
, "job-name") &&
730 (attr
->value_tag
== IPP_TAG_TEXT
||
731 attr
->value_tag
== IPP_TAG_NAME
))
732 title
= attr
->values
[0].string
.text
;
738 * See if we have everything needed...
750 * Allocate memory for the job...
754 temp
= malloc(sizeof(cups_job_t
));
756 temp
= realloc(*jobs
, sizeof(cups_job_t
) * (n
+ 1));
764 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
766 cupsFreeJobs(n
, *jobs
);
779 * Copy the data over...
782 temp
->dest
= _cupsStrAlloc(dest
);
783 temp
->user
= _cupsStrAlloc(user
);
784 temp
->format
= _cupsStrAlloc(format
);
785 temp
->title
= _cupsStrAlloc(title
);
787 temp
->priority
= priority
;
790 temp
->completed_time
= completed_time
;
791 temp
->creation_time
= creation_time
;
792 temp
->processing_time
= processing_time
;
801 if (n
== 0 && cg
->last_error
>= IPP_BAD_REQUEST
)
809 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
811 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
815 const char * /* O - Filename for PPD file */
816 cupsGetPPD(const char *name
) /* I - Printer name */
818 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
819 time_t modtime
= 0; /* Modification time */
823 * Return the PPD file...
826 cg
->ppd_filename
[0] = '\0';
828 if (cupsGetPPD3(CUPS_HTTP_DEFAULT
, name
, &modtime
, cg
->ppd_filename
,
829 sizeof(cg
->ppd_filename
)) == HTTP_OK
)
830 return (cg
->ppd_filename
);
837 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
839 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
842 * @since CUPS 1.1.21@
845 const char * /* O - Filename for PPD file */
846 cupsGetPPD2(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
847 const char *name
) /* I - Printer name */
849 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
850 time_t modtime
= 0; /* Modification time */
853 cg
->ppd_filename
[0] = '\0';
855 if (cupsGetPPD3(http
, name
, &modtime
, cg
->ppd_filename
,
856 sizeof(cg
->ppd_filename
)) == HTTP_OK
)
857 return (cg
->ppd_filename
);
864 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
865 * server if it has changed.
867 * The "modtime" parameter contains the modification time of any
868 * locally-cached content and is updated with the time from the PPD file on
871 * The "buffer" parameter contains the local PPD filename. If it contains
872 * the empty string, a new temporary file is created, otherwise the existing
873 * file will be overwritten as needed.
875 * On success, @code HTTP_OK@ is returned for a new PPD file and
876 * @code HTTP_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other
877 * status is an error.
879 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
883 http_status_t
/* O - HTTP status */
884 cupsGetPPD3(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
885 const char *name
, /* I - Printer name */
886 time_t *modtime
, /* IO - Modification time */
887 char *buffer
, /* I - Filename buffer */
888 size_t bufsize
) /* I - Size of filename buffer */
890 int http_port
; /* Port number */
891 char http_hostname
[HTTP_MAX_HOST
];
892 /* Hostname associated with connection */
893 http_t
*http2
; /* Alternate HTTP connection */
894 int fd
; /* PPD file */
895 char localhost
[HTTP_MAX_URI
],/* Local hostname */
896 hostname
[HTTP_MAX_URI
], /* Hostname */
897 resource
[HTTP_MAX_URI
]; /* Resource name */
898 int port
; /* Port number */
899 http_status_t status
; /* HTTP status from server */
900 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
904 * Range check input...
907 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
908 "bufsize=%d)\n", http
, name
? name
: "(null)", modtime
,
909 modtime
? *modtime
: 0, buffer
, (int)bufsize
));
913 _cupsSetError(IPP_INTERNAL_ERROR
, "No printer name!");
914 return (HTTP_NOT_ACCEPTABLE
);
919 _cupsSetError(IPP_INTERNAL_ERROR
, "No modification time!");
920 return (HTTP_NOT_ACCEPTABLE
);
923 if (!buffer
|| bufsize
<= 1)
925 _cupsSetError(IPP_INTERNAL_ERROR
, "Bad filename buffer!");
926 return (HTTP_NOT_ACCEPTABLE
);
930 * Try finding a printer URI for this printer...
934 http
= _cupsConnect();
936 if (!cups_get_printer_uri(http
, name
, hostname
, sizeof(hostname
), &port
,
937 resource
, sizeof(resource
), 0))
938 return (HTTP_NOT_FOUND
);
940 DEBUG_printf(("Printer hostname=\"%s\", port=%d\n", hostname
, port
));
943 * Remap local hostname to localhost...
946 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
948 DEBUG_printf(("Local hostname=\"%s\"\n", localhost
));
950 if (!strcasecmp(localhost
, hostname
))
951 strcpy(hostname
, "localhost");
954 * Get the hostname and port number we are connected to...
957 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
960 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
961 http_port
= ntohs(http
->hostaddr
->ipv6
.sin6_port
);
963 #endif /* AF_INET6 */
964 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
965 http_port
= ntohs(http
->hostaddr
->ipv4
.sin_port
);
967 http_port
= ippPort();
969 DEBUG_printf(("Connection hostname=\"%s\", port=%d\n", http_hostname
,
973 * Reconnect to the correct server as needed...
976 if (!strcasecmp(http_hostname
, hostname
) && port
== http_port
)
978 else if ((http2
= httpConnectEncrypt(hostname
, port
,
979 cupsEncryption())) == NULL
)
981 DEBUG_puts("Unable to connect to server!");
983 return (HTTP_SERVICE_UNAVAILABLE
);
991 fd
= open(buffer
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
993 fd
= cupsTempFd(buffer
, bufsize
);
998 * Can't open file; close the server connection and return NULL...
1001 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1006 return (HTTP_SERVER_ERROR
);
1010 * And send a request to the HTTP server...
1013 strlcat(resource
, ".ppd", sizeof(resource
));
1016 httpSetField(http2
, HTTP_FIELD_IF_MODIFIED_SINCE
,
1017 httpGetDateString(*modtime
));
1019 status
= cupsGetFd(http2
, resource
, fd
);
1024 * See if we actually got the file or an error...
1027 if (status
== HTTP_OK
)
1028 *modtime
= httpGetDateTime(httpGetField(http2
, HTTP_FIELD_DATE
));
1029 else if (status
!= HTTP_NOT_MODIFIED
)
1033 case HTTP_NOT_FOUND
:
1034 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
));
1037 case HTTP_UNAUTHORIZED
:
1038 _cupsSetError(IPP_NOT_AUTHORIZED
, httpStatus(status
));
1042 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
1044 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
));
1048 unlink(cg
->ppd_filename
);
1055 * Return the PPD file...
1063 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1065 * This function is deprecated - use @link cupsGetDests@ instead.
1070 int /* O - Number of printers */
1071 cupsGetPrinters(char ***printers
) /* O - Printers */
1073 int n
; /* Number of printers */
1074 ipp_t
*request
, /* IPP Request */
1075 *response
; /* IPP Response */
1076 ipp_attribute_t
*attr
; /* Current attribute */
1077 char **temp
; /* Temporary pointer */
1078 http_t
*http
; /* Connection to server */
1082 * Range check input...
1087 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1095 * Try to connect to the server...
1098 if ((http
= _cupsConnect()) == NULL
)
1102 * Build a CUPS_GET_PRINTERS request, which requires the following
1105 * attributes-charset
1106 * attributes-natural-language
1107 * requested-attributes
1110 request
= ippNewRequest(CUPS_GET_PRINTERS
);
1112 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1113 "requested-attributes", NULL
, "printer-name");
1115 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1118 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
1119 "printer-type-mask", CUPS_PRINTER_CLASS
);
1122 * Do the request and get back a response...
1127 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1129 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1130 if (attr
->name
!= NULL
&&
1131 strcasecmp(attr
->name
, "printer-name") == 0 &&
1132 attr
->value_tag
== IPP_TAG_NAME
)
1135 temp
= malloc(sizeof(char *));
1137 temp
= realloc(*printers
, sizeof(char *) * (n
+ 1));
1142 * Ran out of memory!
1148 free((*printers
)[n
]);
1152 ippDelete(response
);
1157 temp
[n
] = strdup(attr
->values
[0].string
.text
);
1161 ippDelete(response
);
1169 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1171 * This function returns the named PPD file from the server. The
1172 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
1175 * You must remove (unlink) the PPD file when you are finished with
1176 * it. The PPD filename is stored in a static location that will be
1177 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1178 * or @link cupsGetServerPPD@.
1183 char * /* O - Name of PPD file or @code NULL@ on error */
1184 cupsGetServerPPD(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
1185 const char *name
) /* I - Name of PPD file ("ppd-name") */
1187 int fd
; /* PPD file descriptor */
1188 ipp_t
*request
; /* IPP request */
1189 _cups_globals_t
*cg
= _cupsGlobals();
1190 /* Pointer to library globals */
1194 * Range check input...
1199 _cupsSetError(IPP_INTERNAL_ERROR
, "No PPD name!");
1205 if ((http
= _cupsConnect()) == NULL
)
1209 * Get a temp file...
1212 if ((fd
= cupsTempFd(cg
->ppd_filename
, sizeof(cg
->ppd_filename
))) < 0)
1215 * Can't open file; close the server connection and return NULL...
1218 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
));
1224 * Get the PPD file...
1227 request
= ippNewRequest(CUPS_GET_PPD
);
1228 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "ppd-name", NULL
,
1231 ippDelete(cupsDoIORequest(http
, request
, "/", -1, fd
));
1235 if (cupsLastError() != IPP_OK
)
1237 unlink(cg
->ppd_filename
);
1241 return (cg
->ppd_filename
);
1246 * 'cupsLastError()' - Return the last IPP status code.
1249 ipp_status_t
/* O - IPP status code from last request */
1252 return (_cupsGlobals()->last_error
);
1257 * 'cupsLastErrorString()' - Return the last IPP status-message.
1262 const char * /* O - status-message text from last request */
1263 cupsLastErrorString(void)
1265 return (_cupsGlobals()->last_status_message
);
1270 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1273 int /* O - Job ID or 0 on error */
1274 cupsPrintFile(const char *name
, /* I - Printer or class name */
1275 const char *filename
, /* I - File to print */
1276 const char *title
, /* I - Title of job */
1277 int num_options
,/* I - Number of options */
1278 cups_option_t
*options
) /* I - Options */
1280 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
1281 "title=\"%s\", num_options=%d, options=%p)\n",
1282 name
, filename
, title
, num_options
, options
));
1284 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, 1, &filename
, title
,
1285 num_options
, options
));
1290 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1293 * @since CUPS 1.1.21@
1296 int /* O - Job ID or 0 on error */
1298 http_t
*http
, /* I - HTTP connection */
1299 const char *name
, /* I - Printer or class name */
1300 const char *filename
, /* I - File to print */
1301 const char *title
, /* I - Title of job */
1302 int num_options
, /* I - Number of options */
1303 cups_option_t
*options
) /* I - Options */
1305 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
1306 "title=\"%s\", num_options=%d, options=%p)\n",
1307 http
, name
, filename
, title
, num_options
, options
));
1309 return (cupsPrintFiles2(http
, name
, 1, &filename
, title
, num_options
,
1315 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1319 int /* O - Job ID or 0 on error */
1321 const char *name
, /* I - Printer or class name */
1322 int num_files
, /* I - Number of files */
1323 const char **files
, /* I - File(s) to print */
1324 const char *title
, /* I - Title of job */
1325 int num_options
, /* I - Number of options */
1326 cups_option_t
*options
) /* I - Options */
1328 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
1329 "files=%p, title=\"%s\", num_options=%d, options=%p)\n",
1330 name
, num_files
, files
, title
, num_options
, options
));
1334 * Print the file(s)...
1337 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT
, name
, num_files
, files
, title
,
1338 num_options
, options
));
1343 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1346 * @since CUPS 1.1.21@
1349 int /* O - Job ID or 0 on error */
1351 http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
1352 const char *name
, /* I - Printer or class name */
1353 int num_files
, /* I - Number of files */
1354 const char **files
, /* I - File(s) to print */
1355 const char *title
, /* I - Title of job */
1356 int num_options
, /* I - Number of options */
1357 cups_option_t
*options
) /* I - Options */
1359 int i
; /* Looping var */
1360 int job_id
; /* New job ID */
1361 const char *docname
; /* Basename of current filename */
1362 const char *format
; /* Document format */
1363 cups_file_t
*fp
; /* Current file */
1364 char buffer
[8192]; /* Copy buffer */
1365 ssize_t bytes
; /* Bytes in buffer */
1366 http_status_t status
; /* Status of write */
1369 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
1370 "files=%p, title=\"%s\", num_options=%d, options=%p)\n",
1371 http
, name
, num_files
, files
, title
, num_options
, options
));
1374 * Range check input...
1377 if (!name
|| num_files
< 1 || !files
)
1379 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1385 * Create the print job...
1388 if ((job_id
= cupsCreateJob(http
, name
, title
, num_options
, options
)) == 0)
1392 * Send each of the files...
1395 if (cupsGetOption("raw", num_options
, options
))
1396 format
= CUPS_FORMAT_RAW
;
1397 else if ((format
= cupsGetOption("document-format", num_options
,
1399 format
= CUPS_FORMAT_AUTO
;
1401 for (i
= 0; i
< num_files
; i
++)
1404 * Start the next file...
1407 if ((docname
= strrchr(files
[i
], '/')) != NULL
)
1412 if ((fp
= cupsFileOpen(files
[i
], "rb")) == NULL
)
1415 * Unable to open print file, cancel the job and return...
1418 cupsCancelJob2(http
, name
, job_id
, 0);
1422 status
= cupsStartDocument(http
, name
, job_id
, docname
, format
,
1423 i
== (num_files
- 1));
1425 while (status
== HTTP_CONTINUE
&&
1426 (bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1427 status
= cupsWriteRequestData(http
, buffer
, bytes
);
1431 if (status
!= HTTP_CONTINUE
|| cupsFinishDocument(http
, name
) != IPP_OK
)
1434 * Unable to queue, cancel the job and return...
1437 cupsCancelJob2(http
, name
, job_id
, 0);
1447 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1449 * Use @link cupsWriteRequestData@ to write data for the document and
1450 * @link cupsFinishDocument@ to finish the document and get the submission status.
1452 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1453 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1454 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1455 * any supported MIME type string can be supplied.
1460 http_status_t
/* O - HTTP status of request */
1462 http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
1463 const char *name
, /* I - Printer or class name */
1464 int job_id
, /* I - Job ID from @link cupsCreateJob@ */
1465 const char *docname
, /* I - Name of document */
1466 const char *format
, /* I - MIME type or @code CUPS_FORMAT_foo@ */
1467 int last_document
) /* I - 1 for last document in job, 0 otherwise */
1469 char resource
[1024], /* Resource for destinatio */
1470 printer_uri
[1024]; /* Printer URI */
1471 ipp_t
*request
; /* Send-Document request */
1472 http_status_t status
; /* HTTP status */
1476 * Create a Send-Document request...
1479 if ((request
= ippNewRequest(IPP_SEND_DOCUMENT
)) == NULL
)
1481 _cupsSetError(IPP_INTERNAL_ERROR
, NULL
);
1485 httpAssembleURIf(HTTP_URI_CODING_ALL
, printer_uri
, sizeof(printer_uri
), "ipp",
1486 NULL
, "localhost", ippPort(), "/printers/%s", name
);
1487 snprintf(resource
, sizeof(resource
), "/printers/%s", name
);
1489 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1491 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", job_id
);
1492 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
1495 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
1498 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
1499 "document-format", NULL
, format
);
1500 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", last_document
);
1503 * Send and delete the request, then return the status...
1506 status
= cupsSendRequest(http
, request
, resource
, CUPS_LENGTH_VARIABLE
);
1515 * '_cupsConnect()' - Get the default server connection...
1518 http_t
* /* O - HTTP connection */
1521 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1525 * See if we are connected to the same server...
1530 int port
; /* Port for connection */
1534 * Get the port associated with the current connection...
1538 if (cg
->http
->hostaddr
->addr
.sa_family
== AF_INET6
)
1539 port
= ntohs(cg
->http
->hostaddr
->ipv6
.sin6_port
);
1541 #endif /* AF_INET6 */
1542 if (cg
->http
->hostaddr
->addr
.sa_family
== AF_INET
)
1543 port
= ntohs(cg
->http
->hostaddr
->ipv4
.sin_port
);
1545 port
= cg
->ipp_port
;
1548 * Compare the connection hostname, port, and encryption settings to
1549 * the cached defaults; these were initialized the first time we
1553 if (strcmp(cg
->http
->hostname
, cg
->server
) || cg
->ipp_port
!= port
||
1554 (cg
->http
->encryption
!= cg
->encryption
&&
1555 cg
->http
->encryption
== HTTP_ENCRYPT_NEVER
))
1558 * Need to close the current connection because something has changed...
1561 httpClose(cg
->http
);
1567 * (Re)connect as needed...
1572 if ((cg
->http
= httpConnectEncrypt(cupsServer(), ippPort(),
1573 cupsEncryption())) == NULL
)
1574 _cupsSetError(IPP_SERVICE_UNAVAILABLE
,
1575 errno
? strerror(errno
) : "Unable to connect to host.");
1579 * Return the cached connection...
1587 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the first printer in a class.
1590 static int /* O - 1 on success, 0 on failure */
1591 cups_get_printer_uri(
1592 http_t
*http
, /* I - HTTP connection */
1593 const char *name
, /* I - Name of printer or class */
1594 char *host
, /* I - Hostname buffer */
1595 int hostsize
, /* I - Size of hostname buffer */
1596 int *port
, /* O - Port number */
1597 char *resource
, /* I - Resource buffer */
1598 int resourcesize
, /* I - Size of resource buffer */
1599 int depth
) /* I - Depth of query */
1601 int i
; /* Looping var */
1602 int http_port
; /* Port number */
1603 http_t
*http2
; /* Alternate HTTP connection */
1604 ipp_t
*request
, /* IPP request */
1605 *response
; /* IPP response */
1606 ipp_attribute_t
*attr
; /* Current attribute */
1607 char uri
[HTTP_MAX_URI
], /* printer-uri attribute */
1608 scheme
[HTTP_MAX_URI
], /* Scheme name */
1609 username
[HTTP_MAX_URI
], /* Username:password */
1610 classname
[255], /* Temporary class name */
1611 http_hostname
[HTTP_MAX_HOST
];
1612 /* Hostname associated with connection */
1613 static const char * const requested_attrs
[] =
1614 { /* Requested attributes */
1615 "printer-uri-supported",
1621 DEBUG_printf(("cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
1622 "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)\n",
1623 http
, name
? name
: "(null)", host
, hostsize
,
1624 resource
, resourcesize
, depth
));
1627 * Setup the printer URI...
1630 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1631 "localhost", 0, "/printers/%s", name
) != HTTP_URI_OK
)
1633 _cupsSetError(IPP_INTERNAL_ERROR
, "Unable to create printer-uri!");
1641 DEBUG_printf(("cups_get_printer_uri: printer-uri=\"%s\"\n", uri
));
1644 * Get the hostname and port number we are connected to...
1647 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
1650 if (http
->hostaddr
->addr
.sa_family
== AF_INET6
)
1651 http_port
= ntohs(http
->hostaddr
->ipv6
.sin6_port
);
1653 #endif /* AF_INET6 */
1654 if (http
->hostaddr
->addr
.sa_family
== AF_INET
)
1655 http_port
= ntohs(http
->hostaddr
->ipv4
.sin_port
);
1657 http_port
= ippPort();
1660 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1663 * attributes-charset
1664 * attributes-natural-language
1666 * requested-attributes
1669 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
1671 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1674 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1675 "requested-attributes",
1676 sizeof(requested_attrs
) / sizeof(requested_attrs
[0]),
1677 NULL
, requested_attrs
);
1680 * Do the request and get back a response...
1683 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1685 if ((attr
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
)) != NULL
)
1688 * Get the first actual printer name in the class...
1691 for (i
= 0; i
< attr
->num_values
; i
++)
1693 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
,
1694 scheme
, sizeof(scheme
), username
, sizeof(username
),
1695 host
, hostsize
, port
, resource
, resourcesize
);
1696 if (!strncmp(resource
, "/printers/", 10))
1702 ippDelete(response
);
1709 * No printers in this class - try recursively looking for a printer,
1710 * but not more than 3 levels deep...
1715 for (i
= 0; i
< attr
->num_values
; i
++)
1717 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
,
1718 scheme
, sizeof(scheme
), username
, sizeof(username
),
1719 host
, hostsize
, port
, resource
, resourcesize
);
1720 if (!strncmp(resource
, "/classes/", 9))
1723 * Found a class! Connect to the right server...
1726 if (!strcasecmp(http_hostname
, host
) && *port
== http_port
)
1728 else if ((http2
= httpConnectEncrypt(host
, *port
,
1729 cupsEncryption())) == NULL
)
1731 DEBUG_puts("Unable to connect to server!");
1737 * Look up printers on that server...
1740 strlcpy(classname
, resource
+ 9, sizeof(classname
));
1742 cups_get_printer_uri(http2
, classname
, host
, hostsize
, port
,
1743 resource
, resourcesize
, depth
+ 1);
1746 * Close the connection as needed...
1758 else if ((attr
= ippFindAttribute(response
, "printer-uri-supported",
1759 IPP_TAG_URI
)) != NULL
)
1761 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[0].string
.text
,
1762 scheme
, sizeof(scheme
), username
, sizeof(username
),
1763 host
, hostsize
, port
, resource
, resourcesize
);
1764 ippDelete(response
);
1769 ippDelete(response
);
1772 if (cupsLastError() != IPP_NOT_FOUND
)
1773 _cupsSetError(IPP_INTERNAL_ERROR
, "No printer-uri found!");
1783 * End of "$Id: util.c 7014 2007-10-10 21:57:43Z mike $".