]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/util.c
Merge changes from CUPS 1.5svn-r9717.
[thirdparty/cups.git] / cups / util.c
CommitLineData
ef416fc2 1/*
b19ccc9e 2 * "$Id: util.c 7850 2008-08-20 00:07:25Z mike $"
ef416fc2 3 *
71e16022 4 * Printing utilities for CUPS.
ef416fc2 5 *
ba55dc12 6 * Copyright 2007-2010 by Apple Inc.
ef416fc2 7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
fa73b229 19 * cupsCancelJob() - Cancel a print job on the default server.
3d052e43 20 * cupsCancelJob2() - Cancel or purge a print job.
6d2f911b 21 * cupsCreateJob() - Create an empty job for streaming.
3d052e43 22 * cupsFinishDocument() - Finish sending a document.
fa73b229 23 * cupsFreeJobs() - Free memory used by job data.
24 * cupsGetClasses() - Get a list of printer classes from the default
25 * server.
3d052e43
MS
26 * cupsGetDefault() - Get the default printer or class for the default
27 * server.
28 * cupsGetDefault2() - Get the default printer or class for the specified
fa73b229 29 * server.
fa73b229 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
33 * server.
3d052e43 34 * cupsGetPPD2() - Get the PPD file for a printer from the specified
fa73b229 35 * server.
2e4ff8af
MS
36 * cupsGetPPD3() - Get the PPD file for a printer on the specified
37 * server if it has changed.
fa73b229 38 * cupsGetPrinters() - Get a list of printers from the default server.
b94498cf 39 * cupsGetServerPPD() - Get an available PPD file from the server.
fa73b229 40 * cupsPrintFile() - Print a file to a printer or class on the default
41 * server.
42 * cupsPrintFile2() - Print a file to a printer or class on the
43 * specified server.
44 * cupsPrintFiles() - Print one or more files to a printer or class on
45 * the default server.
46 * cupsPrintFiles2() - Print one or more files to a printer or class on
47 * the specified server.
3d052e43
MS
48 * cupsStartDocument() - Add a document to a job created with
49 * cupsCreateJob().
fa73b229 50 * cups_get_printer_uri() - Get the printer-uri-supported attribute for the
51 * first printer in a class.
ef416fc2 52 */
53
54/*
55 * Include necessary headers...
56 */
57
71e16022 58#include "cups-private.h"
ef416fc2 59#include <fcntl.h>
60#include <sys/stat.h>
61#if defined(WIN32) || defined(__EMX__)
62# include <io.h>
63#else
64# include <unistd.h>
65#endif /* WIN32 || __EMX__ */
66
67
68/*
69 * Local functions...
70 */
71
fa73b229 72static int cups_get_printer_uri(http_t *http, const char *name,
73 char *host, int hostsize, int *port,
74 char *resource, int resourcesize,
75 int depth);
ef416fc2 76
77
78/*
79 * 'cupsCancelJob()' - Cancel a print job on the default server.
80 *
5a738aea
MS
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.
83 *
84 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
ef416fc2 85 * the cause of any failure.
86 */
87
88int /* O - 1 on success, 0 on failure */
89cupsCancelJob(const char *name, /* I - Name of printer or class */
568fa3fa 90 int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
ef416fc2 91{
5a738aea 92 return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0)
3d052e43
MS
93 < IPP_REDIRECTION_OTHER_SITE);
94}
95
96
97/*
98 * 'cupsCancelJob2()' - Cancel or purge a print job.
99 *
100 * Canceled jobs remain in the job history while purged jobs are removed
101 * from the job history.
102 *
5a738aea
MS
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.
105 *
106 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
3d052e43
MS
107 * the cause of any failure.
108 *
178cb736 109 * @since CUPS 1.4/Mac OS X 10.6@
3d052e43
MS
110 */
111
112ipp_status_t /* O - IPP status */
568fa3fa 113cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
5a738aea 114 const char *name, /* I - Name of printer or class */
568fa3fa 115 int job_id, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
5a738aea 116 int purge) /* I - 1 to purge, 0 to cancel */
3d052e43 117{
5a738aea 118 char uri[HTTP_MAX_URI]; /* Job/printer URI */
3d052e43 119 ipp_t *request; /* IPP request */
ef416fc2 120
121
122 /*
3d052e43 123 * Range check input...
ef416fc2 124 */
125
5a738aea 126 if (job_id < -1 || (!name && job_id == 0))
ef416fc2 127 {
749b1e90 128 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
ef416fc2 129 return (0);
130 }
131
132 /*
3d052e43 133 * Connect to the default server as needed...
ef416fc2 134 */
135
3d052e43
MS
136 if (!http)
137 if ((http = _cupsConnect()) == NULL)
138 return (IPP_SERVICE_UNAVAILABLE);
ef416fc2 139
140 /*
5a738aea 141 * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following
ef416fc2 142 * attributes:
143 *
144 * attributes-charset
145 * attributes-natural-language
5a738aea 146 * job-uri or printer-uri + job-id
3d052e43 147 * requesting-user-name
5a738aea 148 * [purge-job] or [purge-jobs]
ef416fc2 149 */
150
5a738aea 151 request = ippNewRequest(job_id < 0 ? IPP_PURGE_JOBS : IPP_CANCEL_JOB);
ef416fc2 152
5a738aea
MS
153 if (name)
154 {
155 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
156 "localhost", ippPort(), "/printers/%s", name);
157
158 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
159 uri);
160 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
161 job_id);
162 }
163 else if (job_id > 0)
164 {
165 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
166
167 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
168 }
ef416fc2 169
170 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
171 NULL, cupsUser());
5a738aea
MS
172
173 if (purge && job_id >= 0)
3d052e43 174 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1);
5a738aea
MS
175 else if (!purge && job_id < 0)
176 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0);
ef416fc2 177
178 /*
179 * Do the request...
180 */
181
3d052e43
MS
182 ippDelete(cupsDoRequest(http, request, "/jobs/"));
183
184 return (cupsLastError());
185}
186
187
188/*
568fa3fa 189 * 'cupsCreateJob()' - Create an empty job for streaming.
3d052e43 190 *
568fa3fa
MS
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
195 * instead.
3d052e43 196 *
178cb736 197 * @since CUPS 1.4/Mac OS X 10.6@
3d052e43
MS
198 */
199
200int /* O - Job ID or 0 on error */
201cupsCreateJob(
568fa3fa
MS
202 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
203 const char *name, /* I - Destination name */
3d052e43
MS
204 const char *title, /* I - Title of job */
205 int num_options, /* I - Number of options */
206 cups_option_t *options) /* I - Options */
207{
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 */
214
215
216 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", "
e07d4801 217 "num_options=%d, options=%p)",
3d052e43
MS
218 http, name, title, num_options, options));
219
220 /*
221 * Range check input...
222 */
223
224 if (!name)
225 {
749b1e90 226 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
3d052e43
MS
227 return (0);
228 }
229
230 /*
231 * Build a Create-Job request...
232 */
233
234 if ((request = ippNewRequest(IPP_CREATE_JOB)) == NULL)
235 {
749b1e90 236 _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0);
3d052e43
MS
237 return (0);
238 }
239
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);
243
244 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
245 NULL, printer_uri);
246 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
247 NULL, cupsUser());
248 if (title)
249 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
250 title);
251 cupsEncodeOptions(request, num_options, options);
252
253 /*
254 * Send the request and get the job-id...
255 */
256
257 response = cupsDoRequest(http, request, resource);
258
259 if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL)
260 job_id = attr->values[0].integer;
261
262 ippDelete(response);
263
264 /*
265 * Return it...
266 */
267
268 return (job_id);
269}
270
271
272/*
273 * 'cupsFinishDocument()' - Finish sending a document.
274 *
5a738aea
MS
275 * The document must have been started using @link cupsStartDocument@.
276 *
178cb736 277 * @since CUPS 1.4/Mac OS X 10.6@
3d052e43
MS
278 */
279
280ipp_status_t /* O - Status of document submission */
568fa3fa
MS
281cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
282 const char *name) /* I - Destination name */
3d052e43
MS
283{
284 char resource[1024]; /* Printer resource */
285
286
287 snprintf(resource, sizeof(resource), "/printers/%s", name);
ef416fc2 288
3d052e43
MS
289 ippDelete(cupsGetResponse(http, resource));
290
291 return (cupsLastError());
ef416fc2 292}
293
294
ef416fc2 295/*
296 * 'cupsFreeJobs()' - Free memory used by job data.
297 */
298
299void
300cupsFreeJobs(int num_jobs, /* I - Number of jobs */
301 cups_job_t *jobs) /* I - Jobs */
302{
3d052e43
MS
303 int i; /* Looping var */
304 cups_job_t *job; /* Current job */
ef416fc2 305
306
3d052e43 307 if (num_jobs <= 0 || !jobs)
ef416fc2 308 return;
309
3d052e43 310 for (i = num_jobs, job = jobs; i > 0; i --, job ++)
ef416fc2 311 {
3d052e43
MS
312 _cupsStrFree(job->dest);
313 _cupsStrFree(job->user);
314 _cupsStrFree(job->format);
315 _cupsStrFree(job->title);
ef416fc2 316 }
317
318 free(jobs);
319}
320
321
322/*
323 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
324 *
5a738aea 325 * This function is deprecated - use @link cupsGetDests@ instead.
ef416fc2 326 *
327 * @deprecated@
328 */
329
330int /* O - Number of classes */
331cupsGetClasses(char ***classes) /* O - Classes */
332{
333 int n; /* Number of classes */
334 ipp_t *request, /* IPP Request */
335 *response; /* IPP Response */
336 ipp_attribute_t *attr; /* Current attribute */
ef416fc2 337 char **temp; /* Temporary pointer */
3d052e43 338 http_t *http; /* Connection to server */
ef416fc2 339
340
3d052e43 341 if (!classes)
ef416fc2 342 {
749b1e90 343 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
ef416fc2 344
345 return (0);
346 }
347
3d052e43 348 *classes = NULL;
ef416fc2 349
3d052e43 350 if ((http = _cupsConnect()) == NULL)
ef416fc2 351 return (0);
ef416fc2 352
353 /*
354 * Build a CUPS_GET_CLASSES request, which requires the following
355 * attributes:
356 *
357 * attributes-charset
358 * attributes-natural-language
359 * requested-attributes
360 */
361
2e4ff8af 362 request = ippNewRequest(CUPS_GET_CLASSES);
ef416fc2 363
364 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
365 "requested-attributes", NULL, "printer-name");
366
367 /*
368 * Do the request and get back a response...
369 */
370
3d052e43 371 n = 0;
ef416fc2 372
3d052e43 373 if ((response = cupsDoRequest(http, request, "/")) != NULL)
ef416fc2 374 {
375 for (attr = response->attrs; attr != NULL; attr = attr->next)
376 if (attr->name != NULL &&
377 strcasecmp(attr->name, "printer-name") == 0 &&
378 attr->value_tag == IPP_TAG_NAME)
379 {
380 if (n == 0)
381 temp = malloc(sizeof(char *));
382 else
383 temp = realloc(*classes, sizeof(char *) * (n + 1));
384
385 if (temp == NULL)
386 {
387 /*
388 * Ran out of memory!
389 */
390
391 while (n > 0)
392 {
393 n --;
394 free((*classes)[n]);
395 }
396
397 free(*classes);
398 ippDelete(response);
399 return (0);
400 }
401
402 *classes = temp;
403 temp[n] = strdup(attr->values[0].string.text);
404 n ++;
405 }
406
407 ippDelete(response);
408 }
409
410 return (n);
411}
412
413
414/*
415 * 'cupsGetDefault()' - Get the default printer or class for the default server.
416 *
417 * This function returns the default printer or class as defined by
418 * the LPDEST or PRINTER environment variables. If these environment
419 * variables are not set, the server default destination is returned.
5a738aea
MS
420 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
421 * functions to get the user-defined default printer, as this function does
422 * not support the lpoptions-defined default printer.
ef416fc2 423 */
424
5a738aea 425const char * /* O - Default printer or @code NULL@ */
ef416fc2 426cupsGetDefault(void)
427{
ef416fc2 428 /*
429 * Return the default printer...
430 */
431
3d052e43 432 return (cupsGetDefault2(CUPS_HTTP_DEFAULT));
ef416fc2 433}
434
435
436/*
437 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
438 *
439 * This function returns the default printer or class as defined by
440 * the LPDEST or PRINTER environment variables. If these environment
441 * variables are not set, the server default destination is returned.
5a738aea
MS
442 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
443 * functions to get the user-defined default printer, as this function does
444 * not support the lpoptions-defined default printer.
ef416fc2 445 *
426c6a59 446 * @since CUPS 1.1.21/Mac OS X 10.4@
ef416fc2 447 */
448
5a738aea 449const char * /* O - Default printer or @code NULL@ */
568fa3fa 450cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ef416fc2 451{
452 ipp_t *request, /* IPP Request */
453 *response; /* IPP Response */
454 ipp_attribute_t *attr; /* Current attribute */
ef416fc2 455 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
456
457
458 /*
38e73f87 459 * See if we have a user default printer set...
ef416fc2 460 */
461
38e73f87
MS
462 if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer)))
463 return (cg->def_printer);
ef416fc2 464
465 /*
3d052e43 466 * Connect to the server as needed...
ef416fc2 467 */
468
469 if (!http)
3d052e43
MS
470 if ((http = _cupsConnect()) == NULL)
471 return (NULL);
ef416fc2 472
473 /*
474 * Build a CUPS_GET_DEFAULT request, which requires the following
475 * attributes:
476 *
477 * attributes-charset
478 * attributes-natural-language
479 */
480
2e4ff8af 481 request = ippNewRequest(CUPS_GET_DEFAULT);
ef416fc2 482
483 /*
484 * Do the request and get back a response...
485 */
486
487 if ((response = cupsDoRequest(http, request, "/")) != NULL)
488 {
3d052e43
MS
489 if ((attr = ippFindAttribute(response, "printer-name",
490 IPP_TAG_NAME)) != NULL)
ef416fc2 491 {
3d052e43
MS
492 strlcpy(cg->def_printer, attr->values[0].string.text,
493 sizeof(cg->def_printer));
ef416fc2 494 ippDelete(response);
495 return (cg->def_printer);
496 }
497
498 ippDelete(response);
499 }
500
501 return (NULL);
502}
503
504
505/*
506 * 'cupsGetJobs()' - Get the jobs from the default server.
5a738aea
MS
507 *
508 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
509 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
510 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
511 * jobs that are stopped, canceled, aborted, or completed.
ef416fc2 512 */
513
514int /* O - Number of jobs */
515cupsGetJobs(cups_job_t **jobs, /* O - Job data */
568fa3fa 516 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
ecdc0628 517 int myjobs, /* I - 0 = all users, 1 = mine */
5a738aea 518 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
ef416fc2 519{
ef416fc2 520 /*
521 * Return the jobs...
522 */
523
5a738aea 524 return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs));
ef416fc2 525}
526
527
528
529/*
530 * 'cupsGetJobs2()' - Get the jobs from the specified server.
531 *
5a738aea
MS
532 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
533 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
534 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
535 * jobs that are stopped, canceled, aborted, or completed.
536 *
426c6a59 537 * @since CUPS 1.1.21/Mac OS X 10.4@
ef416fc2 538 */
539
540int /* O - Number of jobs */
568fa3fa 541cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ef416fc2 542 cups_job_t **jobs, /* O - Job data */
568fa3fa 543 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
ecdc0628 544 int myjobs, /* I - 0 = all users, 1 = mine */
5a738aea 545 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
ef416fc2 546{
547 int n; /* Number of jobs */
548 ipp_t *request, /* IPP Request */
549 *response; /* IPP Response */
550 ipp_attribute_t *attr; /* Current attribute */
ef416fc2 551 cups_job_t *temp; /* Temporary pointer */
552 int id, /* job-id */
553 priority, /* job-priority */
554 size; /* job-k-octets */
555 ipp_jstate_t state; /* job-state */
556 time_t completed_time, /* time-at-completed */
557 creation_time, /* time-at-creation */
558 processing_time; /* time-at-processing */
559 const char *dest, /* job-printer-uri */
560 *format, /* document-format */
561 *title, /* job-name */
562 *user; /* job-originating-user-name */
563 char uri[HTTP_MAX_URI]; /* URI for jobs */
564 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
565 static const char * const attrs[] = /* Requested attributes */
566 {
5a662dc0 567 "document-format",
ef416fc2 568 "job-id",
ef416fc2 569 "job-k-octets",
5a662dc0
MS
570 "job-name",
571 "job-originating-user-name",
572 "job-printer-uri",
573 "job-priority",
ef416fc2 574 "job-state",
575 "time-at-completed",
576 "time-at-creation",
5a662dc0 577 "time-at-processing"
ef416fc2 578 };
579
580
581 /*
582 * Range check input...
583 */
584
3d052e43 585 if (!jobs)
ef416fc2 586 {
749b1e90 587 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
ef416fc2 588
589 return (-1);
590 }
591
592 /*
593 * Get the right URI...
594 */
595
5a738aea 596 if (name)
ef416fc2 597 {
a4d04587 598 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
5a738aea 599 "localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
ef416fc2 600 {
4d301e69 601 _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri"), 1);
ef416fc2 602
603 return (-1);
604 }
605 }
606 else
b9faaae1 607 strcpy(uri, "ipp://localhost/");
ef416fc2 608
3d052e43
MS
609 if (!http)
610 if ((http = _cupsConnect()) == NULL)
611 return (-1);
ef416fc2 612
613 /*
614 * Build an IPP_GET_JOBS request, which requires the following
615 * attributes:
616 *
617 * attributes-charset
618 * attributes-natural-language
619 * printer-uri
620 * requesting-user-name
621 * which-jobs
622 * my-jobs
623 * requested-attributes
624 */
625
2e4ff8af 626 request = ippNewRequest(IPP_GET_JOBS);
ef416fc2 627
628 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
629 "printer-uri", NULL, uri);
630
631 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
632 "requesting-user-name", NULL, cupsUser());
633
634 if (myjobs)
635 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
636
5a738aea 637 if (whichjobs == CUPS_WHICHJOBS_COMPLETED)
ef416fc2 638 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
639 "which-jobs", NULL, "completed");
5a738aea 640 else if (whichjobs == CUPS_WHICHJOBS_ALL)
ecdc0628 641 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
642 "which-jobs", NULL, "all");
ef416fc2 643
644 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
645 "requested-attributes", sizeof(attrs) / sizeof(attrs[0]),
646 NULL, attrs);
647
648 /*
649 * Do the request and get back a response...
650 */
651
652 n = 0;
653 *jobs = NULL;
654
655 if ((response = cupsDoRequest(http, request, "/")) != NULL)
656 {
3d052e43 657 for (attr = response->attrs; attr; attr = attr->next)
ef416fc2 658 {
659 /*
660 * Skip leading attributes until we hit a job...
661 */
662
3d052e43 663 while (attr && attr->group_tag != IPP_TAG_JOB)
ef416fc2 664 attr = attr->next;
665
3d052e43 666 if (!attr)
ef416fc2 667 break;
668
669 /*
670 * Pull the needed attributes from this job...
671 */
672
673 id = 0;
674 size = 0;
675 priority = 50;
676 state = IPP_JOB_PENDING;
677 user = "unknown";
678 dest = NULL;
679 format = "application/octet-stream";
680 title = "untitled";
681 creation_time = 0;
682 completed_time = 0;
683 processing_time = 0;
684
3d052e43 685 while (attr && attr->group_tag == IPP_TAG_JOB)
ef416fc2 686 {
3d052e43 687 if (!strcmp(attr->name, "job-id") &&
ef416fc2 688 attr->value_tag == IPP_TAG_INTEGER)
689 id = attr->values[0].integer;
3d052e43 690 else if (!strcmp(attr->name, "job-state") &&
ef416fc2 691 attr->value_tag == IPP_TAG_ENUM)
692 state = (ipp_jstate_t)attr->values[0].integer;
3d052e43 693 else if (!strcmp(attr->name, "job-priority") &&
ef416fc2 694 attr->value_tag == IPP_TAG_INTEGER)
695 priority = attr->values[0].integer;
3d052e43 696 else if (!strcmp(attr->name, "job-k-octets") &&
ef416fc2 697 attr->value_tag == IPP_TAG_INTEGER)
698 size = attr->values[0].integer;
3d052e43 699 else if (!strcmp(attr->name, "time-at-completed") &&
ef416fc2 700 attr->value_tag == IPP_TAG_INTEGER)
701 completed_time = attr->values[0].integer;
3d052e43 702 else if (!strcmp(attr->name, "time-at-creation") &&
ef416fc2 703 attr->value_tag == IPP_TAG_INTEGER)
704 creation_time = attr->values[0].integer;
3d052e43 705 else if (!strcmp(attr->name, "time-at-processing") &&
ef416fc2 706 attr->value_tag == IPP_TAG_INTEGER)
707 processing_time = attr->values[0].integer;
3d052e43 708 else if (!strcmp(attr->name, "job-printer-uri") &&
ef416fc2 709 attr->value_tag == IPP_TAG_URI)
710 {
711 if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
712 dest ++;
713 }
3d052e43 714 else if (!strcmp(attr->name, "job-originating-user-name") &&
ef416fc2 715 attr->value_tag == IPP_TAG_NAME)
716 user = attr->values[0].string.text;
3d052e43 717 else if (!strcmp(attr->name, "document-format") &&
ef416fc2 718 attr->value_tag == IPP_TAG_MIMETYPE)
719 format = attr->values[0].string.text;
3d052e43 720 else if (!strcmp(attr->name, "job-name") &&
ef416fc2 721 (attr->value_tag == IPP_TAG_TEXT ||
722 attr->value_tag == IPP_TAG_NAME))
723 title = attr->values[0].string.text;
724
725 attr = attr->next;
726 }
727
728 /*
729 * See if we have everything needed...
730 */
731
3d052e43 732 if (!dest || !id)
ef416fc2 733 {
3d052e43 734 if (!attr)
ef416fc2 735 break;
736 else
737 continue;
738 }
739
740 /*
741 * Allocate memory for the job...
742 */
743
744 if (n == 0)
745 temp = malloc(sizeof(cups_job_t));
746 else
747 temp = realloc(*jobs, sizeof(cups_job_t) * (n + 1));
748
3d052e43 749 if (!temp)
ef416fc2 750 {
751 /*
752 * Ran out of memory!
753 */
754
749b1e90 755 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
3d052e43 756
ef416fc2 757 cupsFreeJobs(n, *jobs);
758 *jobs = NULL;
759
760 ippDelete(response);
3d052e43
MS
761
762 return (-1);
ef416fc2 763 }
764
765 *jobs = temp;
766 temp += n;
767 n ++;
768
769 /*
770 * Copy the data over...
771 */
772
3d052e43
MS
773 temp->dest = _cupsStrAlloc(dest);
774 temp->user = _cupsStrAlloc(user);
775 temp->format = _cupsStrAlloc(format);
776 temp->title = _cupsStrAlloc(title);
ef416fc2 777 temp->id = id;
778 temp->priority = priority;
779 temp->state = state;
780 temp->size = size;
781 temp->completed_time = completed_time;
782 temp->creation_time = creation_time;
783 temp->processing_time = processing_time;
784
3d052e43 785 if (!attr)
ef416fc2 786 break;
787 }
788
789 ippDelete(response);
790 }
791
792 if (n == 0 && cg->last_error >= IPP_BAD_REQUEST)
793 return (-1);
794 else
795 return (n);
796}
797
798
799/*
800 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
801 *
5a738aea 802 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
ef416fc2 803 * in the class.
568fa3fa
MS
804 *
805 * The returned filename is stored in a static buffer and is overwritten with
4e6f60f0
MS
806 * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. The caller "owns" the
807 * file that is created and must @code unlink@ the returned filename.
ef416fc2 808 */
809
810const char * /* O - Filename for PPD file */
568fa3fa 811cupsGetPPD(const char *name) /* I - Destination name */
ef416fc2 812{
813 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
2e4ff8af
MS
814 time_t modtime = 0; /* Modification time */
815
ef416fc2 816
ef416fc2 817 /*
818 * Return the PPD file...
819 */
820
2e4ff8af
MS
821 cg->ppd_filename[0] = '\0';
822
3d052e43 823 if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, cg->ppd_filename,
2e4ff8af
MS
824 sizeof(cg->ppd_filename)) == HTTP_OK)
825 return (cg->ppd_filename);
826 else
827 return (NULL);
ef416fc2 828}
829
830
831/*
832 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
833 *
5a738aea 834 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
ef416fc2 835 * in the class.
836 *
568fa3fa 837 * The returned filename is stored in a static buffer and is overwritten with
4e6f60f0
MS
838 * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. The caller "owns" the
839 * file that is created and must @code unlink@ the returned filename.
568fa3fa 840 *
426c6a59 841 * @since CUPS 1.1.21/Mac OS X 10.4@
ef416fc2 842 */
843
844const char * /* O - Filename for PPD file */
568fa3fa
MS
845cupsGetPPD2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
846 const char *name) /* I - Destination name */
2e4ff8af
MS
847{
848 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
849 time_t modtime = 0; /* Modification time */
850
851
852 cg->ppd_filename[0] = '\0';
853
854 if (cupsGetPPD3(http, name, &modtime, cg->ppd_filename,
855 sizeof(cg->ppd_filename)) == HTTP_OK)
856 return (cg->ppd_filename);
857 else
858 return (NULL);
859}
860
861
862/*
863 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
864 * server if it has changed.
865 *
866 * The "modtime" parameter contains the modification time of any
867 * locally-cached content and is updated with the time from the PPD file on
868 * the server.
869 *
870 * The "buffer" parameter contains the local PPD filename. If it contains
871 * the empty string, a new temporary file is created, otherwise the existing
4e6f60f0
MS
872 * file will be overwritten as needed. The caller "owns" the file that is
873 * created and must @code unlink@ the returned filename.
2e4ff8af 874 *
5a738aea
MS
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.
878 *
879 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
880 * in the class.
568fa3fa 881 *
178cb736 882 * @since CUPS 1.4/Mac OS X 10.6@
2e4ff8af
MS
883 */
884
885http_status_t /* O - HTTP status */
5a738aea 886cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
568fa3fa 887 const char *name, /* I - Destination name */
2e4ff8af
MS
888 time_t *modtime, /* IO - Modification time */
889 char *buffer, /* I - Filename buffer */
890 size_t bufsize) /* I - Size of filename buffer */
ef416fc2 891{
ef416fc2 892 int http_port; /* Port number */
ed486911 893 char http_hostname[HTTP_MAX_HOST];
894 /* Hostname associated with connection */
ef416fc2 895 http_t *http2; /* Alternate HTTP connection */
ef416fc2 896 int fd; /* PPD file */
fa73b229 897 char localhost[HTTP_MAX_URI],/* Local hostname */
ef416fc2 898 hostname[HTTP_MAX_URI], /* Hostname */
899 resource[HTTP_MAX_URI]; /* Resource name */
900 int port; /* Port number */
901 http_status_t status; /* HTTP status from server */
902 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
ef416fc2 903
904
905 /*
906 * Range check input...
907 */
908
2e4ff8af 909 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
e07d4801 910 "bufsize=%d)", http, name, modtime,
ae71f5de 911 modtime ? (int)*modtime : 0, buffer, (int)bufsize));
ef416fc2 912
2e4ff8af
MS
913 if (!name)
914 {
4d301e69 915 _cupsSetError(IPP_INTERNAL_ERROR, _("No printer name"), 1);
2e4ff8af
MS
916 return (HTTP_NOT_ACCEPTABLE);
917 }
918
919 if (!modtime)
920 {
4d301e69 921 _cupsSetError(IPP_INTERNAL_ERROR, _("No modification time"), 1);
2e4ff8af
MS
922 return (HTTP_NOT_ACCEPTABLE);
923 }
924
925 if (!buffer || bufsize <= 1)
926 {
4d301e69 927 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad filename buffer"), 1);
2e4ff8af 928 return (HTTP_NOT_ACCEPTABLE);
ef416fc2 929 }
930
426c6a59
MS
931#ifndef WIN32
932 /*
933 * See if the PPD file is available locally...
934 */
935
936 if (!cg->servername[0])
937 cupsServer();
938
939 if (!strcasecmp(cg->servername, "localhost"))
940 {
941 char ppdname[1024]; /* PPD filename */
942 struct stat ppdinfo; /* PPD file information */
943
944
d1c13e16
MS
945 snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd", cg->cups_serverroot,
946 name);
426c6a59
MS
947 if (!stat(ppdname, &ppdinfo))
948 {
949 /*
950 * OK, the file exists, use it!
951 */
952
953 if (buffer[0])
954 {
955 unlink(buffer);
956
957 if (symlink(ppdname, buffer) && errno != EEXIST)
958 {
959 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
960
961 return (HTTP_SERVER_ERROR);
962 }
963 }
964 else
965 {
966 int tries; /* Number of tries */
967 const char *tmpdir; /* TMPDIR environment variable */
968 struct timeval curtime; /* Current time */
969
970 /*
971 * Previously we put root temporary files in the default CUPS temporary
972 * directory under /var/spool/cups. However, since the scheduler cleans
973 * out temporary files there and runs independently of the user apps, we
974 * don't want to use it unless specifically told to by cupsd.
975 */
976
977 if ((tmpdir = getenv("TMPDIR")) == NULL)
978# ifdef __APPLE__
979 tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */
980# else
981 tmpdir = "/tmp";
982# endif /* __APPLE__ */
983
984 /*
985 * Make the temporary name using the specified directory...
986 */
987
988 tries = 0;
989
990 do
991 {
992 /*
993 * Get the current time of day...
994 */
995
996 gettimeofday(&curtime, NULL);
997
998 /*
999 * Format a string using the hex time values...
1000 */
1001
1002 snprintf(buffer, bufsize, "%s/%08lx%05lx", tmpdir,
1003 (unsigned long)curtime.tv_sec,
1004 (unsigned long)curtime.tv_usec);
1005
1006 /*
1007 * Try to make a symlink...
1008 */
1009
1010 if (!symlink(ppdname, buffer))
1011 break;
1012
1013 tries ++;
1014 }
1015 while (tries < 1000);
1016
1017 if (tries >= 1000)
1018 {
1019 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
1020
1021 return (HTTP_SERVER_ERROR);
1022 }
1023 }
1024
d1c13e16 1025 if (*modtime >= ppdinfo.st_mtime)
426c6a59
MS
1026 return (HTTP_NOT_MODIFIED);
1027 else
1028 {
1029 *modtime = ppdinfo.st_mtime;
1030 return (HTTP_OK);
1031 }
1032 }
1033 }
1034#endif /* !WIN32 */
1035
ef416fc2 1036 /*
fa73b229 1037 * Try finding a printer URI for this printer...
ef416fc2 1038 */
1039
3d052e43 1040 if (!http)
a603edef
MS
1041 if ((http = _cupsConnect()) == NULL)
1042 return (HTTP_SERVICE_UNAVAILABLE);
3d052e43 1043
fa73b229 1044 if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port,
1045 resource, sizeof(resource), 0))
2e4ff8af 1046 return (HTTP_NOT_FOUND);
ef416fc2 1047
e07d4801 1048 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname,
1ff0402e 1049 port));
8ca02f3c 1050
ef416fc2 1051 /*
fa73b229 1052 * Remap local hostname to localhost...
1053 */
1054
757d2cad 1055 httpGetHostname(NULL, localhost, sizeof(localhost));
fa73b229 1056
e07d4801 1057 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost));
8ca02f3c 1058
fa73b229 1059 if (!strcasecmp(localhost, hostname))
1060 strcpy(hostname, "localhost");
1061
1062 /*
ed486911 1063 * Get the hostname and port number we are connected to...
ef416fc2 1064 */
1065
ed486911 1066 httpGetHostname(http, http_hostname, sizeof(http_hostname));
1ff0402e 1067 http_port = _httpAddrPort(http->hostaddr);
ed486911 1068
e07d4801 1069 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
1ff0402e 1070 http_hostname, http_port));
8ca02f3c 1071
ef416fc2 1072 /*
1073 * Reconnect to the correct server as needed...
1074 */
1075
ed486911 1076 if (!strcasecmp(http_hostname, hostname) && port == http_port)
ef416fc2 1077 http2 = http;
1078 else if ((http2 = httpConnectEncrypt(hostname, port,
1079 cupsEncryption())) == NULL)
1080 {
4d301e69 1081 DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
ef416fc2 1082
2e4ff8af 1083 return (HTTP_SERVICE_UNAVAILABLE);
ef416fc2 1084 }
1085
1086 /*
1087 * Get a temp file...
1088 */
1089
2e4ff8af
MS
1090 if (buffer[0])
1091 fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600);
1092 else
1093 fd = cupsTempFd(buffer, bufsize);
1094
1095 if (fd < 0)
ef416fc2 1096 {
1097 /*
1098 * Can't open file; close the server connection and return NULL...
1099 */
1100
749b1e90 1101 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
ef416fc2 1102
1103 if (http2 != http)
1104 httpClose(http2);
1105
2e4ff8af 1106 return (HTTP_SERVER_ERROR);
ef416fc2 1107 }
1108
1109 /*
1110 * And send a request to the HTTP server...
1111 */
1112
fa73b229 1113 strlcat(resource, ".ppd", sizeof(resource));
ef416fc2 1114
2e4ff8af
MS
1115 if (*modtime > 0)
1116 httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE,
1117 httpGetDateString(*modtime));
1118
ef416fc2 1119 status = cupsGetFd(http2, resource, fd);
1120
1121 close(fd);
1122
ef416fc2 1123 /*
1124 * See if we actually got the file or an error...
1125 */
1126
2e4ff8af
MS
1127 if (status == HTTP_OK)
1128 *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE));
1129 else if (status != HTTP_NOT_MODIFIED)
ef416fc2 1130 {
e07d4801 1131 _cupsSetHTTPError(status);
ef416fc2 1132
1133 unlink(cg->ppd_filename);
ef416fc2 1134 }
1135
91c84a35
MS
1136 if (http2 != http)
1137 httpClose(http2);
1138
ef416fc2 1139 /*
1140 * Return the PPD file...
1141 */
1142
e07d4801
MS
1143 DEBUG_printf(("1cupsGetPPD3: Returning status %d", status));
1144
2e4ff8af 1145 return (status);
ef416fc2 1146}
1147
1148
1149/*
1150 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1151 *
5a738aea 1152 * This function is deprecated - use @link cupsGetDests@ instead.
ef416fc2 1153 *
1154 * @deprecated@
1155 */
1156
1157int /* O - Number of printers */
1158cupsGetPrinters(char ***printers) /* O - Printers */
1159{
1160 int n; /* Number of printers */
1161 ipp_t *request, /* IPP Request */
1162 *response; /* IPP Response */
1163 ipp_attribute_t *attr; /* Current attribute */
ef416fc2 1164 char **temp; /* Temporary pointer */
3d052e43
MS
1165 http_t *http; /* Connection to server */
1166
ef416fc2 1167
3d052e43
MS
1168 /*
1169 * Range check input...
1170 */
ef416fc2 1171
3d052e43 1172 if (!printers)
ef416fc2 1173 {
749b1e90 1174 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
ef416fc2 1175
1176 return (0);
1177 }
1178
3d052e43
MS
1179 *printers = NULL;
1180
ef416fc2 1181 /*
1182 * Try to connect to the server...
1183 */
1184
3d052e43 1185 if ((http = _cupsConnect()) == NULL)
ef416fc2 1186 return (0);
ef416fc2 1187
1188 /*
1189 * Build a CUPS_GET_PRINTERS request, which requires the following
1190 * attributes:
1191 *
1192 * attributes-charset
1193 * attributes-natural-language
1194 * requested-attributes
1195 */
1196
2e4ff8af 1197 request = ippNewRequest(CUPS_GET_PRINTERS);
ef416fc2 1198
1199 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1200 "requested-attributes", NULL, "printer-name");
1201
1202 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1203 "printer-type", 0);
1204
1205 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1206 "printer-type-mask", CUPS_PRINTER_CLASS);
1207
1208 /*
1209 * Do the request and get back a response...
1210 */
1211
3d052e43 1212 n = 0;
ef416fc2 1213
3d052e43 1214 if ((response = cupsDoRequest(http, request, "/")) != NULL)
ef416fc2 1215 {
1216 for (attr = response->attrs; attr != NULL; attr = attr->next)
1217 if (attr->name != NULL &&
1218 strcasecmp(attr->name, "printer-name") == 0 &&
1219 attr->value_tag == IPP_TAG_NAME)
1220 {
1221 if (n == 0)
1222 temp = malloc(sizeof(char *));
1223 else
1224 temp = realloc(*printers, sizeof(char *) * (n + 1));
1225
1226 if (temp == NULL)
1227 {
1228 /*
1229 * Ran out of memory!
1230 */
1231
1232 while (n > 0)
1233 {
1234 n --;
1235 free((*printers)[n]);
1236 }
1237
1238 free(*printers);
1239 ippDelete(response);
1240 return (0);
1241 }
1242
1243 *printers = temp;
1244 temp[n] = strdup(attr->values[0].string.text);
1245 n ++;
1246 }
1247
1248 ippDelete(response);
1249 }
1250
1251 return (n);
1252}
1253
1254
b94498cf 1255/*
1256 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1257 *
1258 * This function returns the named PPD file from the server. The
5a738aea 1259 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
b94498cf 1260 * operation.
1261 *
1262 * You must remove (unlink) the PPD file when you are finished with
1263 * it. The PPD filename is stored in a static location that will be
5a738aea
MS
1264 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1265 * or @link cupsGetServerPPD@.
b94498cf 1266 *
426c6a59 1267 * @since CUPS 1.3/Mac OS X 10.5@
b94498cf 1268 */
1269
5a738aea 1270char * /* O - Name of PPD file or @code NULL@ on error */
568fa3fa 1271cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
b94498cf 1272 const char *name) /* I - Name of PPD file ("ppd-name") */
1273{
1274 int fd; /* PPD file descriptor */
1275 ipp_t *request; /* IPP request */
1276 _cups_globals_t *cg = _cupsGlobals();
1277 /* Pointer to library globals */
1278
1279
1280 /*
1281 * Range check input...
1282 */
1283
3d052e43 1284 if (!name)
b94498cf 1285 {
4d301e69 1286 _cupsSetError(IPP_INTERNAL_ERROR, _("No PPD name"), 1);
b94498cf 1287
1288 return (NULL);
1289 }
1290
3d052e43
MS
1291 if (!http)
1292 if ((http = _cupsConnect()) == NULL)
1293 return (NULL);
1294
b94498cf 1295 /*
1296 * Get a temp file...
1297 */
1298
1299 if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0)
1300 {
1301 /*
1302 * Can't open file; close the server connection and return NULL...
1303 */
1304
749b1e90 1305 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
b94498cf 1306
1307 return (NULL);
1308 }
1309
1310 /*
1311 * Get the PPD file...
1312 */
1313
1314 request = ippNewRequest(CUPS_GET_PPD);
1315 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL,
1316 name);
1317
1318 ippDelete(cupsDoIORequest(http, request, "/", -1, fd));
1319
1320 close(fd);
1321
1322 if (cupsLastError() != IPP_OK)
1323 {
1324 unlink(cg->ppd_filename);
1325 return (NULL);
1326 }
1327 else
1328 return (cg->ppd_filename);
1329}
1330
1331
ef416fc2 1332/*
1333 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1334 */
1335
5a738aea 1336int /* O - Job ID or 0 on error */
568fa3fa 1337cupsPrintFile(const char *name, /* I - Destination name */
ef416fc2 1338 const char *filename, /* I - File to print */
1339 const char *title, /* I - Title of job */
1340 int num_options,/* I - Number of options */
1341 cups_option_t *options) /* I - Options */
1342{
1343 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
e07d4801 1344 "title=\"%s\", num_options=%d, options=%p)",
ef416fc2 1345 name, filename, title, num_options, options));
1346
3d052e43
MS
1347 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title,
1348 num_options, options));
ef416fc2 1349}
1350
1351
1352/*
5a738aea
MS
1353 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1354 * server.
ef416fc2 1355 *
426c6a59 1356 * @since CUPS 1.1.21/Mac OS X 10.4@
ef416fc2 1357 */
1358
5a738aea 1359int /* O - Job ID or 0 on error */
3d052e43 1360cupsPrintFile2(
568fa3fa
MS
1361 http_t *http, /* I - Connection to server */
1362 const char *name, /* I - Destination name */
3d052e43
MS
1363 const char *filename, /* I - File to print */
1364 const char *title, /* I - Title of job */
1365 int num_options, /* I - Number of options */
1366 cups_option_t *options) /* I - Options */
ef416fc2 1367{
1368 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
e07d4801 1369 "title=\"%s\", num_options=%d, options=%p)",
ef416fc2 1370 http, name, filename, title, num_options, options));
1371
3d052e43
MS
1372 return (cupsPrintFiles2(http, name, 1, &filename, title, num_options,
1373 options));
ef416fc2 1374}
1375
1376
1377/*
fa73b229 1378 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1379 * default server.
ef416fc2 1380 */
1381
5a738aea 1382int /* O - Job ID or 0 on error */
3d052e43 1383cupsPrintFiles(
568fa3fa 1384 const char *name, /* I - Destination name */
3d052e43
MS
1385 int num_files, /* I - Number of files */
1386 const char **files, /* I - File(s) to print */
1387 const char *title, /* I - Title of job */
1388 int num_options, /* I - Number of options */
1389 cups_option_t *options) /* I - Options */
ef416fc2 1390{
ef416fc2 1391 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
e07d4801 1392 "files=%p, title=\"%s\", num_options=%d, options=%p)",
ef416fc2 1393 name, num_files, files, title, num_options, options));
1394
ef416fc2 1395 /*
1396 * Print the file(s)...
1397 */
1398
3d052e43 1399 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title,
ef416fc2 1400 num_options, options));
1401}
1402
1403
ef416fc2 1404/*
fa73b229 1405 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1406 * specified server.
ef416fc2 1407 *
426c6a59 1408 * @since CUPS 1.1.21/Mac OS X 10.4@
ef416fc2 1409 */
1410
5a738aea 1411int /* O - Job ID or 0 on error */
3d052e43 1412cupsPrintFiles2(
568fa3fa
MS
1413 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1414 const char *name, /* I - Destination name */
3d052e43
MS
1415 int num_files, /* I - Number of files */
1416 const char **files, /* I - File(s) to print */
1417 const char *title, /* I - Title of job */
1418 int num_options, /* I - Number of options */
1419 cups_option_t *options) /* I - Options */
ef416fc2 1420{
1421 int i; /* Looping var */
3d052e43
MS
1422 int job_id; /* New job ID */
1423 const char *docname; /* Basename of current filename */
1424 const char *format; /* Document format */
1425 cups_file_t *fp; /* Current file */
1426 char buffer[8192]; /* Copy buffer */
1427 ssize_t bytes; /* Bytes in buffer */
1428 http_status_t status; /* Status of write */
5a662dc0
MS
1429 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1430 ipp_status_t cancel_status; /* Status code to preserve */
1431 char *cancel_message; /* Error message to preserve */
ef416fc2 1432
1433
3d052e43 1434 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
e07d4801 1435 "files=%p, title=\"%s\", num_options=%d, options=%p)",
ef416fc2 1436 http, name, num_files, files, title, num_options, options));
1437
1438 /*
1439 * Range check input...
1440 */
1441
3d052e43 1442 if (!name || num_files < 1 || !files)
ef416fc2 1443 {
749b1e90 1444 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
ef416fc2 1445
1446 return (0);
1447 }
1448
1449 /*
3d052e43 1450 * Create the print job...
ef416fc2 1451 */
1452
3d052e43 1453 if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0)
ef416fc2 1454 return (0);
ef416fc2 1455
1456 /*
3d052e43 1457 * Send each of the files...
ef416fc2 1458 */
1459
3d052e43
MS
1460 if (cupsGetOption("raw", num_options, options))
1461 format = CUPS_FORMAT_RAW;
1462 else if ((format = cupsGetOption("document-format", num_options,
1463 options)) == NULL)
1464 format = CUPS_FORMAT_AUTO;
ef416fc2 1465
3d052e43
MS
1466 for (i = 0; i < num_files; i ++)
1467 {
1468 /*
1469 * Start the next file...
1470 */
ef416fc2 1471
3d052e43
MS
1472 if ((docname = strrchr(files[i], '/')) != NULL)
1473 docname ++;
1474 else
1475 docname = files[i];
ef416fc2 1476
3d052e43
MS
1477 if ((fp = cupsFileOpen(files[i], "rb")) == NULL)
1478 {
1479 /*
1480 * Unable to open print file, cancel the job and return...
1481 */
ef416fc2 1482
5a662dc0
MS
1483 _cupsSetError(IPP_DOCUMENT_ACCESS_ERROR, NULL, 0);
1484 goto cancel_job;
3d052e43 1485 }
ef416fc2 1486
ba55dc12
MS
1487 status = cupsStartDocument(http, name, job_id, docname, format,
1488 i == (num_files - 1));
ef416fc2 1489
ba55dc12
MS
1490 while (status == HTTP_CONTINUE &&
1491 (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
1492 status = cupsWriteRequestData(http, buffer, bytes);
ef416fc2 1493
3d052e43 1494 cupsFileClose(fp);
ef416fc2 1495
3d052e43 1496 if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK)
ef416fc2 1497 {
1498 /*
3d052e43 1499 * Unable to queue, cancel the job and return...
ef416fc2 1500 */
1501
5a662dc0 1502 goto cancel_job;
3d052e43
MS
1503 }
1504 }
ef416fc2 1505
3d052e43 1506 return (job_id);
5a662dc0
MS
1507
1508 /*
1509 * If we get here, something happened while sending the print job so we need
1510 * to cancel the job without setting the last error (since we need to preserve
1511 * the current error...
1512 */
1513
1514 cancel_job:
1515
1516 cancel_status = cg->last_error;
1517 cancel_message = cg->last_status_message ?
1518 _cupsStrRetain(cg->last_status_message) : NULL;
1519
1520 cupsCancelJob2(http, name, job_id, 0);
1521
1522 cg->last_error = cancel_status;
1523 cg->last_status_message = cancel_message;
1524
1525 return (0);
3d052e43 1526}
ef416fc2 1527
ef416fc2 1528
3d052e43
MS
1529/*
1530 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1531 *
5a738aea
MS
1532 * Use @link cupsWriteRequestData@ to write data for the document and
1533 * @link cupsFinishDocument@ to finish the document and get the submission status.
3d052e43 1534 *
5a738aea
MS
1535 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1536 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1537 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1538 * any supported MIME type string can be supplied.
3d052e43 1539 *
178cb736 1540 * @since CUPS 1.4/Mac OS X 10.6@
3d052e43 1541 */
ef416fc2 1542
3d052e43
MS
1543http_status_t /* O - HTTP status of request */
1544cupsStartDocument(
568fa3fa
MS
1545 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1546 const char *name, /* I - Destination name */
5a738aea 1547 int job_id, /* I - Job ID from @link cupsCreateJob@ */
3d052e43 1548 const char *docname, /* I - Name of document */
5a738aea 1549 const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */
3d052e43
MS
1550 int last_document) /* I - 1 for last document in job, 0 otherwise */
1551{
1552 char resource[1024], /* Resource for destinatio */
1553 printer_uri[1024]; /* Printer URI */
1554 ipp_t *request; /* Send-Document request */
1555 http_status_t status; /* HTTP status */
ef416fc2 1556
bd7854cb 1557
3d052e43
MS
1558 /*
1559 * Create a Send-Document request...
1560 */
bd7854cb 1561
3d052e43
MS
1562 if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL)
1563 {
749b1e90 1564 _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0);
3d052e43
MS
1565 return (0);
1566 }
bd7854cb 1567
3d052e43
MS
1568 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
1569 NULL, "localhost", ippPort(), "/printers/%s", name);
1570 snprintf(resource, sizeof(resource), "/printers/%s", name);
ef416fc2 1571
3d052e43
MS
1572 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1573 NULL, printer_uri);
1574 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
1575 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1576 NULL, cupsUser());
1577 if (docname)
1578 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
1579 NULL, docname);
1580 if (format)
1581 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1582 "document-format", NULL, format);
1583 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document);
ef416fc2 1584
3d052e43
MS
1585 /*
1586 * Send and delete the request, then return the status...
1587 */
ef416fc2 1588
3d052e43 1589 status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE);
ef416fc2 1590
3d052e43 1591 ippDelete(request);
ef416fc2 1592
3d052e43 1593 return (status);
ef416fc2 1594}
1595
1596
fa73b229 1597/*
58dc1933
MS
1598 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
1599 * first printer in a class.
fa73b229 1600 */
1601
1602static int /* O - 1 on success, 0 on failure */
1603cups_get_printer_uri(
568fa3fa 1604 http_t *http, /* I - Connection to server */
fa73b229 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 */
1612{
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 */
ed486911 1622 classname[255], /* Temporary class name */
1623 http_hostname[HTTP_MAX_HOST];
1624 /* Hostname associated with connection */
fa73b229 1625 static const char * const requested_attrs[] =
1626 { /* Requested attributes */
58dc1933 1627 "member-uris",
fa73b229 1628 "printer-uri-supported",
58dc1933 1629 "printer-type"
fa73b229 1630 };
1631
1632
e07d4801
MS
1633 DEBUG_printf(("7cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
1634 "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)",
1635 http, name, host, hostsize, resource, resourcesize, depth));
fa73b229 1636
1637 /*
1638 * Setup the printer URI...
1639 */
1640
a4d04587 1641 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
1642 "localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
fa73b229 1643 {
4d301e69 1644 _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri"), 1);
fa73b229 1645
1646 *host = '\0';
1647 *resource = '\0';
1648
1649 return (0);
1650 }
1651
e07d4801 1652 DEBUG_printf(("9cups_get_printer_uri: printer-uri=\"%s\"", uri));
fa73b229 1653
1654 /*
ed486911 1655 * Get the hostname and port number we are connected to...
fa73b229 1656 */
1657
ed486911 1658 httpGetHostname(http, http_hostname, sizeof(http_hostname));
1ff0402e 1659 http_port = _httpAddrPort(http->hostaddr);
fa73b229 1660
1661 /*
1662 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1663 * attributes:
1664 *
1665 * attributes-charset
1666 * attributes-natural-language
1667 * printer-uri
1668 * requested-attributes
1669 */
1670
1671 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1672
1673 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1674 NULL, uri);
1675
1676 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1677 "requested-attributes",
1678 sizeof(requested_attrs) / sizeof(requested_attrs[0]),
1679 NULL, requested_attrs);
1680
1681 /*
1682 * Do the request and get back a response...
1683 */
1684
1685 if ((response = cupsDoRequest(http, request, "/")) != NULL)
1686 {
1687 if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
1688 {
1689 /*
1690 * Get the first actual printer name in the class...
1691 */
1692
1693 for (i = 0; i < attr->num_values; i ++)
1694 {
a4d04587 1695 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1696 scheme, sizeof(scheme), username, sizeof(username),
1697 host, hostsize, port, resource, resourcesize);
fa73b229 1698 if (!strncmp(resource, "/printers/", 10))
1699 {
1700 /*
1701 * Found a printer!
1702 */
1703
1704 ippDelete(response);
1705
1706 return (1);
1707 }
1708 }
1709
1710 /*
1711 * No printers in this class - try recursively looking for a printer,
1712 * but not more than 3 levels deep...
1713 */
1714
1715 if (depth < 3)
1716 {
1717 for (i = 0; i < attr->num_values; i ++)
1718 {
a4d04587 1719 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1720 scheme, sizeof(scheme), username, sizeof(username),
1721 host, hostsize, port, resource, resourcesize);
fa73b229 1722 if (!strncmp(resource, "/classes/", 9))
1723 {
1724 /*
1725 * Found a class! Connect to the right server...
1726 */
1727
ed486911 1728 if (!strcasecmp(http_hostname, host) && *port == http_port)
fa73b229 1729 http2 = http;
1730 else if ((http2 = httpConnectEncrypt(host, *port,
1731 cupsEncryption())) == NULL)
1732 {
4d301e69 1733 DEBUG_puts("8cups_get_printer_uri: Unable to connect to server");
fa73b229 1734
1735 continue;
1736 }
1737
1738 /*
1739 * Look up printers on that server...
1740 */
1741
1742 strlcpy(classname, resource + 9, sizeof(classname));
1743
1744 cups_get_printer_uri(http2, classname, host, hostsize, port,
1745 resource, resourcesize, depth + 1);
1746
1747 /*
1748 * Close the connection as needed...
1749 */
1750
1751 if (http2 != http)
1752 httpClose(http2);
1753
1754 if (*host)
1755 return (1);
1756 }
1757 }
1758 }
1759 }
1760 else if ((attr = ippFindAttribute(response, "printer-uri-supported",
1761 IPP_TAG_URI)) != NULL)
1762 {
58dc1933
MS
1763 httpSeparateURI(HTTP_URI_CODING_ALL,
1764 _httpResolveURI(attr->values[0].string.text, uri,
07ed0e9a 1765 sizeof(uri), 0, NULL, NULL),
a4d04587 1766 scheme, sizeof(scheme), username, sizeof(username),
1767 host, hostsize, port, resource, resourcesize);
fa73b229 1768 ippDelete(response);
1769
dd1abb6b
MS
1770 if (!strncmp(resource, "/classes/", 9))
1771 {
749b1e90 1772 _cupsSetError(IPP_INTERNAL_ERROR,
4d301e69 1773 _("No printer-uri found for class"), 1);
dd1abb6b
MS
1774
1775 *host = '\0';
1776 *resource = '\0';
1777
1778 return (0);
1779 }
1780
fa73b229 1781 return (1);
1782 }
1783
1784 ippDelete(response);
1785 }
1786
b86bc4cf 1787 if (cupsLastError() != IPP_NOT_FOUND)
4d301e69 1788 _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found"), 1);
b423cd4c 1789
fa73b229 1790 *host = '\0';
1791 *resource = '\0';
1792
1793 return (0);
1794}
1795
1796
ef416fc2 1797/*
b19ccc9e 1798 * End of "$Id: util.c 7850 2008-08-20 00:07:25Z mike $".
ef416fc2 1799 */