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