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