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