]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/util.c
80f36d153a047cc5c9189233ac879d0a584b91aa
[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-2010 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 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 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
903
904
905 /*
906 * Range check input...
907 */
908
909 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
910 "bufsize=%d)", http, name, modtime,
911 modtime ? (int)*modtime : 0, buffer, (int)bufsize));
912
913 if (!name)
914 {
915 _cupsSetError(IPP_INTERNAL_ERROR, _("No printer name"), 1);
916 return (HTTP_NOT_ACCEPTABLE);
917 }
918
919 if (!modtime)
920 {
921 _cupsSetError(IPP_INTERNAL_ERROR, _("No modification time"), 1);
922 return (HTTP_NOT_ACCEPTABLE);
923 }
924
925 if (!buffer || bufsize <= 1)
926 {
927 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad filename buffer"), 1);
928 return (HTTP_NOT_ACCEPTABLE);
929 }
930
931 #ifndef WIN32
932 /*
933 * See if the PPD file is available locally...
934 */
935
936 if (!cg->servername[0])
937 cupsServer();
938
939 if (!strcasecmp(cg->servername, "localhost"))
940 {
941 char ppdname[1024]; /* PPD filename */
942 struct stat ppdinfo; /* PPD file information */
943
944
945 snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd", cg->cups_serverroot,
946 name);
947 if (!stat(ppdname, &ppdinfo))
948 {
949 /*
950 * OK, the file exists, use it!
951 */
952
953 if (buffer[0])
954 {
955 unlink(buffer);
956
957 if (symlink(ppdname, buffer) && errno != EEXIST)
958 {
959 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
960
961 return (HTTP_SERVER_ERROR);
962 }
963 }
964 else
965 {
966 int tries; /* Number of tries */
967 const char *tmpdir; /* TMPDIR environment variable */
968 struct timeval curtime; /* Current time */
969
970 /*
971 * Previously we put root temporary files in the default CUPS temporary
972 * directory under /var/spool/cups. However, since the scheduler cleans
973 * out temporary files there and runs independently of the user apps, we
974 * don't want to use it unless specifically told to by cupsd.
975 */
976
977 if ((tmpdir = getenv("TMPDIR")) == NULL)
978 # ifdef __APPLE__
979 tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */
980 # else
981 tmpdir = "/tmp";
982 # endif /* __APPLE__ */
983
984 /*
985 * Make the temporary name using the specified directory...
986 */
987
988 tries = 0;
989
990 do
991 {
992 /*
993 * Get the current time of day...
994 */
995
996 gettimeofday(&curtime, NULL);
997
998 /*
999 * Format a string using the hex time values...
1000 */
1001
1002 snprintf(buffer, bufsize, "%s/%08lx%05lx", tmpdir,
1003 (unsigned long)curtime.tv_sec,
1004 (unsigned long)curtime.tv_usec);
1005
1006 /*
1007 * Try to make a symlink...
1008 */
1009
1010 if (!symlink(ppdname, buffer))
1011 break;
1012
1013 tries ++;
1014 }
1015 while (tries < 1000);
1016
1017 if (tries >= 1000)
1018 {
1019 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
1020
1021 return (HTTP_SERVER_ERROR);
1022 }
1023 }
1024
1025 if (*modtime >= ppdinfo.st_mtime)
1026 return (HTTP_NOT_MODIFIED);
1027 else
1028 {
1029 *modtime = ppdinfo.st_mtime;
1030 return (HTTP_OK);
1031 }
1032 }
1033 }
1034 #endif /* !WIN32 */
1035
1036 /*
1037 * Try finding a printer URI for this printer...
1038 */
1039
1040 if (!http)
1041 if ((http = _cupsConnect()) == NULL)
1042 return (HTTP_SERVICE_UNAVAILABLE);
1043
1044 if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port,
1045 resource, sizeof(resource), 0))
1046 return (HTTP_NOT_FOUND);
1047
1048 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname,
1049 port));
1050
1051 /*
1052 * Remap local hostname to localhost...
1053 */
1054
1055 httpGetHostname(NULL, localhost, sizeof(localhost));
1056
1057 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost));
1058
1059 if (!strcasecmp(localhost, hostname))
1060 strcpy(hostname, "localhost");
1061
1062 /*
1063 * Get the hostname and port number we are connected to...
1064 */
1065
1066 httpGetHostname(http, http_hostname, sizeof(http_hostname));
1067 http_port = _httpAddrPort(http->hostaddr);
1068
1069 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
1070 http_hostname, http_port));
1071
1072 /*
1073 * Reconnect to the correct server as needed...
1074 */
1075
1076 if (!strcasecmp(http_hostname, hostname) && port == http_port)
1077 http2 = http;
1078 else if ((http2 = httpConnectEncrypt(hostname, port,
1079 cupsEncryption())) == NULL)
1080 {
1081 DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
1082
1083 return (HTTP_SERVICE_UNAVAILABLE);
1084 }
1085
1086 /*
1087 * Get a temp file...
1088 */
1089
1090 if (buffer[0])
1091 fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600);
1092 else
1093 fd = cupsTempFd(buffer, bufsize);
1094
1095 if (fd < 0)
1096 {
1097 /*
1098 * Can't open file; close the server connection and return NULL...
1099 */
1100
1101 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
1102
1103 if (http2 != http)
1104 httpClose(http2);
1105
1106 return (HTTP_SERVER_ERROR);
1107 }
1108
1109 /*
1110 * And send a request to the HTTP server...
1111 */
1112
1113 strlcat(resource, ".ppd", sizeof(resource));
1114
1115 if (*modtime > 0)
1116 httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE,
1117 httpGetDateString(*modtime));
1118
1119 status = cupsGetFd(http2, resource, fd);
1120
1121 close(fd);
1122
1123 /*
1124 * See if we actually got the file or an error...
1125 */
1126
1127 if (status == HTTP_OK)
1128 *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE));
1129 else if (status != HTTP_NOT_MODIFIED)
1130 {
1131 _cupsSetHTTPError(status);
1132
1133 unlink(cg->ppd_filename);
1134 }
1135
1136 if (http2 != http)
1137 httpClose(http2);
1138
1139 /*
1140 * Return the PPD file...
1141 */
1142
1143 DEBUG_printf(("1cupsGetPPD3: Returning status %d", status));
1144
1145 return (status);
1146 }
1147
1148
1149 /*
1150 * 'cupsGetPrinters()' - Get a list of printers from the default server.
1151 *
1152 * This function is deprecated - use @link cupsGetDests@ instead.
1153 *
1154 * @deprecated@
1155 */
1156
1157 int /* O - Number of printers */
1158 cupsGetPrinters(char ***printers) /* O - Printers */
1159 {
1160 int n; /* Number of printers */
1161 ipp_t *request, /* IPP Request */
1162 *response; /* IPP Response */
1163 ipp_attribute_t *attr; /* Current attribute */
1164 char **temp; /* Temporary pointer */
1165 http_t *http; /* Connection to server */
1166
1167
1168 /*
1169 * Range check input...
1170 */
1171
1172 if (!printers)
1173 {
1174 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
1175
1176 return (0);
1177 }
1178
1179 *printers = NULL;
1180
1181 /*
1182 * Try to connect to the server...
1183 */
1184
1185 if ((http = _cupsConnect()) == NULL)
1186 return (0);
1187
1188 /*
1189 * Build a CUPS_GET_PRINTERS request, which requires the following
1190 * attributes:
1191 *
1192 * attributes-charset
1193 * attributes-natural-language
1194 * requested-attributes
1195 */
1196
1197 request = ippNewRequest(CUPS_GET_PRINTERS);
1198
1199 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1200 "requested-attributes", NULL, "printer-name");
1201
1202 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1203 "printer-type", 0);
1204
1205 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
1206 "printer-type-mask", CUPS_PRINTER_CLASS);
1207
1208 /*
1209 * Do the request and get back a response...
1210 */
1211
1212 n = 0;
1213
1214 if ((response = cupsDoRequest(http, request, "/")) != NULL)
1215 {
1216 for (attr = response->attrs; attr != NULL; attr = attr->next)
1217 if (attr->name != NULL &&
1218 strcasecmp(attr->name, "printer-name") == 0 &&
1219 attr->value_tag == IPP_TAG_NAME)
1220 {
1221 if (n == 0)
1222 temp = malloc(sizeof(char *));
1223 else
1224 temp = realloc(*printers, sizeof(char *) * (n + 1));
1225
1226 if (temp == NULL)
1227 {
1228 /*
1229 * Ran out of memory!
1230 */
1231
1232 while (n > 0)
1233 {
1234 n --;
1235 free((*printers)[n]);
1236 }
1237
1238 free(*printers);
1239 ippDelete(response);
1240 return (0);
1241 }
1242
1243 *printers = temp;
1244 temp[n] = strdup(attr->values[0].string.text);
1245 n ++;
1246 }
1247
1248 ippDelete(response);
1249 }
1250
1251 return (n);
1252 }
1253
1254
1255 /*
1256 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
1257 *
1258 * This function returns the named PPD file from the server. The
1259 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
1260 * operation.
1261 *
1262 * You must remove (unlink) the PPD file when you are finished with
1263 * it. The PPD filename is stored in a static location that will be
1264 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
1265 * or @link cupsGetServerPPD@.
1266 *
1267 * @since CUPS 1.3/Mac OS X 10.5@
1268 */
1269
1270 char * /* O - Name of PPD file or @code NULL@ on error */
1271 cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1272 const char *name) /* I - Name of PPD file ("ppd-name") */
1273 {
1274 int fd; /* PPD file descriptor */
1275 ipp_t *request; /* IPP request */
1276 _cups_globals_t *cg = _cupsGlobals();
1277 /* Pointer to library globals */
1278
1279
1280 /*
1281 * Range check input...
1282 */
1283
1284 if (!name)
1285 {
1286 _cupsSetError(IPP_INTERNAL_ERROR, _("No PPD name"), 1);
1287
1288 return (NULL);
1289 }
1290
1291 if (!http)
1292 if ((http = _cupsConnect()) == NULL)
1293 return (NULL);
1294
1295 /*
1296 * Get a temp file...
1297 */
1298
1299 if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0)
1300 {
1301 /*
1302 * Can't open file; close the server connection and return NULL...
1303 */
1304
1305 _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
1306
1307 return (NULL);
1308 }
1309
1310 /*
1311 * Get the PPD file...
1312 */
1313
1314 request = ippNewRequest(CUPS_GET_PPD);
1315 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL,
1316 name);
1317
1318 ippDelete(cupsDoIORequest(http, request, "/", -1, fd));
1319
1320 close(fd);
1321
1322 if (cupsLastError() != IPP_OK)
1323 {
1324 unlink(cg->ppd_filename);
1325 return (NULL);
1326 }
1327 else
1328 return (cg->ppd_filename);
1329 }
1330
1331
1332 /*
1333 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
1334 */
1335
1336 int /* O - Job ID or 0 on error */
1337 cupsPrintFile(const char *name, /* I - Destination name */
1338 const char *filename, /* I - File to print */
1339 const char *title, /* I - Title of job */
1340 int num_options,/* I - Number of options */
1341 cups_option_t *options) /* I - Options */
1342 {
1343 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
1344 "title=\"%s\", num_options=%d, options=%p)",
1345 name, filename, title, num_options, options));
1346
1347 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title,
1348 num_options, options));
1349 }
1350
1351
1352 /*
1353 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
1354 * server.
1355 *
1356 * @since CUPS 1.1.21/Mac OS X 10.4@
1357 */
1358
1359 int /* O - Job ID or 0 on error */
1360 cupsPrintFile2(
1361 http_t *http, /* I - Connection to server */
1362 const char *name, /* I - Destination name */
1363 const char *filename, /* I - File to print */
1364 const char *title, /* I - Title of job */
1365 int num_options, /* I - Number of options */
1366 cups_option_t *options) /* I - Options */
1367 {
1368 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
1369 "title=\"%s\", num_options=%d, options=%p)",
1370 http, name, filename, title, num_options, options));
1371
1372 return (cupsPrintFiles2(http, name, 1, &filename, title, num_options,
1373 options));
1374 }
1375
1376
1377 /*
1378 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
1379 * default server.
1380 */
1381
1382 int /* O - Job ID or 0 on error */
1383 cupsPrintFiles(
1384 const char *name, /* I - Destination name */
1385 int num_files, /* I - Number of files */
1386 const char **files, /* I - File(s) to print */
1387 const char *title, /* I - Title of job */
1388 int num_options, /* I - Number of options */
1389 cups_option_t *options) /* I - Options */
1390 {
1391 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
1392 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1393 name, num_files, files, title, num_options, options));
1394
1395 /*
1396 * Print the file(s)...
1397 */
1398
1399 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title,
1400 num_options, options));
1401 }
1402
1403
1404 /*
1405 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
1406 * specified server.
1407 *
1408 * @since CUPS 1.1.21/Mac OS X 10.4@
1409 */
1410
1411 int /* O - Job ID or 0 on error */
1412 cupsPrintFiles2(
1413 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1414 const char *name, /* I - Destination name */
1415 int num_files, /* I - Number of files */
1416 const char **files, /* I - File(s) to print */
1417 const char *title, /* I - Title of job */
1418 int num_options, /* I - Number of options */
1419 cups_option_t *options) /* I - Options */
1420 {
1421 int i; /* Looping var */
1422 int job_id; /* New job ID */
1423 const char *docname; /* Basename of current filename */
1424 const char *format; /* Document format */
1425 cups_file_t *fp; /* Current file */
1426 char buffer[8192]; /* Copy buffer */
1427 ssize_t bytes; /* Bytes in buffer */
1428 http_status_t status; /* Status of write */
1429 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
1430 ipp_status_t cancel_status; /* Status code to preserve */
1431 char *cancel_message; /* Error message to preserve */
1432
1433
1434 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
1435 "files=%p, title=\"%s\", num_options=%d, options=%p)",
1436 http, name, num_files, files, title, num_options, options));
1437
1438 /*
1439 * Range check input...
1440 */
1441
1442 if (!name || num_files < 1 || !files)
1443 {
1444 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
1445
1446 return (0);
1447 }
1448
1449 /*
1450 * Create the print job...
1451 */
1452
1453 if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0)
1454 return (0);
1455
1456 /*
1457 * Send each of the files...
1458 */
1459
1460 if (cupsGetOption("raw", num_options, options))
1461 format = CUPS_FORMAT_RAW;
1462 else if ((format = cupsGetOption("document-format", num_options,
1463 options)) == NULL)
1464 format = CUPS_FORMAT_AUTO;
1465
1466 for (i = 0; i < num_files; i ++)
1467 {
1468 /*
1469 * Start the next file...
1470 */
1471
1472 if ((docname = strrchr(files[i], '/')) != NULL)
1473 docname ++;
1474 else
1475 docname = files[i];
1476
1477 if ((fp = cupsFileOpen(files[i], "rb")) == NULL)
1478 {
1479 /*
1480 * Unable to open print file, cancel the job and return...
1481 */
1482
1483 _cupsSetError(IPP_DOCUMENT_ACCESS_ERROR, NULL, 0);
1484 goto cancel_job;
1485 }
1486
1487 status = cupsStartDocument(http, name, job_id, docname, format,
1488 i == (num_files - 1));
1489
1490 while (status == HTTP_CONTINUE &&
1491 (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
1492 status = cupsWriteRequestData(http, buffer, bytes);
1493
1494 cupsFileClose(fp);
1495
1496 if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK)
1497 {
1498 /*
1499 * Unable to queue, cancel the job and return...
1500 */
1501
1502 goto cancel_job;
1503 }
1504 }
1505
1506 return (job_id);
1507
1508 /*
1509 * If we get here, something happened while sending the print job so we need
1510 * to cancel the job without setting the last error (since we need to preserve
1511 * the current error...
1512 */
1513
1514 cancel_job:
1515
1516 cancel_status = cg->last_error;
1517 cancel_message = cg->last_status_message ?
1518 _cupsStrRetain(cg->last_status_message) : NULL;
1519
1520 cupsCancelJob2(http, name, job_id, 0);
1521
1522 cg->last_error = cancel_status;
1523 cg->last_status_message = cancel_message;
1524
1525 return (0);
1526 }
1527
1528
1529 /*
1530 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
1531 *
1532 * Use @link cupsWriteRequestData@ to write data for the document and
1533 * @link cupsFinishDocument@ to finish the document and get the submission status.
1534 *
1535 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
1536 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
1537 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
1538 * any supported MIME type string can be supplied.
1539 *
1540 * @since CUPS 1.4/Mac OS X 10.6@
1541 */
1542
1543 http_status_t /* O - HTTP status of request */
1544 cupsStartDocument(
1545 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
1546 const char *name, /* I - Destination name */
1547 int job_id, /* I - Job ID from @link cupsCreateJob@ */
1548 const char *docname, /* I - Name of document */
1549 const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */
1550 int last_document) /* I - 1 for last document in job, 0 otherwise */
1551 {
1552 char resource[1024], /* Resource for destinatio */
1553 printer_uri[1024]; /* Printer URI */
1554 ipp_t *request; /* Send-Document request */
1555 http_status_t status; /* HTTP status */
1556
1557
1558 /*
1559 * Create a Send-Document request...
1560 */
1561
1562 if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL)
1563 {
1564 _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0);
1565 return (0);
1566 }
1567
1568 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
1569 NULL, "localhost", ippPort(), "/printers/%s", name);
1570 snprintf(resource, sizeof(resource), "/printers/%s", name);
1571
1572 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1573 NULL, printer_uri);
1574 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
1575 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
1576 NULL, cupsUser());
1577 if (docname)
1578 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
1579 NULL, docname);
1580 if (format)
1581 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1582 "document-format", NULL, format);
1583 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document);
1584
1585 /*
1586 * Send and delete the request, then return the status...
1587 */
1588
1589 status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE);
1590
1591 ippDelete(request);
1592
1593 return (status);
1594 }
1595
1596
1597 /*
1598 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
1599 * first printer in a class.
1600 */
1601
1602 static int /* O - 1 on success, 0 on failure */
1603 cups_get_printer_uri(
1604 http_t *http, /* I - Connection to server */
1605 const char *name, /* I - Name of printer or class */
1606 char *host, /* I - Hostname buffer */
1607 int hostsize, /* I - Size of hostname buffer */
1608 int *port, /* O - Port number */
1609 char *resource, /* I - Resource buffer */
1610 int resourcesize, /* I - Size of resource buffer */
1611 int depth) /* I - Depth of query */
1612 {
1613 int i; /* Looping var */
1614 int http_port; /* Port number */
1615 http_t *http2; /* Alternate HTTP connection */
1616 ipp_t *request, /* IPP request */
1617 *response; /* IPP response */
1618 ipp_attribute_t *attr; /* Current attribute */
1619 char uri[HTTP_MAX_URI], /* printer-uri attribute */
1620 scheme[HTTP_MAX_URI], /* Scheme name */
1621 username[HTTP_MAX_URI], /* Username:password */
1622 classname[255], /* Temporary class name */
1623 http_hostname[HTTP_MAX_HOST];
1624 /* Hostname associated with connection */
1625 static const char * const requested_attrs[] =
1626 { /* Requested attributes */
1627 "member-uris",
1628 "printer-uri-supported",
1629 "printer-type"
1630 };
1631
1632
1633 DEBUG_printf(("7cups_get_printer_uri(http=%p, name=\"%s\", host=%p, "
1634 "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)",
1635 http, name, host, hostsize, resource, resourcesize, depth));
1636
1637 /*
1638 * Setup the printer URI...
1639 */
1640
1641 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
1642 "localhost", 0, "/printers/%s", name) != HTTP_URI_OK)
1643 {
1644 _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri"), 1);
1645
1646 *host = '\0';
1647 *resource = '\0';
1648
1649 return (0);
1650 }
1651
1652 DEBUG_printf(("9cups_get_printer_uri: printer-uri=\"%s\"", uri));
1653
1654 /*
1655 * Get the hostname and port number we are connected to...
1656 */
1657
1658 httpGetHostname(http, http_hostname, sizeof(http_hostname));
1659 http_port = _httpAddrPort(http->hostaddr);
1660
1661 /*
1662 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1663 * attributes:
1664 *
1665 * attributes-charset
1666 * attributes-natural-language
1667 * printer-uri
1668 * requested-attributes
1669 */
1670
1671 request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1672
1673 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1674 NULL, uri);
1675
1676 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1677 "requested-attributes",
1678 sizeof(requested_attrs) / sizeof(requested_attrs[0]),
1679 NULL, requested_attrs);
1680
1681 /*
1682 * Do the request and get back a response...
1683 */
1684
1685 if ((response = cupsDoRequest(http, request, "/")) != NULL)
1686 {
1687 if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
1688 {
1689 /*
1690 * Get the first actual printer name in the class...
1691 */
1692
1693 for (i = 0; i < attr->num_values; i ++)
1694 {
1695 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1696 scheme, sizeof(scheme), username, sizeof(username),
1697 host, hostsize, port, resource, resourcesize);
1698 if (!strncmp(resource, "/printers/", 10))
1699 {
1700 /*
1701 * Found a printer!
1702 */
1703
1704 ippDelete(response);
1705
1706 return (1);
1707 }
1708 }
1709
1710 /*
1711 * No printers in this class - try recursively looking for a printer,
1712 * but not more than 3 levels deep...
1713 */
1714
1715 if (depth < 3)
1716 {
1717 for (i = 0; i < attr->num_values; i ++)
1718 {
1719 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text,
1720 scheme, sizeof(scheme), username, sizeof(username),
1721 host, hostsize, port, resource, resourcesize);
1722 if (!strncmp(resource, "/classes/", 9))
1723 {
1724 /*
1725 * Found a class! Connect to the right server...
1726 */
1727
1728 if (!strcasecmp(http_hostname, host) && *port == http_port)
1729 http2 = http;
1730 else if ((http2 = httpConnectEncrypt(host, *port,
1731 cupsEncryption())) == NULL)
1732 {
1733 DEBUG_puts("8cups_get_printer_uri: Unable to connect to server");
1734
1735 continue;
1736 }
1737
1738 /*
1739 * Look up printers on that server...
1740 */
1741
1742 strlcpy(classname, resource + 9, sizeof(classname));
1743
1744 cups_get_printer_uri(http2, classname, host, hostsize, port,
1745 resource, resourcesize, depth + 1);
1746
1747 /*
1748 * Close the connection as needed...
1749 */
1750
1751 if (http2 != http)
1752 httpClose(http2);
1753
1754 if (*host)
1755 return (1);
1756 }
1757 }
1758 }
1759 }
1760 else if ((attr = ippFindAttribute(response, "printer-uri-supported",
1761 IPP_TAG_URI)) != NULL)
1762 {
1763 httpSeparateURI(HTTP_URI_CODING_ALL,
1764 _httpResolveURI(attr->values[0].string.text, uri,
1765 sizeof(uri), 0),
1766 scheme, sizeof(scheme), username, sizeof(username),
1767 host, hostsize, port, resource, resourcesize);
1768 ippDelete(response);
1769
1770 if (!strncmp(resource, "/classes/", 9))
1771 {
1772 _cupsSetError(IPP_INTERNAL_ERROR,
1773 _("No printer-uri found for class"), 1);
1774
1775 *host = '\0';
1776 *resource = '\0';
1777
1778 return (0);
1779 }
1780
1781 return (1);
1782 }
1783
1784 ippDelete(response);
1785 }
1786
1787 if (cupsLastError() != IPP_NOT_FOUND)
1788 _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found"), 1);
1789
1790 *host = '\0';
1791 *resource = '\0';
1792
1793 return (0);
1794 }
1795
1796
1797 /*
1798 * End of "$Id: util.c 7850 2008-08-20 00:07:25Z mike $".
1799 */