2 * "$Id: util.c 11173 2013-07-23 12:31:34Z msweet $"
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 */
123 * Range check input...
126 if (job_id < -1 || (!name && job_id == 0))
128 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
133 * Connect to the default server as needed...
137 if ((http = _cupsConnect()) == NULL)
138 return (IPP_SERVICE_UNAVAILABLE);
141 * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following
145 * attributes-natural-language
146 * job-uri or printer-uri + job-id
147 * requesting-user-name
148 * [purge-job] or [purge-jobs]
151 request = ippNewRequest(job_id < 0 ? IPP_PURGE_JOBS : IPP_CANCEL_JOB);
155 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
156 "localhost", ippPort(), "/printers/%s", name);
158 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
160 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
165 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
167 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
170 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
173 if (purge && job_id >= 0)
174 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1);
175 else if (!purge && job_id < 0)
176 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0);
182 ippDelete(cupsDoRequest(http, request, "/jobs/"));
184 return (cupsLastError());
189 * 'cupsCreateJob()' - Create an empty job for streaming.
191 * Use this function when you want to stream print data using the
192 * @link cupsStartDocument@, @link cupsWriteRequestData@, and
193 * @link cupsFinishDocument@ functions. If you have one or more files to
194 * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function
197 * @since CUPS 1.4/OS X 10.6@
200 int /* O - Job ID or 0 on error */
202 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
203 const char *name, /* I - Destination name */
204 const char *title, /* I - Title of job */
205 int num_options, /* I - Number of options */
206 cups_option_t *options) /* I - Options */
208 char printer_uri[1024], /* Printer URI */
209 resource[1024]; /* Printer resource */
210 ipp_t *request, /* Create-Job request */
211 *response; /* Create-Job response */
212 ipp_attribute_t *attr; /* job-id attribute */
213 int job_id = 0; /* job-id value */
216 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", "
217 "num_options=%d, options=%p)",
218 http, name, title, num_options, options));
221 * Range check input...
226 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
231 * Build a Create-Job request...
234 if ((request = ippNewRequest(IPP_CREATE_JOB)) == NULL)
236 _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0);
240 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
241 NULL, "localhost", ippPort(), "/printers/%s", name);
242 snprintf(resource, sizeof(resource), "/printers/%s", name);
244 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
246 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
249 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
251 cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB);
252 cupsEncodeOptions2(request, num_options, options, IPP_TAG_SUBSCRIPTION);
255 * Send the request and get the job-id...
258 response = cupsDoRequest(http, request, resource);
260 if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL)
261 job_id = attr->values[0].integer;
274 * 'cupsFinishDocument()' - Finish sending a document.
276 * The document must have been started using @link cupsStartDocument@.
278 * @since CUPS 1.4/OS X 10.6@
281 ipp_status_t /* O - Status of document submission */
282 cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
283 const char *name) /* I - Destination name */
285 char resource[1024]; /* Printer resource */
288 snprintf(resource, sizeof(resource), "/printers/%s", name);
290 ippDelete(cupsGetResponse(http, resource));
292 return (cupsLastError());
297 * 'cupsFreeJobs()' - Free memory used by job data.
301 cupsFreeJobs(int num_jobs, /* I - Number of jobs */
302 cups_job_t *jobs) /* I - Jobs */
304 int i; /* Looping var */
305 cups_job_t *job; /* Current job */
308 if (num_jobs <= 0 || !jobs)
311 for (i = num_jobs, job = jobs; i > 0; i --, job ++)
313 _cupsStrFree(job->dest);
314 _cupsStrFree(job->user);
315 _cupsStrFree(job->format);
316 _cupsStrFree(job->title);
324 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
326 * This function is deprecated - use @link cupsGetDests@ instead.
331 int /* O - Number of classes */
332 cupsGetClasses(char ***classes) /* O - Classes */
334 int n; /* Number of classes */
335 ipp_t *request, /* IPP Request */
336 *response; /* IPP Response */
337 ipp_attribute_t *attr; /* Current attribute */
338 char **temp; /* Temporary pointer */
339 http_t *http; /* Connection to server */
344 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
351 if ((http = _cupsConnect()) == NULL)
355 * Build a CUPS_GET_CLASSES request, which requires the following
359 * attributes-natural-language
360 * requested-attributes
363 request = ippNewRequest(CUPS_GET_CLASSES);
365 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
366 "requested-attributes", NULL, "printer-name");
369 * Do the request and get back a response...
374 if ((response = cupsDoRequest(http, request, "/")) != NULL)
376 for (attr = response->attrs; attr != NULL; attr = attr->next)
377 if (attr->name != NULL &&
378 _cups_strcasecmp(attr->name, "printer-name") == 0 &&
379 attr->value_tag == IPP_TAG_NAME)
382 temp = malloc(sizeof(char *));
384 temp = realloc(*classes, sizeof(char *) * (n + 1));
404 temp[n] = strdup(attr->values[0].string.text);
416 * 'cupsGetDefault()' - Get the default printer or class for the default server.
418 * This function returns the default printer or class as defined by
419 * the LPDEST or PRINTER environment variables. If these environment
420 * variables are not set, the server default destination is returned.
421 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
422 * functions to get the user-defined default printer, as this function does
423 * not support the lpoptions-defined default printer.
426 const char * /* O - Default printer or @code NULL@ */
430 * Return the default printer...
433 return (cupsGetDefault2(CUPS_HTTP_DEFAULT));
438 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
440 * This function returns the default printer or class as defined by
441 * the LPDEST or PRINTER environment variables. If these environment
442 * variables are not set, the server default destination is returned.
443 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
444 * functions to get the user-defined default printer, as this function does
445 * not support the lpoptions-defined default printer.
447 * @since CUPS 1.1.21/OS X 10.4@
450 const char * /* O - Default printer or @code NULL@ */
451 cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
453 ipp_t *request, /* IPP Request */
454 *response; /* IPP Response */
455 ipp_attribute_t *attr; /* Current attribute */
456 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
460 * See if we have a user default printer set...
463 if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer)))
464 return (cg->def_printer);
467 * Connect to the server as needed...
471 if ((http = _cupsConnect()) == NULL)
475 * Build a CUPS_GET_DEFAULT request, which requires the following
479 * attributes-natural-language
482 request = ippNewRequest(CUPS_GET_DEFAULT);
485 * Do the request and get back a response...
488 if ((response = cupsDoRequest(http, request, "/")) != NULL)
490 if ((attr = ippFindAttribute(response, "printer-name",
491 IPP_TAG_NAME)) != NULL)
493 strlcpy(cg->def_printer, attr->values[0].string.text,
494 sizeof(cg->def_printer));
496 return (cg->def_printer);
507 * 'cupsGetJobs()' - Get the jobs from the default server.
509 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
510 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
511 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
512 * jobs that are stopped, canceled, aborted, or completed.
515 int /* O - Number of jobs */
516 cupsGetJobs(cups_job_t **jobs, /* O - Job data */
517 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
518 int myjobs, /* I - 0 = all users, 1 = mine */
519 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
525 return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs));
531 * 'cupsGetJobs2()' - Get the jobs from the specified server.
533 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
534 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
535 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
536 * jobs that are stopped, canceled, aborted, or completed.
538 * @since CUPS 1.1.21/OS X 10.4@
541 int /* O - Number of jobs */
542 cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
543 cups_job_t **jobs, /* O - Job data */
544 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
545 int myjobs, /* I - 0 = all users, 1 = mine */
546 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
548 int n; /* Number of jobs */
549 ipp_t *request, /* IPP Request */
550 *response; /* IPP Response */
551 ipp_attribute_t *attr; /* Current attribute */
552 cups_job_t *temp; /* Temporary pointer */
554 priority, /* job-priority */
555 size; /* job-k-octets */
556 ipp_jstate_t state; /* job-state */
557 time_t completed_time, /* time-at-completed */
558 creation_time, /* time-at-creation */
559 processing_time; /* time-at-processing */
560 const char *dest, /* job-printer-uri */
561 *format, /* document-format */
562 *title, /* job-name */
563 *user; /* job-originating-user-name */
564 char uri[HTTP_MAX_URI]; /* URI for jobs */
565 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
566 static const char * const attrs[] = /* Requested attributes */
572 "job-originating-user-name",
583 * Range check input...
588 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
594 * Get the right URI...
599 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
600 "localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
602 _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri"), 1);
608 strcpy(uri, "ipp://localhost/");
611 if ((http = _cupsConnect()) == NULL)
615 * Build an IPP_GET_JOBS request, which requires the following
619 * attributes-natural-language
621 * requesting-user-name
624 * requested-attributes
627 request = ippNewRequest(IPP_GET_JOBS);
629 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
630 "printer-uri", NULL, uri);
632 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
633 "requesting-user-name", NULL, cupsUser());
636 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
638 if (whichjobs == CUPS_WHICHJOBS_COMPLETED)
639 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
640 "which-jobs", NULL, "completed");
641 else if (whichjobs == CUPS_WHICHJOBS_ALL)
642 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
643 "which-jobs", NULL, "all");
645 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
646 "requested-attributes", sizeof(attrs) / sizeof(attrs[0]),
650 * Do the request and get back a response...
656 if ((response = cupsDoRequest(http, request, "/")) != NULL)
658 for (attr = response->attrs; attr; attr = attr->next)
661 * Skip leading attributes until we hit a job...
664 while (attr && attr->group_tag != IPP_TAG_JOB)
671 * Pull the needed attributes from this job...
677 state = IPP_JOB_PENDING;
680 format = "application/octet-stream";
686 while (attr && attr->group_tag == IPP_TAG_JOB)
688 if (!strcmp(attr->name, "job-id") &&
689 attr->value_tag == IPP_TAG_INTEGER)
690 id = attr->values[0].integer;
691 else if (!strcmp(attr->name, "job-state") &&
692 attr->value_tag == IPP_TAG_ENUM)
693 state = (ipp_jstate_t)attr->values[0].integer;
694 else if (!strcmp(attr->name, "job-priority") &&
695 attr->value_tag == IPP_TAG_INTEGER)
696 priority = attr->values[0].integer;
697 else if (!strcmp(attr->name, "job-k-octets") &&
698 attr->value_tag == IPP_TAG_INTEGER)
699 size = attr->values[0].integer;
700 else if (!strcmp(attr->name, "time-at-completed") &&
701 attr->value_tag == IPP_TAG_INTEGER)
702 completed_time = attr->values[0].integer;
703 else if (!strcmp(attr->name, "time-at-creation") &&
704 attr->value_tag == IPP_TAG_INTEGER)
705 creation_time = attr->values[0].integer;
706 else if (!strcmp(attr->name, "time-at-processing") &&
707 attr->value_tag == IPP_TAG_INTEGER)
708 processing_time = attr->values[0].integer;
709 else if (!strcmp(attr->name, "job-printer-uri") &&
710 attr->value_tag == IPP_TAG_URI)
712 if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
715 else if (!strcmp(attr->name, "job-originating-user-name") &&
716 attr->value_tag == IPP_TAG_NAME)
717 user = attr->values[0].string.text;
718 else if (!strcmp(attr->name, "document-format") &&
719 attr->value_tag == IPP_TAG_MIMETYPE)
720 format = attr->values[0].string.text;
721 else if (!strcmp(attr->name, "job-name") &&
722 (attr->value_tag == IPP_TAG_TEXT ||
723 attr->value_tag == IPP_TAG_NAME))
724 title = attr->values[0].string.text;
730 * See if we have everything needed...
742 * Allocate memory for the job...
746 temp = malloc(sizeof(cups_job_t));
748 temp = realloc(*jobs, sizeof(cups_job_t) * (n + 1));
756 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
758 cupsFreeJobs(n, *jobs);
771 * Copy the data over...
774 temp->dest = _cupsStrAlloc(dest);
775 temp->user = _cupsStrAlloc(user);
776 temp->format = _cupsStrAlloc(format);
777 temp->title = _cupsStrAlloc(title);
779 temp->priority = priority;
782 temp->completed_time = completed_time;
783 temp->creation_time = creation_time;
784 temp->processing_time = processing_time;
793 if (n == 0 && cg->last_error >= IPP_BAD_REQUEST)
801 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
803 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
806 * The returned filename is stored in a static buffer and is overwritten with
807 * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. The caller "owns" the
808 * file that is created and must @code unlink@ the returned filename.
811 const char * /* O - Filename for PPD file */
812 cupsGetPPD(const char *name) /* I - Destination name */
814 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
815 time_t modtime = 0; /* Modification time */
819 * Return the PPD file...
822 cg->ppd_filename[0] = '\0';
824 if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, cg->ppd_filename,
825 sizeof(cg->ppd_filename)) == HTTP_OK)
826 return (cg->ppd_filename);
833 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
835 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
838 * The returned filename is stored in a static buffer and is overwritten with
839 * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. The caller "owns" the
840 * file that is created and must @code unlink@ the returned filename.
842 * @since CUPS 1.1.21/OS X 10.4@
845 const char * /* O - Filename for PPD file */
846 cupsGetPPD2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
847 const char *name) /* I - Destination 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. The caller "owns" the file that is
874 * created and must @code unlink@ the returned filename.
876 * On success, @code HTTP_OK@ is returned for a new PPD file and
877 * @code HTTP_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other
878 * status is an error.
880 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
883 * @since CUPS 1.4/OS X 10.6@
886 http_status_t /* O - HTTP status */
887 cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
888 const char *name, /* I - Destination name */
889 time_t *modtime, /* IO - Modification time */
890 char *buffer, /* I - Filename buffer */
891 size_t bufsize) /* I - Size of filename buffer */
893 int http_port; /* Port number */
894 char http_hostname[HTTP_MAX_HOST];
895 /* Hostname associated with connection */
896 http_t *http2; /* Alternate HTTP connection */
897 int fd; /* PPD file */
898 char localhost[HTTP_MAX_URI],/* Local hostname */
899 hostname[HTTP_MAX_URI], /* Hostname */
900 resource[HTTP_MAX_URI]; /* Resource name */
901 int port; /* Port number */
902 http_status_t status; /* HTTP status from server */
903 char tempfile[1024] = ""; /* Temporary filename */
904 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
908 * Range check input...
911 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
912 "bufsize=%d)", http, name, modtime,
913 modtime ? (int)*modtime : 0, buffer, (int)bufsize));
917 _cupsSetError(IPP_INTERNAL_ERROR, _("No printer name"), 1);
918 return (HTTP_NOT_ACCEPTABLE);
923 _cupsSetError(IPP_INTERNAL_ERROR, _("No modification time"), 1);
924 return (HTTP_NOT_ACCEPTABLE);
927 if (!buffer || bufsize <= 1)
929 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad filename buffer"), 1);
930 return (HTTP_NOT_ACCEPTABLE);
935 * See if the PPD file is available locally...
939 httpGetHostname(http, hostname, sizeof(hostname));
942 strlcpy(hostname, cupsServer(), sizeof(hostname));
943 if (hostname[0] == '/')
944 strlcpy(hostname, "localhost", sizeof(hostname));
947 if (!_cups_strcasecmp(hostname, "localhost"))
949 char ppdname[1024]; /* PPD filename */
950 struct stat ppdinfo; /* PPD file information */
953 snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd", cg->cups_serverroot,
955 if (!stat(ppdname, &ppdinfo))
958 * OK, the file exists, use it!
965 if (symlink(ppdname, buffer) && errno != EEXIST)
967 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
969 return (HTTP_SERVER_ERROR);
974 int tries; /* Number of tries */
975 const char *tmpdir; /* TMPDIR environment variable */
976 struct timeval curtime; /* Current time */
979 * Previously we put root temporary files in the default CUPS temporary
980 * directory under /var/spool/cups. However, since the scheduler cleans
981 * out temporary files there and runs independently of the user apps, we
982 * don't want to use it unless specifically told to by cupsd.
985 if ((tmpdir = getenv("TMPDIR")) == NULL)
987 tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */
990 # endif /* __APPLE__ */
993 * Make the temporary name using the specified directory...
1001 * Get the current time of day...
1004 gettimeofday(&curtime, NULL);
1007 * Format a string using the hex time values...
1010 snprintf(buffer, bufsize, "%s/%08lx%05lx", tmpdir,
1011 (unsigned long)curtime.tv_sec,
1012 (unsigned long)curtime.tv_usec);
1015 * Try to make a symlink...
1018 if (!symlink(ppdname, buffer))
1023 while (tries < 1000);
1027 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
1029 return (HTTP_SERVER_ERROR);
1033 if (*modtime >= ppdinfo.st_mtime)
1034 return (HTTP_NOT_MODIFIED);
1037 *modtime = ppdinfo.st_mtime;
1045 * Try finding a printer URI for this printer...
1049 if ((http = _cupsConnect()) == NULL)
1050 return (HTTP_SERVICE_UNAVAILABLE);
1052 if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port,
1053 resource, sizeof(resource), 0))
1054 return (HTTP_NOT_FOUND);
1056 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname,
1060 * Remap local hostname to localhost...
1063 httpGetHostname(NULL, localhost, sizeof(localhost));
1065 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost));
1067 if (!_cups_strcasecmp(localhost, hostname))
1068 strcpy(hostname, "localhost");
1071 * Get the hostname and port number we are connected to...
1074 httpGetHostname(http, http_hostname, sizeof(http_hostname));
1075 http_port = _httpAddrPort(http->hostaddr);
1077 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
1078 http_hostname, http_port));
1081 * Reconnect to the correct server as needed...
1084 if (!_cups_strcasecmp(http_hostname, hostname) && port == http_port)
1086 else if ((http2 = httpConnectEncrypt(hostname, port,
1087 cupsEncryption())) == NULL)
1089 DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
1091 return (HTTP_SERVICE_UNAVAILABLE);
1095 * Get a temp file...
1099 fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600);
1101 fd = cupsTempFd(tempfile, sizeof(tempfile));
1106 * Can't open file; close the server connection and return NULL...
1109 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
1114 return (HTTP_SERVER_ERROR);
1118 * And send a request to the HTTP server...
1121 strlcat(resource, ".ppd", sizeof(resource));
1124 httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE,
1125 httpGetDateString(*modtime));
1127 status = cupsGetFd(http2, resource, fd);
1132 * See if we actually got the file or an error...
1135 if (status == HTTP_OK)
1137 *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE));
1140 strlcpy(buffer, tempfile, bufsize);
1142 else if (status != HTTP_NOT_MODIFIED)
1144 _cupsSetHTTPError(status);
1148 else if (tempfile[0])
1151 else if (tempfile[0])
1158 * Return the PPD file...
1161 DEBUG_printf(("1cupsGetPPD3: Returning status %d", status));
1168 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1170 * This function is deprecated - use @link cupsGetDests@ instead.
1175 int /* O - Number of printers */
1176 cupsGetPrinters(char ***printers) /* O - Printers */
1178 int n; /* Number of printers */
1179 ipp_t *request, /* IPP Request */
1180 *response; /* IPP Response */
1181 ipp_attribute_t *attr; /* Current attribute */
1182 char **temp; /* Temporary pointer */
1183 http_t *http; /* Connection to server */
1187 * Range check input...
1192 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
1200 * Try to connect to the server...
1203 if ((http = _cupsConnect()) == NULL)
1207 * Build a CUPS_GET_PRINTERS request, which requires the following
1210 * attributes-charset
1211 * attributes-natural-language
1212 * requested-attributes
1215 request = ippNewRequest(CUPS_GET_PRINTERS);
1217 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1218 "requested-attributes", NULL, "printer-name");
1220 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1223 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1224 "printer-type-mask", CUPS_PRINTER_CLASS);
1227 * Do the request and get back a response...
1232 if ((response = cupsDoRequest(http, request, "/")) != NULL)
1234 for (attr = response->attrs; attr != NULL; attr = attr->next)
1235 if (attr->name != NULL &&
1236 _cups_strcasecmp(attr->name, "printer-name") == 0 &&
1237 attr->value_tag == IPP_TAG_NAME)
1240 temp = malloc(sizeof(char *));
1242 temp = realloc(*printers, sizeof(char *) * (n + 1));
1247 * Ran out of memory!
1253 free((*printers)[n]);
1257 ippDelete(response);
1262 temp[n] = strdup(attr->values[0].string.text);
1266 ippDelete(response);
1274 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1276 * This function returns the named PPD file from the server. The
1277 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
1280 * You must remove (unlink) the PPD file when you are finished with
1281 * it. The PPD filename is stored in a static location that will be
1282 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1283 * or @link cupsGetServerPPD@.
1285 * @since CUPS 1.3/OS X 10.5@
1288 char * /* O - Name of PPD file or @code NULL@ on error */
1289 cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1290 const char *name) /* I - Name of PPD file ("ppd-name") */
1292 int fd; /* PPD file descriptor */
1293 ipp_t *request; /* IPP request */
1294 _cups_globals_t *cg = _cupsGlobals();
1295 /* Pointer to library globals */
1299 * Range check input...
1304 _cupsSetError(IPP_INTERNAL_ERROR, _("No PPD name"), 1);
1310 if ((http = _cupsConnect()) == NULL)
1314 * Get a temp file...
1317 if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0)
1320 * Can't open file; close the server connection and return NULL...
1323 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
1329 * Get the PPD file...
1332 request = ippNewRequest(CUPS_GET_PPD);
1333 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL,
1336 ippDelete(cupsDoIORequest(http, request, "/", -1, fd));
1340 if (cupsLastError() != IPP_OK)
1342 unlink(cg->ppd_filename);
1346 return (cg->ppd_filename);
1351 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1354 int /* O - Job ID or 0 on error */
1355 cupsPrintFile(const char *name, /* I - Destination name */
1356 const char *filename, /* I - File to print */
1357 const char *title, /* I - Title of job */
1358 int num_options,/* I - Number of options */
1359 cups_option_t *options) /* I - Options */
1361 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
1362 "title=\"%s\", num_options=%d, options=%p)",
1363 name, filename, title, num_options, options));
1365 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title,
1366 num_options, options));
1371 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1374 * @since CUPS 1.1.21/OS X 10.4@
1377 int /* O - Job ID or 0 on error */
1379 http_t *http, /* I - Connection to server */
1380 const char *name, /* I - Destination name */
1381 const char *filename, /* I - File to print */
1382 const char *title, /* I - Title of job */
1383 int num_options, /* I - Number of options */
1384 cups_option_t *options) /* I - Options */
1386 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
1387 "title=\"%s\", num_options=%d, options=%p)",
1388 http, name, filename, title, num_options, options));
1390 return (cupsPrintFiles2(http, name, 1, &filename, title, num_options,
1396 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1400 int /* O - Job ID or 0 on error */
1402 const char *name, /* I - Destination name */
1403 int num_files, /* I - Number of files */
1404 const char **files, /* I - File(s) to print */
1405 const char *title, /* I - Title of job */
1406 int num_options, /* I - Number of options */
1407 cups_option_t *options) /* I - Options */
1409 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
1410 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1411 name, num_files, files, title, num_options, options));
1414 * Print the file(s)...
1417 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title,
1418 num_options, options));
1423 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1426 * @since CUPS 1.1.21/OS X 10.4@
1429 int /* O - Job ID or 0 on error */
1431 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1432 const char *name, /* I - Destination name */
1433 int num_files, /* I - Number of files */
1434 const char **files, /* I - File(s) to print */
1435 const char *title, /* I - Title of job */
1436 int num_options, /* I - Number of options */
1437 cups_option_t *options) /* I - Options */
1439 int i; /* Looping var */
1440 int job_id; /* New job ID */
1441 const char *docname; /* Basename of current filename */
1442 const char *format; /* Document format */
1443 cups_file_t *fp; /* Current file */
1444 char buffer[8192]; /* Copy buffer */
1445 ssize_t bytes; /* Bytes in buffer */
1446 http_status_t status; /* Status of write */
1447 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1448 ipp_status_t cancel_status; /* Status code to preserve */
1449 char *cancel_message; /* Error message to preserve */
1452 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
1453 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1454 http, name, num_files, files, title, num_options, options));
1457 * Range check input...
1460 if (!name || num_files < 1 || !files)
1462 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
1468 * Create the print job...
1471 if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0)
1475 * Send each of the files...
1478 if (cupsGetOption("raw", num_options, options))
1479 format = CUPS_FORMAT_RAW;
1480 else if ((format = cupsGetOption("document-format", num_options,
1482 format = CUPS_FORMAT_AUTO;
1484 for (i = 0; i < num_files; i ++)
1487 * Start the next file...
1490 if ((docname = strrchr(files[i], '/')) != NULL)
1495 if ((fp = cupsFileOpen(files[i], "rb")) == NULL)
1498 * Unable to open print file, cancel the job and return...
1501 _cupsSetError(IPP_DOCUMENT_ACCESS_ERROR, NULL, 0);
1505 status = cupsStartDocument(http, name, job_id, docname, format,
1506 i == (num_files - 1));
1508 while (status == HTTP_CONTINUE &&
1509 (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
1510 status = cupsWriteRequestData(http, buffer, bytes);
1514 if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK)
1517 * Unable to queue, cancel the job and return...
1527 * If we get here, something happened while sending the print job so we need
1528 * to cancel the job without setting the last error (since we need to preserve
1529 * the current error...
1534 cancel_status = cg->last_error;
1535 cancel_message = cg->last_status_message ?
1536 _cupsStrRetain(cg->last_status_message) : NULL;
1538 cupsCancelJob2(http, name, job_id, 0);
1540 cg->last_error = cancel_status;
1541 cg->last_status_message = cancel_message;
1548 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1550 * Use @link cupsWriteRequestData@ to write data for the document and
1551 * @link cupsFinishDocument@ to finish the document and get the submission status.
1553 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1554 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1555 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1556 * any supported MIME type string can be supplied.
1558 * @since CUPS 1.4/OS X 10.6@
1561 http_status_t /* O - HTTP status of request */
1563 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1564 const char *name, /* I - Destination name */
1565 int job_id, /* I - Job ID from @link cupsCreateJob@ */
1566 const char *docname, /* I - Name of document */
1567 const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */
1568 int last_document) /* I - 1 for last document in job, 0 otherwise */
1570 char resource[1024], /* Resource for destinatio */
1571 printer_uri[1024]; /* Printer URI */
1572 ipp_t *request; /* Send-Document request */
1573 http_status_t status; /* HTTP status */
1577 * Create a Send-Document request...
1580 if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL)
1582 _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0);
1583 return (HTTP_ERROR);
1586 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
1587 NULL, "localhost", ippPort(), "/printers/%s", name);
1588 snprintf(resource, sizeof(resource), "/printers/%s", name);
1590 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1592 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
1593 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1596 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
1599 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1600 "document-format", NULL, format);
1601 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document);
1604 * Send and delete the request, then return the status...
1607 status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE);
1616 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
1617 * first printer in a class.
1620 static int /* O - 1 on success, 0 on failure */
1621 cups_get_printer_uri(
1622 http_t *http, /* I - Connection to server */
1623 const char *name, /* I - Name of printer or class */
1624 char *host, /* I - Hostname buffer */
1625 int hostsize, /* I - Size of hostname buffer */
1626 int *port, /* O - Port number */
1627 char *resource, /* I - Resource buffer */
1628 int resourcesize, /* I - Size of resource buffer */
1629 int depth) /* I - Depth of query */
1631 int i; /* Looping var */
1632 int http_port; /* Port number */
1633 http_t *http2; /* Alternate HTTP connection */
1634 ipp_t *request, /* IPP request */
1635 *response; /* IPP response */
1636 ipp_attribute_t *attr; /* Current attribute */
1637 char uri[HTTP_MAX_URI], /* printer-uri attribute */
1638 scheme[HTTP_MAX_URI], /* Scheme name */
1639 username[HTTP_MAX_URI], /* Username:password */
1640 classname[255], /* Temporary class name */
1641 http_hostname[HTTP_MAX_HOST];
1642 /* Hostname associated with connection */
1643 static const char * const requested_attrs[] =
1644 { /* Requested attributes */
1647 "printer-uri-supported",
1652 DEBUG_printf(("7cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
1653 "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)",
1654 http, name, host, hostsize, resource, resourcesize, depth));
1657 * Setup the printer URI...
1660 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
1661 "localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
1663 _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri"), 1);
1671 DEBUG_printf(("9cups_get_printer_uri: printer-uri=\"%s\"", uri));
1674 * Get the hostname and port number we are connected to...
1677 httpGetHostname(http, http_hostname, sizeof(http_hostname));
1678 http_port = _httpAddrPort(http->hostaddr);
1681 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1684 * attributes-charset
1685 * attributes-natural-language
1687 * requested-attributes
1690 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1692 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1695 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1696 "requested-attributes",
1697 sizeof(requested_attrs) / sizeof(requested_attrs[0]),
1698 NULL, requested_attrs);
1701 * Do the request and get back a response...
1704 snprintf(resource, resourcesize, "/printers/%s", name);
1706 if ((response = cupsDoRequest(http, request, resource)) != NULL)
1708 const char *device_uri = NULL; /* device-uri value */
1710 if ((attr = ippFindAttribute(response, "device-uri",
1711 IPP_TAG_URI)) != NULL)
1712 device_uri = attr->values[0].string.text;
1715 (!strncmp(device_uri, "ipp://", 6) ||
1716 !strncmp(device_uri, "ipps://", 7) ||
1717 ((strstr(device_uri, "._ipp.") != NULL ||
1718 strstr(device_uri, "._ipps.") != NULL) &&
1719 !strcmp(device_uri + strlen(device_uri) - 5, "/cups"))))
1722 * Statically-configured shared printer.
1725 httpSeparateURI(HTTP_URI_CODING_ALL,
1726 _httpResolveURI(device_uri, uri, sizeof(uri),
1727 _HTTP_RESOLVE_DEFAULT, NULL, NULL),
1728 scheme, sizeof(scheme), username, sizeof(username),
1729 host, hostsize, port, resource, resourcesize);
1730 ippDelete(response);
1734 else if ((attr = ippFindAttribute(response, "member-uris",
1735 IPP_TAG_URI)) != NULL)
1738 * Get the first actual printer name in the class...
1741 for (i = 0; i < attr->num_values; i ++)
1743 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1744 scheme, sizeof(scheme), username, sizeof(username),
1745 host, hostsize, port, resource, resourcesize);
1746 if (!strncmp(resource, "/printers/", 10))
1752 ippDelete(response);
1759 * No printers in this class - try recursively looking for a printer,
1760 * but not more than 3 levels deep...
1765 for (i = 0; i < attr->num_values; i ++)
1767 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1768 scheme, sizeof(scheme), username, sizeof(username),
1769 host, hostsize, port, resource, resourcesize);
1770 if (!strncmp(resource, "/classes/", 9))
1773 * Found a class! Connect to the right server...
1776 if (!_cups_strcasecmp(http_hostname, host) && *port == http_port)
1778 else if ((http2 = httpConnectEncrypt(host, *port,
1779 cupsEncryption())) == NULL)
1781 DEBUG_puts("8cups_get_printer_uri: Unable to connect to server");
1787 * Look up printers on that server...
1790 strlcpy(classname, resource + 9, sizeof(classname));
1792 cups_get_printer_uri(http2, classname, host, hostsize, port,
1793 resource, resourcesize, depth + 1);
1796 * Close the connection as needed...
1808 else if ((attr = ippFindAttribute(response, "printer-uri-supported",
1809 IPP_TAG_URI)) != NULL)
1811 httpSeparateURI(HTTP_URI_CODING_ALL,
1812 _httpResolveURI(attr->values[0].string.text, uri,
1813 sizeof(uri), _HTTP_RESOLVE_DEFAULT,
1815 scheme, sizeof(scheme), username, sizeof(username),
1816 host, hostsize, port, resource, resourcesize);
1817 ippDelete(response);
1819 if (!strncmp(resource, "/classes/", 9))
1821 _cupsSetError(IPP_INTERNAL_ERROR,
1822 _("No printer-uri found for class"), 1);
1833 ippDelete(response);
1836 if (cupsLastError() != IPP_NOT_FOUND)
1837 _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found"), 1);
1847 * End of "$Id: util.c 11173 2013-07-23 12:31:34Z msweet $".