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