]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/util.c
Merge changes from CUPS 1.6svn-r10390.
[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 *
eac3a0a0 6 * Copyright 2007-2011 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 &&
88f9aafc 377 _cups_strcasecmp(attr->name, "printer-name") == 0 &&
ef416fc2 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 */
12f89d24 902 char tempfile[1024] = ""; /* Temporary filename */
ef416fc2 903 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
ef416fc2 904
905
906 /*
907 * Range check input...
908 */
909
2e4ff8af 910 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
e07d4801 911 "bufsize=%d)", http, name, modtime,
ae71f5de 912 modtime ? (int)*modtime : 0, buffer, (int)bufsize));
ef416fc2 913
2e4ff8af
MS
914 if (!name)
915 {
4d301e69 916 _cupsSetError(IPP_INTERNAL_ERROR, _("No printer name"), 1);
2e4ff8af
MS
917 return (HTTP_NOT_ACCEPTABLE);
918 }
919
920 if (!modtime)
921 {
4d301e69 922 _cupsSetError(IPP_INTERNAL_ERROR, _("No modification time"), 1);
2e4ff8af
MS
923 return (HTTP_NOT_ACCEPTABLE);
924 }
925
926 if (!buffer || bufsize <= 1)
927 {
4d301e69 928 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad filename buffer"), 1);
2e4ff8af 929 return (HTTP_NOT_ACCEPTABLE);
ef416fc2 930 }
931
426c6a59
MS
932#ifndef WIN32
933 /*
934 * See if the PPD file is available locally...
935 */
936
937 if (!cg->servername[0])
938 cupsServer();
939
88f9aafc 940 if (!_cups_strcasecmp(cg->servername, "localhost"))
426c6a59
MS
941 {
942 char ppdname[1024]; /* PPD filename */
943 struct stat ppdinfo; /* PPD file information */
944
945
d1c13e16
MS
946 snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd", cg->cups_serverroot,
947 name);
426c6a59
MS
948 if (!stat(ppdname, &ppdinfo))
949 {
950 /*
951 * OK, the file exists, use it!
952 */
953
954 if (buffer[0])
955 {
956 unlink(buffer);
957
958 if (symlink(ppdname, buffer) && errno != EEXIST)
959 {
960 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
961
962 return (HTTP_SERVER_ERROR);
963 }
964 }
965 else
966 {
967 int tries; /* Number of tries */
968 const char *tmpdir; /* TMPDIR environment variable */
969 struct timeval curtime; /* Current time */
970
971 /*
972 * Previously we put root temporary files in the default CUPS temporary
973 * directory under /var/spool/cups. However, since the scheduler cleans
974 * out temporary files there and runs independently of the user apps, we
975 * don't want to use it unless specifically told to by cupsd.
976 */
977
978 if ((tmpdir = getenv("TMPDIR")) == NULL)
979# ifdef __APPLE__
980 tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */
981# else
982 tmpdir = "/tmp";
983# endif /* __APPLE__ */
984
985 /*
986 * Make the temporary name using the specified directory...
987 */
988
989 tries = 0;
990
991 do
992 {
993 /*
994 * Get the current time of day...
995 */
996
997 gettimeofday(&curtime, NULL);
998
999 /*
1000 * Format a string using the hex time values...
1001 */
1002
1003 snprintf(buffer, bufsize, "%s/%08lx%05lx", tmpdir,
1004 (unsigned long)curtime.tv_sec,
1005 (unsigned long)curtime.tv_usec);
1006
1007 /*
1008 * Try to make a symlink...
1009 */
1010
1011 if (!symlink(ppdname, buffer))
1012 break;
1013
1014 tries ++;
1015 }
1016 while (tries < 1000);
1017
1018 if (tries >= 1000)
1019 {
1020 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
1021
1022 return (HTTP_SERVER_ERROR);
1023 }
1024 }
1025
d1c13e16 1026 if (*modtime >= ppdinfo.st_mtime)
426c6a59
MS
1027 return (HTTP_NOT_MODIFIED);
1028 else
1029 {
1030 *modtime = ppdinfo.st_mtime;
1031 return (HTTP_OK);
1032 }
1033 }
1034 }
1035#endif /* !WIN32 */
1036
ef416fc2 1037 /*
fa73b229 1038 * Try finding a printer URI for this printer...
ef416fc2 1039 */
1040
3d052e43 1041 if (!http)
a603edef
MS
1042 if ((http = _cupsConnect()) == NULL)
1043 return (HTTP_SERVICE_UNAVAILABLE);
3d052e43 1044
fa73b229 1045 if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port,
1046 resource, sizeof(resource), 0))
2e4ff8af 1047 return (HTTP_NOT_FOUND);
ef416fc2 1048
e07d4801 1049 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname,
1ff0402e 1050 port));
8ca02f3c 1051
ef416fc2 1052 /*
fa73b229 1053 * Remap local hostname to localhost...
1054 */
1055
757d2cad 1056 httpGetHostname(NULL, localhost, sizeof(localhost));
fa73b229 1057
e07d4801 1058 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost));
8ca02f3c 1059
88f9aafc 1060 if (!_cups_strcasecmp(localhost, hostname))
fa73b229 1061 strcpy(hostname, "localhost");
1062
1063 /*
ed486911 1064 * Get the hostname and port number we are connected to...
ef416fc2 1065 */
1066
ed486911 1067 httpGetHostname(http, http_hostname, sizeof(http_hostname));
1ff0402e 1068 http_port = _httpAddrPort(http->hostaddr);
ed486911 1069
e07d4801 1070 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
1ff0402e 1071 http_hostname, http_port));
8ca02f3c 1072
ef416fc2 1073 /*
1074 * Reconnect to the correct server as needed...
1075 */
1076
88f9aafc 1077 if (!_cups_strcasecmp(http_hostname, hostname) && port == http_port)
ef416fc2 1078 http2 = http;
1079 else if ((http2 = httpConnectEncrypt(hostname, port,
1080 cupsEncryption())) == NULL)
1081 {
4d301e69 1082 DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
ef416fc2 1083
2e4ff8af 1084 return (HTTP_SERVICE_UNAVAILABLE);
ef416fc2 1085 }
1086
1087 /*
1088 * Get a temp file...
1089 */
1090
2e4ff8af
MS
1091 if (buffer[0])
1092 fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600);
1093 else
12f89d24 1094 fd = cupsTempFd(tempfile, sizeof(tempfile));
2e4ff8af
MS
1095
1096 if (fd < 0)
ef416fc2 1097 {
1098 /*
1099 * Can't open file; close the server connection and return NULL...
1100 */
1101
749b1e90 1102 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
ef416fc2 1103
1104 if (http2 != http)
1105 httpClose(http2);
1106
2e4ff8af 1107 return (HTTP_SERVER_ERROR);
ef416fc2 1108 }
1109
1110 /*
1111 * And send a request to the HTTP server...
1112 */
1113
fa73b229 1114 strlcat(resource, ".ppd", sizeof(resource));
ef416fc2 1115
2e4ff8af
MS
1116 if (*modtime > 0)
1117 httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE,
1118 httpGetDateString(*modtime));
1119
ef416fc2 1120 status = cupsGetFd(http2, resource, fd);
1121
1122 close(fd);
1123
ef416fc2 1124 /*
1125 * See if we actually got the file or an error...
1126 */
1127
2e4ff8af 1128 if (status == HTTP_OK)
12f89d24 1129 {
2e4ff8af 1130 *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE));
12f89d24
MS
1131
1132 if (tempfile[0])
1133 strlcpy(buffer, tempfile, bufsize);
1134 }
2e4ff8af 1135 else if (status != HTTP_NOT_MODIFIED)
ef416fc2 1136 {
e07d4801 1137 _cupsSetHTTPError(status);
ef416fc2 1138
12f89d24
MS
1139 if (buffer[0])
1140 unlink(buffer);
1141 else if (tempfile[0])
1142 unlink(tempfile);
ef416fc2 1143 }
12f89d24
MS
1144 else if (tempfile[0])
1145 unlink(tempfile);
ef416fc2 1146
91c84a35
MS
1147 if (http2 != http)
1148 httpClose(http2);
1149
ef416fc2 1150 /*
1151 * Return the PPD file...
1152 */
1153
e07d4801
MS
1154 DEBUG_printf(("1cupsGetPPD3: Returning status %d", status));
1155
2e4ff8af 1156 return (status);
ef416fc2 1157}
1158
1159
1160/*
1161 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1162 *
5a738aea 1163 * This function is deprecated - use @link cupsGetDests@ instead.
ef416fc2 1164 *
1165 * @deprecated@
1166 */
1167
1168int /* O - Number of printers */
1169cupsGetPrinters(char ***printers) /* O - Printers */
1170{
1171 int n; /* Number of printers */
1172 ipp_t *request, /* IPP Request */
1173 *response; /* IPP Response */
1174 ipp_attribute_t *attr; /* Current attribute */
ef416fc2 1175 char **temp; /* Temporary pointer */
3d052e43
MS
1176 http_t *http; /* Connection to server */
1177
ef416fc2 1178
3d052e43
MS
1179 /*
1180 * Range check input...
1181 */
ef416fc2 1182
3d052e43 1183 if (!printers)
ef416fc2 1184 {
749b1e90 1185 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
ef416fc2 1186
1187 return (0);
1188 }
1189
3d052e43
MS
1190 *printers = NULL;
1191
ef416fc2 1192 /*
1193 * Try to connect to the server...
1194 */
1195
3d052e43 1196 if ((http = _cupsConnect()) == NULL)
ef416fc2 1197 return (0);
ef416fc2 1198
1199 /*
1200 * Build a CUPS_GET_PRINTERS request, which requires the following
1201 * attributes:
1202 *
1203 * attributes-charset
1204 * attributes-natural-language
1205 * requested-attributes
1206 */
1207
2e4ff8af 1208 request = ippNewRequest(CUPS_GET_PRINTERS);
ef416fc2 1209
1210 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1211 "requested-attributes", NULL, "printer-name");
1212
1213 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1214 "printer-type", 0);
1215
1216 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1217 "printer-type-mask", CUPS_PRINTER_CLASS);
1218
1219 /*
1220 * Do the request and get back a response...
1221 */
1222
3d052e43 1223 n = 0;
ef416fc2 1224
3d052e43 1225 if ((response = cupsDoRequest(http, request, "/")) != NULL)
ef416fc2 1226 {
1227 for (attr = response->attrs; attr != NULL; attr = attr->next)
1228 if (attr->name != NULL &&
88f9aafc 1229 _cups_strcasecmp(attr->name, "printer-name") == 0 &&
ef416fc2 1230 attr->value_tag == IPP_TAG_NAME)
1231 {
1232 if (n == 0)
1233 temp = malloc(sizeof(char *));
1234 else
1235 temp = realloc(*printers, sizeof(char *) * (n + 1));
1236
1237 if (temp == NULL)
1238 {
1239 /*
1240 * Ran out of memory!
1241 */
1242
1243 while (n > 0)
1244 {
1245 n --;
1246 free((*printers)[n]);
1247 }
1248
1249 free(*printers);
1250 ippDelete(response);
1251 return (0);
1252 }
1253
1254 *printers = temp;
1255 temp[n] = strdup(attr->values[0].string.text);
1256 n ++;
1257 }
1258
1259 ippDelete(response);
1260 }
1261
1262 return (n);
1263}
1264
1265
b94498cf 1266/*
1267 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1268 *
1269 * This function returns the named PPD file from the server. The
5a738aea 1270 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
b94498cf 1271 * operation.
1272 *
1273 * You must remove (unlink) the PPD file when you are finished with
1274 * it. The PPD filename is stored in a static location that will be
5a738aea
MS
1275 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1276 * or @link cupsGetServerPPD@.
b94498cf 1277 *
426c6a59 1278 * @since CUPS 1.3/Mac OS X 10.5@
b94498cf 1279 */
1280
5a738aea 1281char * /* O - Name of PPD file or @code NULL@ on error */
568fa3fa 1282cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
b94498cf 1283 const char *name) /* I - Name of PPD file ("ppd-name") */
1284{
1285 int fd; /* PPD file descriptor */
1286 ipp_t *request; /* IPP request */
1287 _cups_globals_t *cg = _cupsGlobals();
1288 /* Pointer to library globals */
1289
1290
1291 /*
1292 * Range check input...
1293 */
1294
3d052e43 1295 if (!name)
b94498cf 1296 {
4d301e69 1297 _cupsSetError(IPP_INTERNAL_ERROR, _("No PPD name"), 1);
b94498cf 1298
1299 return (NULL);
1300 }
1301
3d052e43
MS
1302 if (!http)
1303 if ((http = _cupsConnect()) == NULL)
1304 return (NULL);
1305
b94498cf 1306 /*
1307 * Get a temp file...
1308 */
1309
1310 if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0)
1311 {
1312 /*
1313 * Can't open file; close the server connection and return NULL...
1314 */
1315
749b1e90 1316 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
b94498cf 1317
1318 return (NULL);
1319 }
1320
1321 /*
1322 * Get the PPD file...
1323 */
1324
1325 request = ippNewRequest(CUPS_GET_PPD);
1326 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL,
1327 name);
1328
1329 ippDelete(cupsDoIORequest(http, request, "/", -1, fd));
1330
1331 close(fd);
1332
1333 if (cupsLastError() != IPP_OK)
1334 {
1335 unlink(cg->ppd_filename);
1336 return (NULL);
1337 }
1338 else
1339 return (cg->ppd_filename);
1340}
1341
1342
ef416fc2 1343/*
1344 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1345 */
1346
5a738aea 1347int /* O - Job ID or 0 on error */
568fa3fa 1348cupsPrintFile(const char *name, /* I - Destination name */
ef416fc2 1349 const char *filename, /* I - File to print */
1350 const char *title, /* I - Title of job */
1351 int num_options,/* I - Number of options */
1352 cups_option_t *options) /* I - Options */
1353{
1354 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
e07d4801 1355 "title=\"%s\", num_options=%d, options=%p)",
ef416fc2 1356 name, filename, title, num_options, options));
1357
3d052e43
MS
1358 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title,
1359 num_options, options));
ef416fc2 1360}
1361
1362
1363/*
5a738aea
MS
1364 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1365 * server.
ef416fc2 1366 *
426c6a59 1367 * @since CUPS 1.1.21/Mac OS X 10.4@
ef416fc2 1368 */
1369
5a738aea 1370int /* O - Job ID or 0 on error */
3d052e43 1371cupsPrintFile2(
568fa3fa
MS
1372 http_t *http, /* I - Connection to server */
1373 const char *name, /* I - Destination name */
3d052e43
MS
1374 const char *filename, /* I - File to print */
1375 const char *title, /* I - Title of job */
1376 int num_options, /* I - Number of options */
1377 cups_option_t *options) /* I - Options */
ef416fc2 1378{
1379 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
e07d4801 1380 "title=\"%s\", num_options=%d, options=%p)",
ef416fc2 1381 http, name, filename, title, num_options, options));
1382
3d052e43
MS
1383 return (cupsPrintFiles2(http, name, 1, &filename, title, num_options,
1384 options));
ef416fc2 1385}
1386
1387
1388/*
fa73b229 1389 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1390 * default server.
ef416fc2 1391 */
1392
5a738aea 1393int /* O - Job ID or 0 on error */
3d052e43 1394cupsPrintFiles(
568fa3fa 1395 const char *name, /* I - Destination name */
3d052e43
MS
1396 int num_files, /* I - Number of files */
1397 const char **files, /* I - File(s) to print */
1398 const char *title, /* I - Title of job */
1399 int num_options, /* I - Number of options */
1400 cups_option_t *options) /* I - Options */
ef416fc2 1401{
ef416fc2 1402 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
e07d4801 1403 "files=%p, title=\"%s\", num_options=%d, options=%p)",
ef416fc2 1404 name, num_files, files, title, num_options, options));
1405
ef416fc2 1406 /*
1407 * Print the file(s)...
1408 */
1409
3d052e43 1410 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title,
ef416fc2 1411 num_options, options));
1412}
1413
1414
ef416fc2 1415/*
fa73b229 1416 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1417 * specified server.
ef416fc2 1418 *
426c6a59 1419 * @since CUPS 1.1.21/Mac OS X 10.4@
ef416fc2 1420 */
1421
5a738aea 1422int /* O - Job ID or 0 on error */
3d052e43 1423cupsPrintFiles2(
568fa3fa
MS
1424 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1425 const char *name, /* I - Destination name */
3d052e43
MS
1426 int num_files, /* I - Number of files */
1427 const char **files, /* I - File(s) to print */
1428 const char *title, /* I - Title of job */
1429 int num_options, /* I - Number of options */
1430 cups_option_t *options) /* I - Options */
ef416fc2 1431{
1432 int i; /* Looping var */
3d052e43
MS
1433 int job_id; /* New job ID */
1434 const char *docname; /* Basename of current filename */
1435 const char *format; /* Document format */
1436 cups_file_t *fp; /* Current file */
1437 char buffer[8192]; /* Copy buffer */
1438 ssize_t bytes; /* Bytes in buffer */
1439 http_status_t status; /* Status of write */
5a662dc0
MS
1440 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1441 ipp_status_t cancel_status; /* Status code to preserve */
1442 char *cancel_message; /* Error message to preserve */
ef416fc2 1443
1444
3d052e43 1445 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
e07d4801 1446 "files=%p, title=\"%s\", num_options=%d, options=%p)",
ef416fc2 1447 http, name, num_files, files, title, num_options, options));
1448
1449 /*
1450 * Range check input...
1451 */
1452
3d052e43 1453 if (!name || num_files < 1 || !files)
ef416fc2 1454 {
749b1e90 1455 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
ef416fc2 1456
1457 return (0);
1458 }
1459
1460 /*
3d052e43 1461 * Create the print job...
ef416fc2 1462 */
1463
3d052e43 1464 if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0)
ef416fc2 1465 return (0);
ef416fc2 1466
1467 /*
3d052e43 1468 * Send each of the files...
ef416fc2 1469 */
1470
3d052e43
MS
1471 if (cupsGetOption("raw", num_options, options))
1472 format = CUPS_FORMAT_RAW;
1473 else if ((format = cupsGetOption("document-format", num_options,
1474 options)) == NULL)
1475 format = CUPS_FORMAT_AUTO;
ef416fc2 1476
3d052e43
MS
1477 for (i = 0; i < num_files; i ++)
1478 {
1479 /*
1480 * Start the next file...
1481 */
ef416fc2 1482
3d052e43
MS
1483 if ((docname = strrchr(files[i], '/')) != NULL)
1484 docname ++;
1485 else
1486 docname = files[i];
ef416fc2 1487
3d052e43
MS
1488 if ((fp = cupsFileOpen(files[i], "rb")) == NULL)
1489 {
1490 /*
1491 * Unable to open print file, cancel the job and return...
1492 */
ef416fc2 1493
5a662dc0
MS
1494 _cupsSetError(IPP_DOCUMENT_ACCESS_ERROR, NULL, 0);
1495 goto cancel_job;
3d052e43 1496 }
ef416fc2 1497
ba55dc12
MS
1498 status = cupsStartDocument(http, name, job_id, docname, format,
1499 i == (num_files - 1));
ef416fc2 1500
ba55dc12
MS
1501 while (status == HTTP_CONTINUE &&
1502 (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
1503 status = cupsWriteRequestData(http, buffer, bytes);
ef416fc2 1504
3d052e43 1505 cupsFileClose(fp);
ef416fc2 1506
3d052e43 1507 if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK)
ef416fc2 1508 {
1509 /*
3d052e43 1510 * Unable to queue, cancel the job and return...
ef416fc2 1511 */
1512
5a662dc0 1513 goto cancel_job;
3d052e43
MS
1514 }
1515 }
ef416fc2 1516
3d052e43 1517 return (job_id);
5a662dc0
MS
1518
1519 /*
1520 * If we get here, something happened while sending the print job so we need
1521 * to cancel the job without setting the last error (since we need to preserve
1522 * the current error...
1523 */
1524
1525 cancel_job:
1526
1527 cancel_status = cg->last_error;
1528 cancel_message = cg->last_status_message ?
1529 _cupsStrRetain(cg->last_status_message) : NULL;
1530
1531 cupsCancelJob2(http, name, job_id, 0);
1532
1533 cg->last_error = cancel_status;
1534 cg->last_status_message = cancel_message;
1535
1536 return (0);
3d052e43 1537}
ef416fc2 1538
ef416fc2 1539
3d052e43
MS
1540/*
1541 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1542 *
5a738aea
MS
1543 * Use @link cupsWriteRequestData@ to write data for the document and
1544 * @link cupsFinishDocument@ to finish the document and get the submission status.
3d052e43 1545 *
5a738aea
MS
1546 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1547 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1548 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1549 * any supported MIME type string can be supplied.
3d052e43 1550 *
178cb736 1551 * @since CUPS 1.4/Mac OS X 10.6@
3d052e43 1552 */
ef416fc2 1553
3d052e43
MS
1554http_status_t /* O - HTTP status of request */
1555cupsStartDocument(
568fa3fa
MS
1556 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1557 const char *name, /* I - Destination name */
5a738aea 1558 int job_id, /* I - Job ID from @link cupsCreateJob@ */
3d052e43 1559 const char *docname, /* I - Name of document */
5a738aea 1560 const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */
3d052e43
MS
1561 int last_document) /* I - 1 for last document in job, 0 otherwise */
1562{
1563 char resource[1024], /* Resource for destinatio */
1564 printer_uri[1024]; /* Printer URI */
1565 ipp_t *request; /* Send-Document request */
1566 http_status_t status; /* HTTP status */
ef416fc2 1567
bd7854cb 1568
3d052e43
MS
1569 /*
1570 * Create a Send-Document request...
1571 */
bd7854cb 1572
3d052e43
MS
1573 if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL)
1574 {
749b1e90 1575 _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0);
82cc1f9a 1576 return (HTTP_ERROR);
3d052e43 1577 }
bd7854cb 1578
3d052e43
MS
1579 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
1580 NULL, "localhost", ippPort(), "/printers/%s", name);
1581 snprintf(resource, sizeof(resource), "/printers/%s", name);
ef416fc2 1582
3d052e43
MS
1583 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1584 NULL, printer_uri);
1585 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
1586 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1587 NULL, cupsUser());
1588 if (docname)
1589 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
1590 NULL, docname);
1591 if (format)
1592 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1593 "document-format", NULL, format);
1594 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document);
ef416fc2 1595
3d052e43
MS
1596 /*
1597 * Send and delete the request, then return the status...
1598 */
ef416fc2 1599
3d052e43 1600 status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE);
ef416fc2 1601
3d052e43 1602 ippDelete(request);
ef416fc2 1603
3d052e43 1604 return (status);
ef416fc2 1605}
1606
1607
fa73b229 1608/*
58dc1933
MS
1609 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
1610 * first printer in a class.
fa73b229 1611 */
1612
1613static int /* O - 1 on success, 0 on failure */
1614cups_get_printer_uri(
568fa3fa 1615 http_t *http, /* I - Connection to server */
fa73b229 1616 const char *name, /* I - Name of printer or class */
1617 char *host, /* I - Hostname buffer */
1618 int hostsize, /* I - Size of hostname buffer */
1619 int *port, /* O - Port number */
1620 char *resource, /* I - Resource buffer */
1621 int resourcesize, /* I - Size of resource buffer */
1622 int depth) /* I - Depth of query */
1623{
1624 int i; /* Looping var */
1625 int http_port; /* Port number */
1626 http_t *http2; /* Alternate HTTP connection */
1627 ipp_t *request, /* IPP request */
1628 *response; /* IPP response */
1629 ipp_attribute_t *attr; /* Current attribute */
1630 char uri[HTTP_MAX_URI], /* printer-uri attribute */
1631 scheme[HTTP_MAX_URI], /* Scheme name */
1632 username[HTTP_MAX_URI], /* Username:password */
ed486911 1633 classname[255], /* Temporary class name */
1634 http_hostname[HTTP_MAX_HOST];
1635 /* Hostname associated with connection */
fa73b229 1636 static const char * const requested_attrs[] =
1637 { /* Requested attributes */
58dc1933 1638 "member-uris",
fa73b229 1639 "printer-uri-supported",
58dc1933 1640 "printer-type"
fa73b229 1641 };
1642
1643
e07d4801
MS
1644 DEBUG_printf(("7cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
1645 "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)",
1646 http, name, host, hostsize, resource, resourcesize, depth));
fa73b229 1647
1648 /*
1649 * Setup the printer URI...
1650 */
1651
a4d04587 1652 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
1653 "localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
fa73b229 1654 {
4d301e69 1655 _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri"), 1);
fa73b229 1656
1657 *host = '\0';
1658 *resource = '\0';
1659
1660 return (0);
1661 }
1662
e07d4801 1663 DEBUG_printf(("9cups_get_printer_uri: printer-uri=\"%s\"", uri));
fa73b229 1664
1665 /*
ed486911 1666 * Get the hostname and port number we are connected to...
fa73b229 1667 */
1668
ed486911 1669 httpGetHostname(http, http_hostname, sizeof(http_hostname));
1ff0402e 1670 http_port = _httpAddrPort(http->hostaddr);
fa73b229 1671
1672 /*
1673 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1674 * attributes:
1675 *
1676 * attributes-charset
1677 * attributes-natural-language
1678 * printer-uri
1679 * requested-attributes
1680 */
1681
1682 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1683
1684 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1685 NULL, uri);
1686
1687 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1688 "requested-attributes",
1689 sizeof(requested_attrs) / sizeof(requested_attrs[0]),
1690 NULL, requested_attrs);
1691
1692 /*
1693 * Do the request and get back a response...
1694 */
1695
1696 if ((response = cupsDoRequest(http, request, "/")) != NULL)
1697 {
1698 if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
1699 {
1700 /*
1701 * Get the first actual printer name in the class...
1702 */
1703
1704 for (i = 0; i < attr->num_values; i ++)
1705 {
a4d04587 1706 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1707 scheme, sizeof(scheme), username, sizeof(username),
1708 host, hostsize, port, resource, resourcesize);
fa73b229 1709 if (!strncmp(resource, "/printers/", 10))
1710 {
1711 /*
1712 * Found a printer!
1713 */
1714
1715 ippDelete(response);
1716
1717 return (1);
1718 }
1719 }
1720
1721 /*
1722 * No printers in this class - try recursively looking for a printer,
1723 * but not more than 3 levels deep...
1724 */
1725
1726 if (depth < 3)
1727 {
1728 for (i = 0; i < attr->num_values; i ++)
1729 {
a4d04587 1730 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1731 scheme, sizeof(scheme), username, sizeof(username),
1732 host, hostsize, port, resource, resourcesize);
fa73b229 1733 if (!strncmp(resource, "/classes/", 9))
1734 {
1735 /*
1736 * Found a class! Connect to the right server...
1737 */
1738
88f9aafc 1739 if (!_cups_strcasecmp(http_hostname, host) && *port == http_port)
fa73b229 1740 http2 = http;
1741 else if ((http2 = httpConnectEncrypt(host, *port,
1742 cupsEncryption())) == NULL)
1743 {
4d301e69 1744 DEBUG_puts("8cups_get_printer_uri: Unable to connect to server");
fa73b229 1745
1746 continue;
1747 }
1748
1749 /*
1750 * Look up printers on that server...
1751 */
1752
1753 strlcpy(classname, resource + 9, sizeof(classname));
1754
1755 cups_get_printer_uri(http2, classname, host, hostsize, port,
1756 resource, resourcesize, depth + 1);
1757
1758 /*
1759 * Close the connection as needed...
1760 */
1761
1762 if (http2 != http)
1763 httpClose(http2);
1764
1765 if (*host)
1766 return (1);
1767 }
1768 }
1769 }
1770 }
1771 else if ((attr = ippFindAttribute(response, "printer-uri-supported",
1772 IPP_TAG_URI)) != NULL)
1773 {
58dc1933
MS
1774 httpSeparateURI(HTTP_URI_CODING_ALL,
1775 _httpResolveURI(attr->values[0].string.text, uri,
eac3a0a0
MS
1776 sizeof(uri), _HTTP_RESOLVE_DEFAULT,
1777 NULL, NULL),
a4d04587 1778 scheme, sizeof(scheme), username, sizeof(username),
1779 host, hostsize, port, resource, resourcesize);
fa73b229 1780 ippDelete(response);
1781
dd1abb6b
MS
1782 if (!strncmp(resource, "/classes/", 9))
1783 {
749b1e90 1784 _cupsSetError(IPP_INTERNAL_ERROR,
4d301e69 1785 _("No printer-uri found for class"), 1);
dd1abb6b
MS
1786
1787 *host = '\0';
1788 *resource = '\0';
1789
1790 return (0);
1791 }
1792
fa73b229 1793 return (1);
1794 }
1795
1796 ippDelete(response);
1797 }
1798
b86bc4cf 1799 if (cupsLastError() != IPP_NOT_FOUND)
4d301e69 1800 _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found"), 1);
b423cd4c 1801
fa73b229 1802 *host = '\0';
1803 *resource = '\0';
1804
1805 return (0);
1806}
1807
1808
ef416fc2 1809/*
b19ccc9e 1810 * End of "$Id: util.c 7850 2008-08-20 00:07:25Z mike $".
ef416fc2 1811 */