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