]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/util.c
Prep for 2.2.4 release.
[thirdparty/cups.git] / cups / util.c
CommitLineData
ef416fc2 1/*
7e86f2f6 2 * Printing utilities for CUPS.
ef416fc2 3 *
7536de1a 4 * Copyright 2007-2017 by Apple Inc.
7e86f2f6 5 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 6 *
7e86f2f6
MS
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
57b7b66b 11 * missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 12 *
7e86f2f6 13 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 14 */
15
16/*
17 * Include necessary headers...
18 */
19
71e16022 20#include "cups-private.h"
ef416fc2 21#include <fcntl.h>
22#include <sys/stat.h>
23#if defined(WIN32) || defined(__EMX__)
24# include <io.h>
25#else
26# include <unistd.h>
27#endif /* WIN32 || __EMX__ */
28
29
7536de1a
MS
30/*
31 * Enumeration data and callback...
32 */
33
34typedef struct _cups_createdata_s
35{
36 const char *name; /* Destination name */
37 cups_dest_t *dest; /* Matching destination */
38} _cups_createdata_t;
39
40static int cups_create_cb(_cups_createdata_t *data, unsigned flags, cups_dest_t *dest);
41
42
ef416fc2 43/*
44 * 'cupsCancelJob()' - Cancel a print job on the default server.
45 *
5a738aea
MS
46 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
47 * to cancel the current job on the named destination.
48 *
49 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
ef416fc2 50 * the cause of any failure.
53af7f21
MS
51 *
52 * @exclude all@
ef416fc2 53 */
54
55int /* O - 1 on success, 0 on failure */
56cupsCancelJob(const char *name, /* I - Name of printer or class */
568fa3fa 57 int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
ef416fc2 58{
5a738aea 59 return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0)
cb7f98ee 60 < IPP_STATUS_REDIRECTION_OTHER_SITE);
3d052e43
MS
61}
62
63
64/*
65 * 'cupsCancelJob2()' - Cancel or purge a print job.
66 *
67 * Canceled jobs remain in the job history while purged jobs are removed
68 * from the job history.
69 *
5a738aea
MS
70 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
71 * to cancel the current job on the named destination.
72 *
73 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
3d052e43
MS
74 * the cause of any failure.
75 *
53af7f21 76 * @since CUPS 1.4/macOS 10.6@ @exclude all@
3d052e43
MS
77 */
78
79ipp_status_t /* O - IPP status */
568fa3fa 80cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
5a738aea 81 const char *name, /* I - Name of printer or class */
568fa3fa 82 int job_id, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
5a738aea 83 int purge) /* I - 1 to purge, 0 to cancel */
3d052e43 84{
5a738aea 85 char uri[HTTP_MAX_URI]; /* Job/printer URI */
3d052e43 86 ipp_t *request; /* IPP request */
ef416fc2 87
88
89 /*
3d052e43 90 * Range check input...
ef416fc2 91 */
92
5a738aea 93 if (job_id < -1 || (!name && job_id == 0))
ef416fc2 94 {
cb7f98ee 95 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
ef416fc2 96 return (0);
97 }
98
99 /*
3d052e43 100 * Connect to the default server as needed...
ef416fc2 101 */
102
3d052e43
MS
103 if (!http)
104 if ((http = _cupsConnect()) == NULL)
cb7f98ee 105 return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE);
ef416fc2 106
107 /*
5a738aea 108 * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following
ef416fc2 109 * attributes:
110 *
111 * attributes-charset
112 * attributes-natural-language
5a738aea 113 * job-uri or printer-uri + job-id
3d052e43 114 * requesting-user-name
5a738aea 115 * [purge-job] or [purge-jobs]
ef416fc2 116 */
117
cb7f98ee 118 request = ippNewRequest(job_id < 0 ? IPP_OP_PURGE_JOBS : IPP_OP_CANCEL_JOB);
ef416fc2 119
5a738aea
MS
120 if (name)
121 {
122 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
123 "localhost", ippPort(), "/printers/%s", name);
124
125 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
126 uri);
127 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
128 job_id);
129 }
130 else if (job_id > 0)
131 {
132 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
133
134 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
135 }
ef416fc2 136
137 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
138 NULL, cupsUser());
5a738aea
MS
139
140 if (purge && job_id >= 0)
3d052e43 141 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1);
5a738aea
MS
142 else if (!purge && job_id < 0)
143 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0);
ef416fc2 144
145 /*
146 * Do the request...
147 */
148
3d052e43
MS
149 ippDelete(cupsDoRequest(http, request, "/jobs/"));
150
151 return (cupsLastError());
152}
153
154
155/*
568fa3fa 156 * 'cupsCreateJob()' - Create an empty job for streaming.
3d052e43 157 *
568fa3fa
MS
158 * Use this function when you want to stream print data using the
159 * @link cupsStartDocument@, @link cupsWriteRequestData@, and
160 * @link cupsFinishDocument@ functions. If you have one or more files to
161 * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function
162 * instead.
3d052e43 163 *
53af7f21 164 * @since CUPS 1.4/macOS 10.6@ @exclude all@
3d052e43
MS
165 */
166
167int /* O - Job ID or 0 on error */
168cupsCreateJob(
568fa3fa
MS
169 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
170 const char *name, /* I - Destination name */
3d052e43
MS
171 const char *title, /* I - Title of job */
172 int num_options, /* I - Number of options */
173 cups_option_t *options) /* I - Options */
174{
3d052e43 175 int job_id = 0; /* job-id value */
7536de1a
MS
176 ipp_status_t status; /* Create-Job status */
177 _cups_createdata_t data; /* Enumeration data */
178 cups_dinfo_t *info; /* Destination information */
3d052e43
MS
179
180
807315e6 181 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", num_options=%d, options=%p)", (void *)http, name, title, num_options, (void *)options));
3d052e43
MS
182
183 /*
184 * Range check input...
185 */
186
187 if (!name)
188 {
cb7f98ee 189 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
3d052e43
MS
190 return (0);
191 }
192
193 /*
7536de1a 194 * Lookup the destination...
3d052e43
MS
195 */
196
7536de1a
MS
197 data.name = name;
198 data.dest = NULL;
199
f50db552 200 cupsEnumDests(0, 1000, NULL, 0, 0, (cups_dest_cb_t)cups_create_cb, &data);
7536de1a
MS
201
202 if (!data.dest)
3d052e43 203 {
7536de1a
MS
204 DEBUG_puts("1cupsCreateJob: Destination not found.");
205 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0);
3d052e43
MS
206 return (0);
207 }
208
3d052e43 209 /*
7536de1a 210 * Query dest information and create the job...
3d052e43
MS
211 */
212
7536de1a
MS
213 DEBUG_puts("1cupsCreateJob: Querying destination info.");
214 if ((info = cupsCopyDestInfo(http, data.dest)) == NULL)
215 {
216 DEBUG_puts("1cupsCreateJob: Query failed.");
217 cupsFreeDests(1, data.dest);
218 return (0);
219 }
3d052e43 220
7536de1a
MS
221 status = cupsCreateDestJob(http, data.dest, info, &job_id, title, num_options, options);
222 DEBUG_printf(("1cupsCreateJob: cupsCreateDestJob returned %04x (%s)", status, ippErrorString(status)));
3d052e43 223
7536de1a
MS
224 cupsFreeDestInfo(info);
225 cupsFreeDests(1, data.dest);
3d052e43
MS
226
227 /*
7536de1a 228 * Return the job...
3d052e43
MS
229 */
230
7536de1a
MS
231 if (status >= IPP_STATUS_REDIRECTION_OTHER_SITE)
232 return (0);
233 else
234 return (job_id);
3d052e43
MS
235}
236
237
238/*
239 * 'cupsFinishDocument()' - Finish sending a document.
240 *
5a738aea
MS
241 * The document must have been started using @link cupsStartDocument@.
242 *
53af7f21 243 * @since CUPS 1.4/macOS 10.6@ @exclude all@
3d052e43
MS
244 */
245
246ipp_status_t /* O - Status of document submission */
568fa3fa
MS
247cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
248 const char *name) /* I - Destination name */
3d052e43
MS
249{
250 char resource[1024]; /* Printer resource */
251
252
253 snprintf(resource, sizeof(resource), "/printers/%s", name);
ef416fc2 254
3d052e43
MS
255 ippDelete(cupsGetResponse(http, resource));
256
257 return (cupsLastError());
ef416fc2 258}
259
260
ef416fc2 261/*
262 * 'cupsFreeJobs()' - Free memory used by job data.
263 */
264
265void
266cupsFreeJobs(int num_jobs, /* I - Number of jobs */
267 cups_job_t *jobs) /* I - Jobs */
268{
3d052e43
MS
269 int i; /* Looping var */
270 cups_job_t *job; /* Current job */
ef416fc2 271
272
3d052e43 273 if (num_jobs <= 0 || !jobs)
ef416fc2 274 return;
275
3d052e43 276 for (i = num_jobs, job = jobs; i > 0; i --, job ++)
ef416fc2 277 {
3d052e43
MS
278 _cupsStrFree(job->dest);
279 _cupsStrFree(job->user);
280 _cupsStrFree(job->format);
281 _cupsStrFree(job->title);
ef416fc2 282 }
283
284 free(jobs);
285}
286
287
288/*
289 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
290 *
240214ef
MS
291 * This function is deprecated and no longer returns a list of printer
292 * classes - use @link cupsGetDests@ instead.
ef416fc2 293 *
53af7f21 294 * @deprecated@ @exclude all@
ef416fc2 295 */
296
297int /* O - Number of classes */
298cupsGetClasses(char ***classes) /* O - Classes */
299{
240214ef
MS
300 if (classes)
301 *classes = NULL;
ef416fc2 302
240214ef 303 return (0);
ef416fc2 304}
305
306
307/*
308 * 'cupsGetDefault()' - Get the default printer or class for the default server.
309 *
310 * This function returns the default printer or class as defined by
311 * the LPDEST or PRINTER environment variables. If these environment
312 * variables are not set, the server default destination is returned.
5a738aea
MS
313 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
314 * functions to get the user-defined default printer, as this function does
315 * not support the lpoptions-defined default printer.
53af7f21
MS
316 *
317 * @exclude all@
ef416fc2 318 */
319
5a738aea 320const char * /* O - Default printer or @code NULL@ */
ef416fc2 321cupsGetDefault(void)
322{
ef416fc2 323 /*
324 * Return the default printer...
325 */
326
3d052e43 327 return (cupsGetDefault2(CUPS_HTTP_DEFAULT));
ef416fc2 328}
329
330
331/*
332 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
333 *
334 * This function returns the default printer or class as defined by
335 * the LPDEST or PRINTER environment variables. If these environment
336 * variables are not set, the server default destination is returned.
5a738aea
MS
337 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
338 * functions to get the user-defined default printer, as this function does
339 * not support the lpoptions-defined default printer.
ef416fc2 340 *
53af7f21 341 * @since CUPS 1.1.21/macOS 10.4@ @exclude all@
ef416fc2 342 */
343
5a738aea 344const char * /* O - Default printer or @code NULL@ */
568fa3fa 345cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ef416fc2 346{
347 ipp_t *request, /* IPP Request */
348 *response; /* IPP Response */
349 ipp_attribute_t *attr; /* Current attribute */
ef416fc2 350 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
351
352
353 /*
38e73f87 354 * See if we have a user default printer set...
ef416fc2 355 */
356
38e73f87
MS
357 if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer)))
358 return (cg->def_printer);
ef416fc2 359
360 /*
3d052e43 361 * Connect to the server as needed...
ef416fc2 362 */
363
364 if (!http)
3d052e43
MS
365 if ((http = _cupsConnect()) == NULL)
366 return (NULL);
ef416fc2 367
368 /*
369 * Build a CUPS_GET_DEFAULT request, which requires the following
370 * attributes:
371 *
372 * attributes-charset
373 * attributes-natural-language
374 */
375
cb7f98ee 376 request = ippNewRequest(IPP_OP_CUPS_GET_DEFAULT);
ef416fc2 377
378 /*
379 * Do the request and get back a response...
380 */
381
382 if ((response = cupsDoRequest(http, request, "/")) != NULL)
383 {
3d052e43
MS
384 if ((attr = ippFindAttribute(response, "printer-name",
385 IPP_TAG_NAME)) != NULL)
ef416fc2 386 {
3d052e43
MS
387 strlcpy(cg->def_printer, attr->values[0].string.text,
388 sizeof(cg->def_printer));
ef416fc2 389 ippDelete(response);
390 return (cg->def_printer);
391 }
392
393 ippDelete(response);
394 }
395
396 return (NULL);
397}
398
399
400/*
401 * 'cupsGetJobs()' - Get the jobs from the default server.
5a738aea
MS
402 *
403 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
404 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
405 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
406 * jobs that are stopped, canceled, aborted, or completed.
53af7f21
MS
407 *
408 * @exclude all@
ef416fc2 409 */
410
411int /* O - Number of jobs */
412cupsGetJobs(cups_job_t **jobs, /* O - Job data */
568fa3fa 413 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
ecdc0628 414 int myjobs, /* I - 0 = all users, 1 = mine */
5a738aea 415 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
ef416fc2 416{
ef416fc2 417 /*
418 * Return the jobs...
419 */
420
5a738aea 421 return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs));
ef416fc2 422}
423
424
425
426/*
427 * 'cupsGetJobs2()' - Get the jobs from the specified server.
428 *
5a738aea
MS
429 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
430 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
431 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
432 * jobs that are stopped, canceled, aborted, or completed.
433 *
8072030b 434 * @since CUPS 1.1.21/macOS 10.4@
ef416fc2 435 */
436
437int /* O - Number of jobs */
568fa3fa 438cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ef416fc2 439 cups_job_t **jobs, /* O - Job data */
568fa3fa 440 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
ecdc0628 441 int myjobs, /* I - 0 = all users, 1 = mine */
5a738aea 442 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
ef416fc2 443{
444 int n; /* Number of jobs */
445 ipp_t *request, /* IPP Request */
446 *response; /* IPP Response */
447 ipp_attribute_t *attr; /* Current attribute */
ef416fc2 448 cups_job_t *temp; /* Temporary pointer */
449 int id, /* job-id */
450 priority, /* job-priority */
451 size; /* job-k-octets */
452 ipp_jstate_t state; /* job-state */
453 time_t completed_time, /* time-at-completed */
454 creation_time, /* time-at-creation */
455 processing_time; /* time-at-processing */
456 const char *dest, /* job-printer-uri */
457 *format, /* document-format */
458 *title, /* job-name */
459 *user; /* job-originating-user-name */
460 char uri[HTTP_MAX_URI]; /* URI for jobs */
461 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
462 static const char * const attrs[] = /* Requested attributes */
463 {
5a662dc0 464 "document-format",
ef416fc2 465 "job-id",
ef416fc2 466 "job-k-octets",
5a662dc0
MS
467 "job-name",
468 "job-originating-user-name",
469 "job-printer-uri",
470 "job-priority",
ef416fc2 471 "job-state",
472 "time-at-completed",
473 "time-at-creation",
5a662dc0 474 "time-at-processing"
ef416fc2 475 };
476
477
478 /*
479 * Range check input...
480 */
481
3d052e43 482 if (!jobs)
ef416fc2 483 {
cb7f98ee 484 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
ef416fc2 485
486 return (-1);
487 }
488
489 /*
490 * Get the right URI...
491 */
492
5a738aea 493 if (name)
ef416fc2 494 {
a4d04587 495 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
cb7f98ee
MS
496 "localhost", 0, "/printers/%s",
497 name) < HTTP_URI_STATUS_OK)
ef416fc2 498 {
cb7f98ee
MS
499 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
500 _("Unable to create printer-uri"), 1);
ef416fc2 501
502 return (-1);
503 }
504 }
505 else
5a9febac 506 strlcpy(uri, "ipp://localhost/", sizeof(uri));
ef416fc2 507
3d052e43
MS
508 if (!http)
509 if ((http = _cupsConnect()) == NULL)
510 return (-1);
ef416fc2 511
512 /*
513 * Build an IPP_GET_JOBS request, which requires the following
514 * attributes:
515 *
516 * attributes-charset
517 * attributes-natural-language
518 * printer-uri
519 * requesting-user-name
520 * which-jobs
521 * my-jobs
522 * requested-attributes
523 */
524
cb7f98ee 525 request = ippNewRequest(IPP_OP_GET_JOBS);
ef416fc2 526
527 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
528 "printer-uri", NULL, uri);
529
530 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
531 "requesting-user-name", NULL, cupsUser());
532
533 if (myjobs)
534 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
535
5a738aea 536 if (whichjobs == CUPS_WHICHJOBS_COMPLETED)
ef416fc2 537 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
538 "which-jobs", NULL, "completed");
5a738aea 539 else if (whichjobs == CUPS_WHICHJOBS_ALL)
ecdc0628 540 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
541 "which-jobs", NULL, "all");
ef416fc2 542
543 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
544 "requested-attributes", sizeof(attrs) / sizeof(attrs[0]),
545 NULL, attrs);
546
547 /*
548 * Do the request and get back a response...
549 */
550
551 n = 0;
552 *jobs = NULL;
553
554 if ((response = cupsDoRequest(http, request, "/")) != NULL)
555 {
3d052e43 556 for (attr = response->attrs; attr; attr = attr->next)
ef416fc2 557 {
558 /*
559 * Skip leading attributes until we hit a job...
560 */
561
3d052e43 562 while (attr && attr->group_tag != IPP_TAG_JOB)
ef416fc2 563 attr = attr->next;
564
3d052e43 565 if (!attr)
ef416fc2 566 break;
567
568 /*
569 * Pull the needed attributes from this job...
570 */
571
572 id = 0;
573 size = 0;
574 priority = 50;
cb7f98ee 575 state = IPP_JSTATE_PENDING;
ef416fc2 576 user = "unknown";
577 dest = NULL;
578 format = "application/octet-stream";
579 title = "untitled";
580 creation_time = 0;
581 completed_time = 0;
582 processing_time = 0;
583
3d052e43 584 while (attr && attr->group_tag == IPP_TAG_JOB)
ef416fc2 585 {
3d052e43 586 if (!strcmp(attr->name, "job-id") &&
ef416fc2 587 attr->value_tag == IPP_TAG_INTEGER)
588 id = attr->values[0].integer;
3d052e43 589 else if (!strcmp(attr->name, "job-state") &&
ef416fc2 590 attr->value_tag == IPP_TAG_ENUM)
591 state = (ipp_jstate_t)attr->values[0].integer;
3d052e43 592 else if (!strcmp(attr->name, "job-priority") &&
ef416fc2 593 attr->value_tag == IPP_TAG_INTEGER)
594 priority = attr->values[0].integer;
3d052e43 595 else if (!strcmp(attr->name, "job-k-octets") &&
ef416fc2 596 attr->value_tag == IPP_TAG_INTEGER)
597 size = attr->values[0].integer;
3d052e43 598 else if (!strcmp(attr->name, "time-at-completed") &&
ef416fc2 599 attr->value_tag == IPP_TAG_INTEGER)
600 completed_time = attr->values[0].integer;
3d052e43 601 else if (!strcmp(attr->name, "time-at-creation") &&
ef416fc2 602 attr->value_tag == IPP_TAG_INTEGER)
603 creation_time = attr->values[0].integer;
3d052e43 604 else if (!strcmp(attr->name, "time-at-processing") &&
ef416fc2 605 attr->value_tag == IPP_TAG_INTEGER)
606 processing_time = attr->values[0].integer;
3d052e43 607 else if (!strcmp(attr->name, "job-printer-uri") &&
ef416fc2 608 attr->value_tag == IPP_TAG_URI)
609 {
610 if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
611 dest ++;
612 }
3d052e43 613 else if (!strcmp(attr->name, "job-originating-user-name") &&
ef416fc2 614 attr->value_tag == IPP_TAG_NAME)
615 user = attr->values[0].string.text;
3d052e43 616 else if (!strcmp(attr->name, "document-format") &&
ef416fc2 617 attr->value_tag == IPP_TAG_MIMETYPE)
618 format = attr->values[0].string.text;
3d052e43 619 else if (!strcmp(attr->name, "job-name") &&
ef416fc2 620 (attr->value_tag == IPP_TAG_TEXT ||
621 attr->value_tag == IPP_TAG_NAME))
622 title = attr->values[0].string.text;
623
624 attr = attr->next;
625 }
626
627 /*
628 * See if we have everything needed...
629 */
630
3d052e43 631 if (!dest || !id)
ef416fc2 632 {
3d052e43 633 if (!attr)
ef416fc2 634 break;
635 else
636 continue;
637 }
638
639 /*
640 * Allocate memory for the job...
641 */
642
643 if (n == 0)
644 temp = malloc(sizeof(cups_job_t));
645 else
7e86f2f6 646 temp = realloc(*jobs, sizeof(cups_job_t) * (size_t)(n + 1));
ef416fc2 647
3d052e43 648 if (!temp)
ef416fc2 649 {
650 /*
651 * Ran out of memory!
652 */
653
cb7f98ee 654 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
3d052e43 655
ef416fc2 656 cupsFreeJobs(n, *jobs);
657 *jobs = NULL;
658
659 ippDelete(response);
3d052e43
MS
660
661 return (-1);
ef416fc2 662 }
663
664 *jobs = temp;
665 temp += n;
666 n ++;
667
668 /*
669 * Copy the data over...
670 */
671
3d052e43
MS
672 temp->dest = _cupsStrAlloc(dest);
673 temp->user = _cupsStrAlloc(user);
674 temp->format = _cupsStrAlloc(format);
675 temp->title = _cupsStrAlloc(title);
ef416fc2 676 temp->id = id;
677 temp->priority = priority;
678 temp->state = state;
679 temp->size = size;
680 temp->completed_time = completed_time;
681 temp->creation_time = creation_time;
682 temp->processing_time = processing_time;
683
3d052e43 684 if (!attr)
ef416fc2 685 break;
686 }
687
688 ippDelete(response);
689 }
690
cb7f98ee 691 if (n == 0 && cg->last_error >= IPP_STATUS_ERROR_BAD_REQUEST)
ef416fc2 692 return (-1);
693 else
694 return (n);
695}
696
697
ef416fc2 698/*
699 * 'cupsGetPrinters()' - Get a list of printers from the default server.
700 *
240214ef
MS
701 * This function is deprecated and no longer returns a list of printers - use
702 * @link cupsGetDests@ instead.
ef416fc2 703 *
53af7f21 704 * @deprecated@ @exclude all@
ef416fc2 705 */
706
707int /* O - Number of printers */
708cupsGetPrinters(char ***printers) /* O - Printers */
709{
240214ef
MS
710 if (printers)
711 *printers = NULL;
ef416fc2 712
240214ef 713 return (0);
ef416fc2 714}
715
716
ef416fc2 717/*
718 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
53af7f21
MS
719 *
720 * @exclude all@
ef416fc2 721 */
722
5a738aea 723int /* O - Job ID or 0 on error */
568fa3fa 724cupsPrintFile(const char *name, /* I - Destination name */
ef416fc2 725 const char *filename, /* I - File to print */
726 const char *title, /* I - Title of job */
727 int num_options,/* I - Number of options */
728 cups_option_t *options) /* I - Options */
729{
807315e6 730 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", title=\"%s\", num_options=%d, options=%p)", name, filename, title, num_options, (void *)options));
ef416fc2 731
3d052e43
MS
732 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title,
733 num_options, options));
ef416fc2 734}
735
736
737/*
5a738aea
MS
738 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
739 * server.
ef416fc2 740 *
53af7f21 741 * @since CUPS 1.1.21/macOS 10.4@ @exclude all@
ef416fc2 742 */
743
5a738aea 744int /* O - Job ID or 0 on error */
3d052e43 745cupsPrintFile2(
568fa3fa
MS
746 http_t *http, /* I - Connection to server */
747 const char *name, /* I - Destination name */
3d052e43
MS
748 const char *filename, /* I - File to print */
749 const char *title, /* I - Title of job */
750 int num_options, /* I - Number of options */
751 cups_option_t *options) /* I - Options */
ef416fc2 752{
807315e6 753 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", title=\"%s\", num_options=%d, options=%p)", (void *)http, name, filename, title, num_options, (void *)options));
ef416fc2 754
3d052e43
MS
755 return (cupsPrintFiles2(http, name, 1, &filename, title, num_options,
756 options));
ef416fc2 757}
758
759
760/*
fa73b229 761 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
762 * default server.
53af7f21
MS
763 *
764 * @exclude all@
ef416fc2 765 */
766
5a738aea 767int /* O - Job ID or 0 on error */
3d052e43 768cupsPrintFiles(
568fa3fa 769 const char *name, /* I - Destination name */
3d052e43
MS
770 int num_files, /* I - Number of files */
771 const char **files, /* I - File(s) to print */
772 const char *title, /* I - Title of job */
773 int num_options, /* I - Number of options */
774 cups_option_t *options) /* I - Options */
ef416fc2 775{
807315e6 776 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, files=%p, title=\"%s\", num_options=%d, options=%p)", name, num_files, (void *)files, title, num_options, (void *)options));
ef416fc2 777
ef416fc2 778 /*
779 * Print the file(s)...
780 */
781
3d052e43 782 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title,
ef416fc2 783 num_options, options));
784}
785
786
ef416fc2 787/*
fa73b229 788 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
789 * specified server.
ef416fc2 790 *
53af7f21 791 * @since CUPS 1.1.21/macOS 10.4@ @exclude all@
ef416fc2 792 */
793
5a738aea 794int /* O - Job ID or 0 on error */
3d052e43 795cupsPrintFiles2(
568fa3fa
MS
796 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
797 const char *name, /* I - Destination name */
3d052e43
MS
798 int num_files, /* I - Number of files */
799 const char **files, /* I - File(s) to print */
800 const char *title, /* I - Title of job */
801 int num_options, /* I - Number of options */
802 cups_option_t *options) /* I - Options */
ef416fc2 803{
804 int i; /* Looping var */
3d052e43
MS
805 int job_id; /* New job ID */
806 const char *docname; /* Basename of current filename */
807 const char *format; /* Document format */
808 cups_file_t *fp; /* Current file */
809 char buffer[8192]; /* Copy buffer */
810 ssize_t bytes; /* Bytes in buffer */
811 http_status_t status; /* Status of write */
5a662dc0
MS
812 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
813 ipp_status_t cancel_status; /* Status code to preserve */
814 char *cancel_message; /* Error message to preserve */
ef416fc2 815
816
807315e6 817 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, files=%p, title=\"%s\", num_options=%d, options=%p)", (void *)http, name, num_files, (void *)files, title, num_options, (void *)options));
ef416fc2 818
819 /*
820 * Range check input...
821 */
822
3d052e43 823 if (!name || num_files < 1 || !files)
ef416fc2 824 {
cb7f98ee 825 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
ef416fc2 826
827 return (0);
828 }
829
830 /*
3d052e43 831 * Create the print job...
ef416fc2 832 */
833
3d052e43 834 if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0)
ef416fc2 835 return (0);
ef416fc2 836
837 /*
3d052e43 838 * Send each of the files...
ef416fc2 839 */
840
3d052e43
MS
841 if (cupsGetOption("raw", num_options, options))
842 format = CUPS_FORMAT_RAW;
843 else if ((format = cupsGetOption("document-format", num_options,
844 options)) == NULL)
845 format = CUPS_FORMAT_AUTO;
ef416fc2 846
3d052e43
MS
847 for (i = 0; i < num_files; i ++)
848 {
849 /*
850 * Start the next file...
851 */
ef416fc2 852
3d052e43
MS
853 if ((docname = strrchr(files[i], '/')) != NULL)
854 docname ++;
855 else
856 docname = files[i];
ef416fc2 857
3d052e43
MS
858 if ((fp = cupsFileOpen(files[i], "rb")) == NULL)
859 {
860 /*
861 * Unable to open print file, cancel the job and return...
862 */
ef416fc2 863
cb7f98ee 864 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_ACCESS, NULL, 0);
5a662dc0 865 goto cancel_job;
3d052e43 866 }
ef416fc2 867
ba55dc12
MS
868 status = cupsStartDocument(http, name, job_id, docname, format,
869 i == (num_files - 1));
ef416fc2 870
cb7f98ee 871 while (status == HTTP_STATUS_CONTINUE &&
ba55dc12 872 (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
7e86f2f6 873 status = cupsWriteRequestData(http, buffer, (size_t)bytes);
ef416fc2 874
3d052e43 875 cupsFileClose(fp);
ef416fc2 876
cb7f98ee 877 if (status != HTTP_STATUS_CONTINUE || cupsFinishDocument(http, name) != IPP_STATUS_OK)
ef416fc2 878 {
879 /*
3d052e43 880 * Unable to queue, cancel the job and return...
ef416fc2 881 */
882
5a662dc0 883 goto cancel_job;
3d052e43
MS
884 }
885 }
ef416fc2 886
3d052e43 887 return (job_id);
5a662dc0
MS
888
889 /*
890 * If we get here, something happened while sending the print job so we need
891 * to cancel the job without setting the last error (since we need to preserve
892 * the current error...
893 */
894
895 cancel_job:
896
897 cancel_status = cg->last_error;
898 cancel_message = cg->last_status_message ?
899 _cupsStrRetain(cg->last_status_message) : NULL;
900
901 cupsCancelJob2(http, name, job_id, 0);
902
903 cg->last_error = cancel_status;
904 cg->last_status_message = cancel_message;
905
906 return (0);
3d052e43 907}
ef416fc2 908
ef416fc2 909
3d052e43
MS
910/*
911 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
912 *
5a738aea
MS
913 * Use @link cupsWriteRequestData@ to write data for the document and
914 * @link cupsFinishDocument@ to finish the document and get the submission status.
3d052e43 915 *
5a738aea
MS
916 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
917 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
918 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
919 * any supported MIME type string can be supplied.
3d052e43 920 *
53af7f21 921 * @since CUPS 1.4/macOS 10.6@ @exclude all@
3d052e43 922 */
ef416fc2 923
3d052e43
MS
924http_status_t /* O - HTTP status of request */
925cupsStartDocument(
568fa3fa
MS
926 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
927 const char *name, /* I - Destination name */
5a738aea 928 int job_id, /* I - Job ID from @link cupsCreateJob@ */
3d052e43 929 const char *docname, /* I - Name of document */
5a738aea 930 const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */
3d052e43
MS
931 int last_document) /* I - 1 for last document in job, 0 otherwise */
932{
933 char resource[1024], /* Resource for destinatio */
934 printer_uri[1024]; /* Printer URI */
935 ipp_t *request; /* Send-Document request */
936 http_status_t status; /* HTTP status */
ef416fc2 937
bd7854cb 938
3d052e43
MS
939 /*
940 * Create a Send-Document request...
941 */
bd7854cb 942
cb7f98ee 943 if ((request = ippNewRequest(IPP_OP_SEND_DOCUMENT)) == NULL)
3d052e43 944 {
cb7f98ee
MS
945 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0);
946 return (HTTP_STATUS_ERROR);
3d052e43 947 }
bd7854cb 948
3d052e43
MS
949 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
950 NULL, "localhost", ippPort(), "/printers/%s", name);
951 snprintf(resource, sizeof(resource), "/printers/%s", name);
ef416fc2 952
3d052e43
MS
953 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
954 NULL, printer_uri);
955 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
956 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
957 NULL, cupsUser());
958 if (docname)
959 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
960 NULL, docname);
961 if (format)
962 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
963 "document-format", NULL, format);
7e86f2f6 964 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", (char)last_document);
ef416fc2 965
3d052e43
MS
966 /*
967 * Send and delete the request, then return the status...
968 */
ef416fc2 969
3d052e43 970 status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE);
ef416fc2 971
3d052e43 972 ippDelete(request);
ef416fc2 973
3d052e43 974 return (status);
ef416fc2 975}
7536de1a
MS
976
977
978/*
979 * 'cups_create_cb()' - Find the destination for printing.
980 */
981
982static int /* O - 0 on match */
983cups_create_cb(
984 _cups_createdata_t *data, /* I - Data from cupsCreateJob call */
985 unsigned flags, /* I - Enumeration flags */
986 cups_dest_t *dest) /* I - Destination */
987{
2a75f21b 988 DEBUG_printf(("2cups_create_cb(data=%p(%s), flags=%08x, dest=%p(%s))", (void *)data, data->name, flags, (void *)dest, dest->name));
7536de1a
MS
989
990 (void)flags;
991
992 if (dest->instance || strcasecmp(data->name, dest->name))
993 return (1);
994
995 cupsCopyDest(dest, 0, &data->dest);
996
997 return (0);
998}