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