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