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