]> git.pld-linux.org Git - packages/cups.git/blob - ipp-revert-1.4.patch
- use new functions
[packages/cups.git] / ipp-revert-1.4.patch
1 Index: branch-1.5/backend/ipp.c
2 ===================================================================
3 --- branch-1.5/backend/ipp.c    (.../branch-1.5/backend/ipp.c)  (revision 10232)
4 +++ branch-1.4/backend/ipp.c    (.../branch-1.4/backend/ipp.c)  (revision 10232)
5 @@ -1,9 +1,9 @@
6  /*
7   * "$Id$"
8   *
9 - *   IPP backend for CUPS.
10 + *   IPP backend for the Common UNIX Printing System (CUPS).
11   *
12 - *   Copyright 2007-2011 by Apple Inc.
13 + *   Copyright 2007-2010 by Apple Inc.
14   *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
15   *
16   *   These coded instructions, statements, and computer programs are the
17 @@ -18,16 +18,14 @@
18   *
19   *   main()                 - Send a file to the printer or server.
20   *   cancel_job()           - Cancel a print job.
21 - *   check_printer_state()  - Check the printer state.
22 - *   compress_files()       - Compress print files.
23 - *   monitor_printer()      - Monitor the printer state.
24 - *   new_request()          - Create a new print creation or validation request.
25 + *   check_printer_state()  - Check the printer state...
26 + *   compress_files()       - Compress print files...
27   *   password_cb()          - Disable the password prompt for
28   *                            cupsDoFileRequest().
29   *   report_attr()          - Report an IPP attribute value.
30   *   report_printer_state() - Report the printer state.
31 - *   run_as_user()          - Run the IPP backend as the printing user.
32 - *   timeout_cb()           - Handle HTTP timeouts.
33 + *   run_pictwps_filter()   - Convert PICT files to PostScript when printing
34 + *                            remotely.
35   *   sigterm_handler()      - Handle 'terminate' signals that stop the backend.
36   */
37  
38 @@ -35,138 +33,49 @@
39   * Include necessary headers.
40   */
41  
42 +#include <cups/http-private.h>
43  #include "backend-private.h"
44 -#include <cups/array-private.h>
45  #include <sys/types.h>
46  #include <sys/stat.h>
47  #include <sys/wait.h>
48 -#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
49 -#  include <xpc/xpc.h>
50 -#  define kPMPrintUIToolAgent  "com.apple.printuitool.agent"
51 -#  define kPMStartJob          100
52 -#  define kPMWaitForJob                101
53 -extern void    xpc_connection_set_target_uid(xpc_connection_t connection,
54 -                                             uid_t uid);
55 -#endif /* HAVE_GSSAPI && HAVE_XPC */
56  
57 -
58  /*
59 - * Types...
60 - */
61 -
62 -typedef struct _cups_monitor_s         /**** Monitoring data ****/
63 -{
64 -  const char           *uri,           /* Printer URI */
65 -                       *hostname,      /* Hostname */
66 -                       *user,          /* Username */
67 -                       *resource;      /* Resource path */
68 -  int                  port,           /* Port number */
69 -                       version,        /* IPP version */
70 -                       job_id;         /* Job ID for submitted job */
71 -  http_encryption_t    encryption;     /* Use encryption? */
72 -  ipp_jstate_t         job_state;      /* Current job state */
73 -  ipp_pstate_t         printer_state;  /* Current printer state */
74 -} _cups_monitor_t;
75 -
76 -
77 -/*
78   * Globals...
79   */
80  
81 -static const char      *auth_info_required;
82 +static char    *password = NULL;       /* Password for device URI */
83 +static int     password_tries = 0;     /* Password tries */
84 +static const char *auth_info_required = "none";
85                                         /* New auth-info-required value */
86 -#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
87 -static int             child_pid = 0;  /* Child process ID */
88 -#endif /* HAVE_GSSAPI && HAVE_XPC */
89 -static const char * const jattrs[] =   /* Job attributes we want */
90 -{
91 -  "job-impressions-completed",
92 -  "job-media-sheets-completed",
93 -  "job-state",
94 -  "job-state-reasons"
95 -};
96 -static int             job_canceled = 0;
97 -                                       /* Job cancelled? */
98 -static char            *password = NULL;
99 -                                       /* Password for device URI */
100 -static int             password_tries = 0;
101 -                                       /* Password tries */
102 -static const char * const pattrs[] =   /* Printer attributes we want */
103 -{
104 -  "copies-supported",
105 -  "cups-version",
106 -  "document-format-supported",
107 -  "marker-colors",
108 -  "marker-high-levels",
109 -  "marker-levels",
110 -  "marker-low-levels",
111 -  "marker-message",
112 -  "marker-names",
113 -  "marker-types",
114 -  "media-col-supported",
115 -  "multiple-document-handling-supported",
116 -  "operations-supported",
117 -  "printer-alert",
118 -  "printer-alert-description",
119 -  "printer-is-accepting-jobs",
120 -  "printer-state",
121 -  "printer-state-message",
122 -  "printer-state-reasons"
123 -};
124 -static const char * const remote_job_states[] =
125 -{                                      /* Remote job state keywords */
126 -  "+cups-remote-pending",
127 -  "+cups-remote-pending-held",
128 -  "+cups-remote-processing",
129 -  "+cups-remote-stopped",
130 -  "+cups-remote-canceled",
131 -  "+cups-remote-aborted",
132 -  "+cups-remote-completed"
133 -};
134 -static _cups_mutex_t   report_mutex = _CUPS_MUTEX_INITIALIZER;
135 -                                       /* Mutex to control access */
136 -static int             num_attr_cache = 0;
137 -                                       /* Number of cached attributes */
138 -static cups_option_t   *attr_cache = NULL;
139 -                                       /* Cached attributes */
140 -static cups_array_t    *state_reasons; /* Array of printe-state-reasons keywords */
141 -static char            tmpfilename[1024] = "";
142 -                                       /* Temporary spool file name */
143 +#ifdef __APPLE__
144 +static char    pstmpname[1024] = "";   /* Temporary PostScript file name */
145 +#endif /* __APPLE__ */
146 +static char    tmpfilename[1024] = ""; /* Temporary spool file name */
147 +static int     job_cancelled = 0;      /* Job cancelled? */
148  
149  
150  /*
151   * Local functions...
152   */
153  
154 -static void            cancel_job(http_t *http, const char *uri, int id,
155 -                                  const char *resource, const char *user,
156 -                                  int version);
157 -static ipp_pstate_t    check_printer_state(http_t *http, const char *uri,
158 -                                           const char *resource,
159 -                                           const char *user, int version);
160 +static void    cancel_job(http_t *http, const char *uri, int id,
161 +                          const char *resource, const char *user, int version);
162 +static void    check_printer_state(http_t *http, const char *uri,
163 +                                   const char *resource, const char *user,
164 +                                   int version, int job_id);
165  #ifdef HAVE_LIBZ
166 -static void            compress_files(int num_files, char **files);
167 +static void    compress_files(int num_files, char **files);
168  #endif /* HAVE_LIBZ */
169 -static void            *monitor_printer(_cups_monitor_t *monitor);
170 -static ipp_t           *new_request(ipp_op_t op, int version, const char *uri,
171 -                                    const char *user, const char *title,
172 -                                    int num_options, cups_option_t *options,
173 -                                    const char *compression, int copies,
174 -                                    const char *format, _ppd_cache_t *pc,
175 -                                    ipp_attribute_t *media_col_sup,
176 -                                    ipp_attribute_t *doc_handling_sup);
177 -static const char      *password_cb(const char *);
178 -static void            report_attr(ipp_attribute_t *attr);
179 -static void            report_printer_state(ipp_t *ipp);
180 -#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
181 -static int             run_as_user(int argc, char *argv[], uid_t uid,
182 -                                   const char *device_uri, int fd);
183 -#endif /* HAVE_GSSAPI && HAVE_XPC */
184 -static void            sigterm_handler(int sig);
185 -static int             timeout_cb(http_t *http, void *user_data);
186 -static void            update_reasons(ipp_attribute_t *attr, const char *s);
187 +static const char *password_cb(const char *);
188 +static void    report_attr(ipp_attribute_t *attr);
189 +static int     report_printer_state(ipp_t *ipp, int job_id);
190  
191 +#ifdef __APPLE__
192 +static int     run_pictwps_filter(char **argv, const char *filename);
193 +#endif /* __APPLE__ */
194 +static void    sigterm_handler(int sig);
195  
196 +
197  /*
198   * 'main()' - Send a file to the printer or server.
199   *
200 @@ -193,60 +102,65 @@
201                 *name,                  /* Name of option */
202                 *value,                 /* Value of option */
203                 sep;                    /* Separator character */
204 -  http_addrlist_t *addrlist;           /* Address of printer */
205    int          snmp_fd,                /* SNMP socket */
206                 start_count,            /* Page count via SNMP at start */
207                 page_count,             /* Page count via SNMP */
208                 have_supplies;          /* Printer supports supply levels? */
209    int          num_files;              /* Number of files to print */
210    char         **files,                /* Files to print */
211 -               *compatfile = NULL;     /* Compatibility filename */
212 -  off_t                compatsize = 0;         /* Size of compatibility file */
213 +               *filename;              /* Pointer to single filename */
214    int          port;                   /* Port number (not used) */
215 -  char         portname[255];          /* Port name */
216    char         uri[HTTP_MAX_URI];      /* Updated URI without user/pass */
217 -  http_status_t        http_status;            /* Status of HTTP request */
218    ipp_status_t ipp_status;             /* Status of IPP request */
219    http_t       *http;                  /* HTTP connection */
220    ipp_t                *request,               /* IPP request */
221                 *response,              /* IPP response */
222                 *supported;             /* get-printer-attributes response */
223    time_t       start_time;             /* Time of first connect */
224 +  int          recoverable;            /* Recoverable error shown? */
225    int          contimeout;             /* Connection timeout */
226 -  int          delay,                  /* Delay for retries */
227 -               prev_delay;             /* Previous delay */
228 -  const char   *compression;           /* Compression mode */
229 -  int          waitjob,                /* Wait for job complete? */
230 +  int          delay;                  /* Delay for retries... */
231 +  int          compression,            /* Do compression of the job data? */
232 +               waitjob,                /* Wait for job complete? */
233                 waitprinter;            /* Wait for printer ready? */
234 -  _cups_monitor_t monitor;             /* Monitoring data */
235    ipp_attribute_t *job_id_attr;                /* job-id attribute */
236    int          job_id;                 /* job-id value */
237    ipp_attribute_t *job_sheets;         /* job-media-sheets-completed */
238    ipp_attribute_t *job_state;          /* job-state */
239    ipp_attribute_t *copies_sup;         /* copies-supported */
240 -  ipp_attribute_t *cups_version;       /* cups-version */
241    ipp_attribute_t *format_sup;         /* document-format-supported */
242 -  ipp_attribute_t *media_col_sup;      /* media-col-supported */
243 -  ipp_attribute_t *operations_sup;     /* operations-supported */
244 -  ipp_attribute_t *doc_handling_sup;   /* multiple-document-handling-supported */
245    ipp_attribute_t *printer_state;      /* printer-state attribute */
246    ipp_attribute_t *printer_accepting;  /* printer-is-accepting-jobs */
247 -  int          validate_job;           /* Does printer support Validate-Job? */
248    int          copies,                 /* Number of copies for job */
249                 copies_remaining;       /* Number of copies remaining */
250    const char   *content_type,          /* CONTENT_TYPE environment variable */
251 -               *final_content_type,    /* FINAL_CONTENT_TYPE environment var */
252 -               *document_format;       /* document-format value */
253 -  int          fd;                     /* File descriptor */
254 -  off_t                bytes = 0;              /* Bytes copied */
255 -  char         buffer[16384];          /* Copy buffer */
256 +               *final_content_type;    /* FINAL_CONTENT_TYPE environment var */
257  #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
258    struct sigaction action;             /* Actions for POSIX signals */
259  #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
260    int          version;                /* IPP version */
261 -  ppd_file_t   *ppd;                   /* PPD file */
262 -  _ppd_cache_t *pc;                    /* PPD cache and mapping data */
263 -  fd_set       input;                  /* Input set for select() */
264 +  static const char * const pattrs[] =
265 +               {                       /* Printer attributes we want */
266 +                  "com.apple.print.recoverable-message",
267 +                 "copies-supported",
268 +                 "document-format-supported",
269 +                 "marker-colors",
270 +                 "marker-high-levels",
271 +                 "marker-levels",
272 +                 "marker-low-levels",
273 +                 "marker-message",
274 +                 "marker-names",
275 +                 "marker-types",
276 +                 "printer-is-accepting-jobs",
277 +                 "printer-state",
278 +                 "printer-state-message",
279 +                 "printer-state-reasons",
280 +               };
281 +  static const char * const jattrs[] =
282 +               {                       /* Job attributes we want */
283 +                 "job-media-sheets-completed",
284 +                 "job-state"
285 +               };
286  
287  
288   /*
289 @@ -297,75 +211,12 @@
290    else if (argc < 6)
291    {
292      _cupsLangPrintf(stderr,
293 -                    _("Usage: %s job-id user title copies options [file]"),
294 +                    _("Usage: %s job-id user title copies options [file]\n"),
295                     argv[0]);
296      return (CUPS_BACKEND_STOP);
297    }
298  
299   /*
300 -  * Get the device URI...
301 -  */
302 -
303 -  while ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
304 -  {
305 -    _cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer."));
306 -    sleep(10);
307 -
308 -    if (getenv("CLASS") != NULL)
309 -      return (CUPS_BACKEND_FAILED);
310 -  }
311 -
312 -  if ((auth_info_required = getenv("AUTH_INFO_REQUIRED")) == NULL)
313 -    auth_info_required = "none";
314 -
315 -  state_reasons = _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"));
316 -
317 -#ifdef HAVE_GSSAPI
318 - /*
319 -  * For Kerberos, become the printing user (if we can) to get the credentials
320 -  * that way.
321 -  */
322 -
323 -  if (!getuid() && (value = getenv("AUTH_UID")) != NULL)
324 -  {
325 -    uid_t      uid = (uid_t)atoi(value);
326 -                                       /* User ID */
327 -
328 -#  ifdef HAVE_XPC
329 -    if (uid > 0)
330 -    {
331 -      if (argc == 6)
332 -        return (run_as_user(argc, argv, uid, device_uri, 0));
333 -      else
334 -      {
335 -        int status = 0;                        /* Exit status */
336 -
337 -        for (i = 6; i < argc && !status && !job_canceled; i ++)
338 -       {
339 -         if ((fd = open(argv[i], O_RDONLY)) >= 0)
340 -         {
341 -           status = run_as_user(argc, argv, uid, device_uri, fd);
342 -           close(fd);
343 -         }
344 -         else
345 -         {
346 -           _cupsLangPrintError("ERROR", _("Unable to open print file"));
347 -           status = CUPS_BACKEND_FAILED;
348 -         }
349 -       }
350 -
351 -       return (status);
352 -      }
353 -    }
354 -
355 -#  else /* No XPC, just try to run as the user ID */
356 -    if (uid > 0)
357 -      seteuid(uid);
358 -#  endif /* HAVE_XPC */
359 -  }
360 -#endif /* HAVE_GSSAPI */
361 -
362 - /*
363    * Get the (final) content type...
364    */
365  
366 @@ -384,6 +235,9 @@
367    * Extract the hostname and printer name from the URI...
368    */
369  
370 +  if ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
371 +    return (CUPS_BACKEND_FAILED);
372 +
373    httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
374                    username, sizeof(username), hostname, sizeof(hostname), &port,
375                   resource, sizeof(resource));
376 @@ -400,8 +254,8 @@
377    * See if there are any options...
378    */
379  
380 -  compression = NULL;
381 -  version     = 20;
382 +  compression = 0;
383 +  version     = 11;
384    waitjob     = 1;
385    waitprinter = 1;
386    contimeout  = 7 * 24 * 60 * 60;
387 @@ -454,48 +308,48 @@
388        * Process the option...
389        */
390  
391 -      if (!_cups_strcasecmp(name, "waitjob"))
392 +      if (!strcasecmp(name, "waitjob"))
393        {
394         /*
395          * Wait for job completion?
396         */
397  
398 -        waitjob = !_cups_strcasecmp(value, "on") ||
399 -                 !_cups_strcasecmp(value, "yes") ||
400 -                 !_cups_strcasecmp(value, "true");
401 +        waitjob = !strcasecmp(value, "on") ||
402 +                 !strcasecmp(value, "yes") ||
403 +                 !strcasecmp(value, "true");
404        }
405 -      else if (!_cups_strcasecmp(name, "waitprinter"))
406 +      else if (!strcasecmp(name, "waitprinter"))
407        {
408         /*
409          * Wait for printer idle?
410         */
411  
412 -        waitprinter = !_cups_strcasecmp(value, "on") ||
413 -                     !_cups_strcasecmp(value, "yes") ||
414 -                     !_cups_strcasecmp(value, "true");
415 +        waitprinter = !strcasecmp(value, "on") ||
416 +                     !strcasecmp(value, "yes") ||
417 +                     !strcasecmp(value, "true");
418        }
419 -      else if (!_cups_strcasecmp(name, "encryption"))
420 +      else if (!strcasecmp(name, "encryption"))
421        {
422         /*
423          * Enable/disable encryption?
424         */
425  
426 -        if (!_cups_strcasecmp(value, "always"))
427 +        if (!strcasecmp(value, "always"))
428           cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
429 -        else if (!_cups_strcasecmp(value, "required"))
430 +        else if (!strcasecmp(value, "required"))
431           cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
432 -        else if (!_cups_strcasecmp(value, "never"))
433 +        else if (!strcasecmp(value, "never"))
434           cupsSetEncryption(HTTP_ENCRYPT_NEVER);
435 -        else if (!_cups_strcasecmp(value, "ifrequested"))
436 +        else if (!strcasecmp(value, "ifrequested"))
437           cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
438         else
439         {
440 -         _cupsLangPrintFilter(stderr, "ERROR",
441 -                              _("Unknown encryption option value: \"%s\"."),
442 -                              value);
443 +         _cupsLangPrintf(stderr,
444 +                         _("ERROR: Unknown encryption option value \"%s\"!\n"),
445 +                         value);
446          }
447        }
448 -      else if (!_cups_strcasecmp(name, "version"))
449 +      else if (!strcasecmp(name, "version"))
450        {
451          if (!strcmp(value, "1.0"))
452           version = 10;
453 @@ -505,24 +359,23 @@
454           version = 20;
455         else if (!strcmp(value, "2.1"))
456           version = 21;
457 -       else if (!strcmp(value, "2.2"))
458 -         version = 22;
459         else
460         {
461 -         _cupsLangPrintFilter(stderr, "ERROR",
462 -                              _("Unknown version option value: \"%s\"."),
463 -                              value);
464 +         _cupsLangPrintf(stderr,
465 +                         _("ERROR: Unknown version option value \"%s\"!\n"),
466 +                         value);
467         }
468        }
469  #ifdef HAVE_LIBZ
470 -      else if (!_cups_strcasecmp(name, "compression"))
471 +      else if (!strcasecmp(name, "compression"))
472        {
473 -        if (!_cups_strcasecmp(value, "true") || !_cups_strcasecmp(value, "yes") ||
474 -           !_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "gzip"))
475 -         compression = "gzip";
476 +        compression = !strcasecmp(value, "true") ||
477 +                     !strcasecmp(value, "yes") ||
478 +                     !strcasecmp(value, "on") ||
479 +                     !strcasecmp(value, "gzip");
480        }
481  #endif /* HAVE_LIBZ */
482 -      else if (!_cups_strcasecmp(name, "contimeout"))
483 +      else if (!strcasecmp(name, "contimeout"))
484        {
485         /*
486          * Set the connection timeout...
487 @@ -537,9 +390,9 @@
488          * Unknown option...
489         */
490  
491 -       _cupsLangPrintFilter(stderr, "ERROR",
492 -                            _("Unknown option \"%s\" with value \"%s\"."),
493 -                            name, value);
494 +       _cupsLangPrintf(stderr,
495 +                       _("ERROR: Unknown option \"%s\" with value \"%s\"!\n"),
496 +                       name, value);
497        }
498      }
499    }
500 @@ -552,13 +405,64 @@
501  
502    if (argc == 6)
503    {
504 -    num_files    = 0;
505 -    files        = NULL;
506 -    send_options = !_cups_strcasecmp(final_content_type, "application/pdf") ||
507 -                   !_cups_strcasecmp(final_content_type, "application/vnd.cups-pdf") ||
508 -                   !_cups_strncasecmp(final_content_type, "image/", 6);
509 +   /*
510 +    * Copy stdin to a temporary file...
511 +    */
512  
513 -    fputs("DEBUG: Sending stdin for job...\n", stderr);
514 +    int                        fd;             /* File descriptor */
515 +    http_addrlist_t    *addrlist;      /* Address list */
516 +    off_t              tbytes;         /* Total bytes copied */
517 +
518 +
519 +    fputs("STATE: +connecting-to-device\n", stderr);
520 +    fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
521 +
522 +    if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, "1")) == NULL)
523 +    {
524 +      _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'!\n"),
525 +                     hostname);
526 +      return (CUPS_BACKEND_STOP);
527 +    }
528 +
529 +    snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family);
530 +
531 +    if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
532 +    {
533 +      _cupsLangPrintError("ERROR", _("Unable to create temporary file"));
534 +      return (CUPS_BACKEND_FAILED);
535 +    }
536 +
537 +    _cupsLangPuts(stderr, _("INFO: Copying print data...\n"));
538 +
539 +    tbytes = backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0,
540 +                            backendNetworkSideCB);
541 +
542 +    if (snmp_fd >= 0)
543 +      _cupsSNMPClose(snmp_fd);
544 +
545 +    httpAddrFreeList(addrlist);
546 +
547 +    close(fd);
548 +
549 +   /*
550 +    * Don't try printing files less than 2 bytes...
551 +    */
552 +
553 +    if (tbytes <= 1)
554 +    {
555 +      _cupsLangPuts(stderr, _("ERROR: Empty print file!\n"));
556 +      unlink(tmpfilename);
557 +      return (CUPS_BACKEND_FAILED);
558 +    }
559 +
560 +   /*
561 +    * Point to the single file from stdin...
562 +    */
563 +
564 +    filename     = tmpfilename;
565 +    num_files    = 1;
566 +    files        = &filename;
567 +    send_options = 0;
568    }
569    else
570    {
571 @@ -574,10 +478,10 @@
572      if (compression)
573        compress_files(num_files, files);
574  #endif /* HAVE_LIBZ */
575 -
576 -    fprintf(stderr, "DEBUG: %d files to send in job...\n", num_files);
577    }
578  
579 +  fprintf(stderr, "DEBUG: %d files to send in job...\n", num_files);
580 +
581   /*
582    * Set the authentication info, if any...
583    */
584 @@ -595,7 +499,7 @@
585  
586      cupsSetUser(username);
587    }
588 -  else
589 +  else if (!getuid())
590    {
591     /*
592      * Try loading authentication information from the environment.
593 @@ -610,75 +514,23 @@
594    }
595  
596   /*
597 -  * Try finding the remote server...
598 +  * Try connecting to the remote server...
599    */
600  
601 -  start_time = time(NULL);
602 +  delay       = 5;
603 +  recoverable = 0;
604 +  start_time  = time(NULL);
605  
606 -  sprintf(portname, "%d", port);
607 +  fputs("STATE: +connecting-to-device\n", stderr);
608  
609 -  update_reasons(NULL, "+connecting-to-device");
610 -  fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
611 -
612 -  while ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL)
613 -  {
614 -    _cupsLangPrintFilter(stderr, "INFO",
615 -                         _("Unable to locate printer \"%s\"."), hostname);
616 -    sleep(10);
617 -
618 -    if (getenv("CLASS") != NULL)
619 -    {
620 -      update_reasons(NULL, "-connecting-to-device");
621 -      return (CUPS_BACKEND_STOP);
622 -    }
623 -  }
624 -
625 -  http = _httpCreate(hostname, port, addrlist, cupsEncryption(), AF_UNSPEC);
626 -  httpSetTimeout(http, 30.0, timeout_cb, NULL);
627 -
628 - /*
629 -  * See if the printer supports SNMP...
630 -  */
631 -
632 -  if ((snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family)) >= 0)
633 -  {
634 -    have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr),
635 -                                         &start_count, NULL);
636 -  }
637 -  else
638 -    have_supplies = start_count = 0;
639 -
640 - /*
641 -  * Wait for data from the filter...
642 -  */
643 -
644 -  if (num_files == 0)
645 -  {
646 -    if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 0, backendNetworkSideCB))
647 -      return (CUPS_BACKEND_OK);
648 -    else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0)
649 -      return (CUPS_BACKEND_OK);
650 -  }
651 -
652 - /*
653 -  * Try connecting to the remote server...
654 -  */
655 -
656 -  delay = _cupsNextDelay(0, &prev_delay);
657 -
658    do
659    {
660      fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
661 -    _cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer."));
662 +    _cupsLangPuts(stderr, _("INFO: Connecting to printer...\n"));
663  
664 -    if (httpReconnect(http))
665 +    if ((http = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL)
666      {
667 -      int error = errno;               /* Connection error */
668 -
669 -      if (http->status == HTTP_PKI_ERROR)
670 -       update_reasons(NULL, "+cups-certificate-error");
671 -
672 -      if (job_canceled)
673 +      if (job_cancelled)
674         break;
675  
676        if (getenv("CLASS") != NULL)
677 @@ -690,85 +542,100 @@
678         * available printer in the class.
679         */
680  
681 -        _cupsLangPrintFilter(stderr, "INFO",
682 -                            _("Unable to contact printer, queuing on next "
683 -                              "printer in class."));
684 +        _cupsLangPuts(stderr,
685 +                     _("INFO: Unable to contact printer, queuing on next "
686 +                       "printer in class...\n"));
687  
688 +        if (tmpfilename[0])
689 +         unlink(tmpfilename);
690 +
691         /*
692          * Sleep 5 seconds to keep the job from requeuing too rapidly...
693         */
694  
695         sleep(5);
696  
697 -       update_reasons(NULL, "-connecting-to-device");
698 -
699          return (CUPS_BACKEND_FAILED);
700        }
701  
702 -      fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
703 -
704        if (errno == ECONNREFUSED || errno == EHOSTDOWN ||
705            errno == EHOSTUNREACH)
706        {
707          if (contimeout && (time(NULL) - start_time) > contimeout)
708         {
709 -         _cupsLangPrintFilter(stderr, "ERROR",
710 -                              _("The printer is not responding."));
711 -         update_reasons(NULL, "-connecting-to-device");
712 +         _cupsLangPuts(stderr, _("ERROR: Printer not responding!\n"));
713           return (CUPS_BACKEND_FAILED);
714         }
715  
716 -       switch (error)
717 -       {
718 -         case EHOSTDOWN :
719 -             _cupsLangPrintFilter(stderr, "WARNING",
720 -                                  _("The printer may not exist or "
721 -                                    "is unavailable at this time."));
722 -             break;
723 +        recoverable = 1;
724  
725 -         case EHOSTUNREACH :
726 -             _cupsLangPrintFilter(stderr, "WARNING",
727 -                                  _("The printer is unreachable at this "
728 -                                    "time."));
729 -             break;
730 +       _cupsLangPrintf(stderr,
731 +                       _("WARNING: recoverable: Network host \'%s\' is busy; "
732 +                         "will retry in %d seconds...\n"),
733 +                       hostname, delay);
734  
735 -         case ECONNREFUSED :
736 -         default :
737 -             _cupsLangPrintFilter(stderr, "WARNING",
738 -                                  _("The printer is busy."));
739 -             break;
740 -        }
741 -
742         sleep(delay);
743  
744 -        delay = _cupsNextDelay(delay, &prev_delay);
745 +       if (delay < 30)
746 +         delay += 5;
747        }
748 +      else if (h_errno)
749 +      {
750 +       _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'!\n"),
751 +                       hostname);
752 +       return (CUPS_BACKEND_STOP);
753 +      }
754        else
755        {
756 -       _cupsLangPrintFilter(stderr, "ERROR",
757 -                            _("The printer is not responding."));
758 +        recoverable = 1;
759 +
760 +        fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
761 +       _cupsLangPuts(stderr,
762 +                     _("ERROR: recoverable: Unable to connect to printer; will "
763 +                       "retry in 30 seconds...\n"));
764         sleep(30);
765        }
766  
767 -      if (job_canceled)
768 +      if (job_cancelled)
769         break;
770      }
771 -    else
772 -      update_reasons(NULL, "-cups-certificate-error");
773    }
774 -  while (http->fd < 0);
775 +  while (http == NULL);
776  
777 -  if (job_canceled || !http)
778 +  if (job_cancelled || !http)
779 +  {
780 +    if (tmpfilename[0])
781 +      unlink(tmpfilename);
782 +
783      return (CUPS_BACKEND_FAILED);
784 +  }
785  
786 -  update_reasons(NULL, "-connecting-to-device");
787 -  _cupsLangPrintFilter(stderr, "INFO", _("Connected to printer."));
788 +  fputs("STATE: -connecting-to-device\n", stderr);
789 +  _cupsLangPuts(stderr, _("INFO: Connected to printer...\n"));
790  
791 -  fprintf(stderr, "DEBUG: Connected to %s:%d...\n",
792 -         httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
793 -         _httpAddrPort(http->hostaddr));
794 +#ifdef AF_INET6
795 +  if (http->hostaddr->addr.sa_family == AF_INET6)
796 +    fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
797 +           httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
798 +           ntohs(http->hostaddr->ipv6.sin6_port));
799 +  else
800 +#endif /* AF_INET6 */
801 +    if (http->hostaddr->addr.sa_family == AF_INET)
802 +      fprintf(stderr, "DEBUG: Connected to %s:%d (IPv4)...\n",
803 +             httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
804 +             ntohs(http->hostaddr->ipv4.sin_port));
805  
806   /*
807 +  * See if the printer supports SNMP...
808 +  */
809 +
810 +  if ((snmp_fd = _cupsSNMPOpen(http->hostaddr->addr.sa_family)) >= 0)
811 +    have_supplies = !backendSNMPSupplies(snmp_fd, http->hostaddr, &start_count,
812 +                                         NULL);
813 +  else
814 +    have_supplies = start_count = 0;
815 +
816 + /*
817    * Build a URI for the printer and fill the standard IPP attributes for
818    * an IPP_PRINT_FILE request.  We can't use the URI in argv[0] because it
819    * might contain username:password information...
820 @@ -779,17 +646,13 @@
821  
822   /*
823    * First validate the destination and see if the device supports multiple
824 -  * copies...
825 +  * copies.  We have to do this because some IPP servers (e.g. HP JetDirect)
826 +  * don't support the copies attribute...
827    */
828  
829 -  copies_sup       = NULL;
830 -  cups_version     = NULL;
831 -  format_sup       = NULL;
832 -  media_col_sup    = NULL;
833 -  supported        = NULL;
834 -  operations_sup   = NULL;
835 -  doc_handling_sup = NULL;
836 -  validate_job     = 0;
837 +  copies_sup = NULL;
838 +  format_sup = NULL;
839 +  supported  = NULL;
840  
841    do
842    {
843 @@ -821,73 +684,57 @@
844      fputs("DEBUG: Getting supported attributes...\n", stderr);
845  
846      if (http->version < HTTP_1_1)
847 -    {
848 -      fprintf(stderr, "DEBUG: Printer responded with HTTP version %d.%d.\n",
849 -              http->version / 100, http->version % 100);
850 -      update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
851 -                          "cups-ipp-wrong-http-version");
852 -    }
853 +      httpReconnect(http);
854  
855 -    supported  = cupsDoRequest(http, request, resource);
856 -    ipp_status = cupsLastError();
857 +    if ((supported = cupsDoRequest(http, request, resource)) == NULL)
858 +      ipp_status = cupsLastError();
859 +    else
860 +      ipp_status = supported->request.status.status_code;
861  
862 -    fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
863 -            ippErrorString(ipp_status), cupsLastErrorString());
864 -
865      if (ipp_status > IPP_OK_CONFLICT)
866      {
867 -      fprintf(stderr, "DEBUG: Get-Printer-Attributes returned %s.\n",
868 -              ippErrorString(ipp_status));
869 -
870        if (ipp_status == IPP_PRINTER_BUSY ||
871           ipp_status == IPP_SERVICE_UNAVAILABLE)
872        {
873          if (contimeout && (time(NULL) - start_time) > contimeout)
874         {
875 -         _cupsLangPrintFilter(stderr, "ERROR",
876 -                              _("The printer is not responding."));
877 +         _cupsLangPuts(stderr, _("ERROR: Printer not responding!\n"));
878           return (CUPS_BACKEND_FAILED);
879         }
880  
881 -       _cupsLangPrintFilter(stderr, "INFO", _("The printer is busy."));
882 +        recoverable = 1;
883  
884 -        report_printer_state(supported);
885 +       _cupsLangPrintf(stderr,
886 +                       _("WARNING: recoverable: Network host \'%s\' is busy; "
887 +                         "will retry in %d seconds...\n"),
888 +                       hostname, delay);
889  
890 +        report_printer_state(supported, 0);
891 +
892         sleep(delay);
893  
894 -        delay = _cupsNextDelay(delay, &prev_delay);
895 +       if (delay < 30)
896 +         delay += 5;
897        }
898        else if ((ipp_status == IPP_BAD_REQUEST ||
899                 ipp_status == IPP_VERSION_NOT_SUPPORTED) && version > 10)
900        {
901         /*
902 -       * Switch to IPP/1.1 or IPP/1.0...
903 +       * Switch to IPP/1.0...
904         */
905  
906 -        if (version >= 20)
907 -       {
908 -         _cupsLangPrintFilter(stderr, "INFO",
909 -                              _("Printer does not support IPP/%d.%d, trying "
910 -                                "IPP/%s."), version / 10, version % 10, "1.1");
911 -         version = 11;
912 -       }
913 -       else
914 -       {
915 -         _cupsLangPrintFilter(stderr, "INFO",
916 -                              _("Printer does not support IPP/%d.%d, trying "
917 -                                "IPP/%s."), version / 10, version % 10, "1.0");
918 -         version = 10;
919 -        }
920 -
921 +       _cupsLangPrintf(stderr,
922 +                       _("INFO: Printer does not support IPP/%d.%d, trying "
923 +                         "IPP/1.0...\n"), version / 10, version % 10);
924 +       version = 10;
925         httpReconnect(http);
926        }
927        else if (ipp_status == IPP_NOT_FOUND)
928        {
929 -        _cupsLangPrintFilter(stderr, "ERROR",
930 -                            _("The printer URI is incorrect or no longer "
931 -                              "exists."));
932 +        _cupsLangPuts(stderr, _("ERROR: Destination printer does not exist!\n"));
933  
934 -       ippDelete(supported);
935 +       if (supported)
936 +          ippDelete(supported);
937  
938         return (CUPS_BACKEND_STOP);
939        }
940 @@ -902,92 +749,33 @@
941        }
942        else
943        {
944 -       _cupsLangPrintFilter(stderr, "ERROR",
945 -                            _("Unable to get printer status."));
946 +       _cupsLangPrintf(stderr,
947 +                       _("ERROR: Unable to get printer status (%s)!\n"),
948 +                       cupsLastErrorString());
949          sleep(10);
950        }
951  
952 -      ippDelete(supported);
953 -      supported = NULL;
954 +      if (supported)
955 +        ippDelete(supported);
956 +
957        continue;
958      }
959 -
960 -    if (!getenv("CLASS"))
961 +    else if ((copies_sup = ippFindAttribute(supported, "copies-supported",
962 +                                           IPP_TAG_RANGE)) != NULL)
963      {
964       /*
965 -      * Check printer-is-accepting-jobs = false and printer-state-reasons for the
966 -      * "spool-area-full" keyword...
967 -      */
968 -
969 -      int busy = 0;
970 -
971 -      if ((printer_accepting = ippFindAttribute(supported,
972 -                                               "printer-is-accepting-jobs",
973 -                                               IPP_TAG_BOOLEAN)) != NULL &&
974 -         !printer_accepting->values[0].boolean)
975 -        busy = 1;
976 -      else if (!printer_accepting)
977 -        update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
978 -                            "cups-ipp-missing-printer-is-accepting-jobs");
979 -
980 -      if ((printer_state = ippFindAttribute(supported,
981 -                                           "printer-state-reasons",
982 -                                           IPP_TAG_KEYWORD)) != NULL && !busy)
983 -      {
984 -       for (i = 0; i < printer_state->num_values; i ++)
985 -         if (!strcmp(printer_state->values[0].string.text,
986 -                     "spool-area-full") ||
987 -             !strncmp(printer_state->values[0].string.text, "spool-area-full-",
988 -                      16))
989 -         {
990 -           busy = 1;
991 -           break;
992 -         }
993 -      }
994 -      else
995 -        update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
996 -                            "cups-ipp-missing-printer-state-reasons");
997 -
998 -      if (busy)
999 -      {
1000 -       _cupsLangPrintFilter(stderr, "INFO", _("The printer is busy."));
1001 -
1002 -       report_printer_state(supported);
1003 -
1004 -       sleep(delay);
1005 -
1006 -       delay = _cupsNextDelay(delay, &prev_delay);
1007 -
1008 -       ippDelete(supported);
1009 -       supported = NULL;
1010 -       continue;
1011 -      }
1012 -    }
1013 -
1014 -   /*
1015 -    * Check for supported attributes...
1016 -    */
1017 -
1018 -    if ((copies_sup = ippFindAttribute(supported, "copies-supported",
1019 -                                      IPP_TAG_RANGE)) != NULL)
1020 -    {
1021 -     /*
1022        * Has the "copies-supported" attribute - does it have an upper
1023        * bound > 1?
1024        */
1025  
1026 -      fprintf(stderr, "DEBUG: copies-supported=%d-%d\n",
1027 -             copies_sup->values[0].range.lower,
1028 -             copies_sup->values[0].range.upper);
1029 -
1030        if (copies_sup->values[0].range.upper <= 1)
1031         copies_sup = NULL; /* No */
1032      }
1033  
1034 -    cups_version = ippFindAttribute(supported, "cups-version", IPP_TAG_TEXT);
1035 +    format_sup = ippFindAttribute(supported, "document-format-supported",
1036 +                                 IPP_TAG_MIMETYPE);
1037  
1038 -    if ((format_sup = ippFindAttribute(supported, "document-format-supported",
1039 -                                      IPP_TAG_MIMETYPE)) != NULL)
1040 +    if (format_sup)
1041      {
1042        fprintf(stderr, "DEBUG: document-format-supported (%d values)\n",
1043               format_sup->num_values);
1044 @@ -996,71 +784,7 @@
1045                 format_sup->values[i].string.text);
1046      }
1047  
1048 -    if ((media_col_sup = ippFindAttribute(supported, "media-col-supported",
1049 -                                         IPP_TAG_KEYWORD)) != NULL)
1050 -    {
1051 -      fprintf(stderr, "DEBUG: media-col-supported (%d values)\n",
1052 -             media_col_sup->num_values);
1053 -      for (i = 0; i < media_col_sup->num_values; i ++)
1054 -       fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
1055 -               media_col_sup->values[i].string.text);
1056 -    }
1057 -
1058 -    if ((operations_sup = ippFindAttribute(supported, "operations-supported",
1059 -                                          IPP_TAG_ENUM)) != NULL)
1060 -    {
1061 -      for (i = 0; i < operations_sup->num_values; i ++)
1062 -        if (operations_sup->values[i].integer == IPP_PRINT_JOB)
1063 -         break;
1064 -
1065 -      if (i >= operations_sup->num_values)
1066 -       update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1067 -                            "cups-ipp-missing-print-job");
1068 -
1069 -      for (i = 0; i < operations_sup->num_values; i ++)
1070 -        if (operations_sup->values[i].integer == IPP_CANCEL_JOB)
1071 -         break;
1072 -
1073 -      if (i >= operations_sup->num_values)
1074 -       update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1075 -                            "cups-ipp-missing-cancel-job");
1076 -
1077 -      for (i = 0; i < operations_sup->num_values; i ++)
1078 -        if (operations_sup->values[i].integer == IPP_GET_JOB_ATTRIBUTES)
1079 -         break;
1080 -
1081 -      if (i >= operations_sup->num_values)
1082 -       update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1083 -                             "cups-ipp-missing-get-job-attributes");
1084 -
1085 -      for (i = 0; i < operations_sup->num_values; i ++)
1086 -        if (operations_sup->values[i].integer == IPP_GET_PRINTER_ATTRIBUTES)
1087 -         break;
1088 -
1089 -      if (i >= operations_sup->num_values)
1090 -       update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1091 -                            "cups-ipp-missing-get-printer-attributes");
1092 -
1093 -      for (i = 0; i < operations_sup->num_values; i ++)
1094 -        if (operations_sup->values[i].integer == IPP_VALIDATE_JOB)
1095 -       {
1096 -         validate_job = 1;
1097 -         break;
1098 -       }
1099 -
1100 -      if (!validate_job)
1101 -       update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1102 -                             "cups-ipp-missing-validate-job");
1103 -    }
1104 -    else
1105 -      update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1106 -                          "cups-ipp-missing-operations-supported");
1107 -
1108 -    doc_handling_sup = ippFindAttribute(supported,
1109 -                                       "multiple-document-handling-supported",
1110 -                                       IPP_TAG_KEYWORD);
1111 -
1112 -    report_printer_state(supported);
1113 +    report_printer_state(supported, 0);
1114    }
1115    while (ipp_status > IPP_OK_CONFLICT);
1116  
1117 @@ -1089,13 +813,16 @@
1118        * available printer in the class.
1119        */
1120  
1121 -      _cupsLangPrintFilter(stderr, "INFO",
1122 -                           _("Unable to contact printer, queuing on next "
1123 -                            "printer in class."));
1124 +      _cupsLangPuts(stderr,
1125 +                    _("INFO: Unable to contact printer, queuing on next "
1126 +                     "printer in class...\n"));
1127  
1128        ippDelete(supported);
1129        httpClose(http);
1130  
1131 +      if (tmpfilename[0])
1132 +       unlink(tmpfilename);
1133 +
1134       /*
1135        * Sleep 5 seconds to keep the job from requeuing too rapidly...
1136        */
1137 @@ -1106,6 +833,18 @@
1138      }
1139    }
1140  
1141 +  if (recoverable)
1142 +  {
1143 +   /*
1144 +    * If we've shown a recoverable error make sure the printer proxies
1145 +    * have a chance to see the recovered message. Not pretty but
1146 +    * necessary for now...
1147 +    */
1148 +
1149 +    fputs("INFO: recovered: \n", stderr);
1150 +    sleep(5);
1151 +  }
1152 +
1153   /*
1154    * See if the printer supports multiple copies...
1155    */
1156 @@ -1116,309 +855,210 @@
1157    {
1158      copies_remaining = 1;
1159  
1160 -    if (argc < 7 && !send_options)
1161 +    if (argc < 7)
1162        copies = 1;
1163    }
1164    else
1165      copies_remaining = copies;
1166  
1167   /*
1168 -  * Prepare remaining printing options...
1169 +  * Then issue the print-job request...
1170    */
1171  
1172 -  options = NULL;
1173 -  pc      = NULL;
1174 +  job_id  = 0;
1175  
1176 -  if (send_options)
1177 +  while (copies_remaining > 0)
1178    {
1179 -    num_options = cupsParseOptions(argv[5], 0, &options);
1180 +   /*
1181 +    * Check for side-channel requests...
1182 +    */
1183  
1184 -    if (!cups_version && media_col_sup)
1185 -    {
1186 -     /*
1187 -      * Load the PPD file and generate PWG attribute mapping information...
1188 -      */
1189 +    backendCheckSideChannel(snmp_fd, http->hostaddr);
1190  
1191 -      ppd = ppdOpenFile(getenv("PPD"));
1192 -      pc  = _ppdCacheCreateWithPPD(ppd);
1193 +   /*
1194 +    * Build the IPP request...
1195 +    */
1196  
1197 -      ppdClose(ppd);
1198 -    }
1199 -  }
1200 -  else
1201 -    num_options = 0;
1202 +    if (job_cancelled)
1203 +      break;
1204  
1205 -  document_format = NULL;
1206 +    if (num_files > 1)
1207 +      request = ippNewRequest(IPP_CREATE_JOB);
1208 +    else
1209 +      request = ippNewRequest(IPP_PRINT_JOB);
1210  
1211 -  if (format_sup != NULL)
1212 -  {
1213 -    for (i = 0; i < format_sup->num_values; i ++)
1214 -      if (!_cups_strcasecmp(final_content_type, format_sup->values[i].string.text))
1215 -      {
1216 -        document_format = final_content_type;
1217 -       break;
1218 -      }
1219 +    request->request.op.version[0] = version / 10;
1220 +    request->request.op.version[1] = version % 10;
1221  
1222 -    if (!document_format)
1223 -    {
1224 -      for (i = 0; i < format_sup->num_values; i ++)
1225 -       if (!_cups_strcasecmp("application/octet-stream",
1226 -                       format_sup->values[i].string.text))
1227 -       {
1228 -         document_format = "application/octet-stream";
1229 -         break;
1230 -       }
1231 -    }
1232 -  }
1233 +    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1234 +                NULL, uri);
1235  
1236 - /*
1237 -  * If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1238 -  * to a temporary file so that we can do a HTTP/1.0 submission...
1239 -  *
1240 -  * (I hate compatibility hacks!)
1241 -  */
1242 +    fprintf(stderr, "DEBUG: printer-uri = \"%s\"\n", uri);
1243  
1244 -  if (http->version < HTTP_1_1 && num_files == 0)
1245 -  {
1246 -    if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
1247 -    {
1248 -      perror("DEBUG: Unable to create temporary file");
1249 -      return (CUPS_BACKEND_FAILED);
1250 -    }
1251 +    if (argv[2][0])
1252 +      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1253 +                   "requesting-user-name", NULL, argv[2]);
1254  
1255 -    _cupsLangPrintFilter(stderr, "INFO", _("Copying print data."));
1256 +    fprintf(stderr, "DEBUG: requesting-user-name = \"%s\"\n", argv[2]);
1257  
1258 -    compatsize = backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0,
1259 -                               backendNetworkSideCB);
1260 +   /*
1261 +    * Only add a "job-name" attribute if the remote server supports
1262 +    * copy generation - some IPP implementations like HP's don't seem
1263 +    * to like UTF-8 job names (STR #1837)...
1264 +    */
1265  
1266 -    close(fd);
1267 +    if (argv[3][0] && copies_sup)
1268 +      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
1269 +                  argv[3]);
1270  
1271 -    compatfile = tmpfilename;
1272 -    files      = &compatfile;
1273 -    num_files  = 1;
1274 -  }
1275 -  else if (http->version < HTTP_1_1 && num_files == 1)
1276 -  {
1277 -    struct stat        fileinfo;               /* File information */
1278 +    fprintf(stderr, "DEBUG: job-name = \"%s\"\n", argv[3]);
1279  
1280 -    if (!stat(files[0], &fileinfo))
1281 -      compatsize = fileinfo.st_size;
1282 -  }
1283 +#ifdef HAVE_LIBZ
1284 +    if (compression)
1285 +      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1286 +                   "compression", NULL, "gzip");
1287 +#endif /* HAVE_LIBZ */
1288  
1289 - /*
1290 -  * Start monitoring the printer in the background...
1291 -  */
1292 +   /*
1293 +    * Handle options on the command-line...
1294 +    */
1295  
1296 -  monitor.uri           = uri;
1297 -  monitor.hostname      = hostname;
1298 -  monitor.user          = argv[2];
1299 -  monitor.resource      = resource;
1300 -  monitor.port          = port;
1301 -  monitor.version       = version;
1302 -  monitor.job_id        = 0;
1303 -  monitor.encryption    = cupsEncryption();
1304 -  monitor.job_state     = IPP_JOB_PENDING;
1305 -  monitor.printer_state = IPP_PRINTER_IDLE;
1306 +    options     = NULL;
1307 +    num_options = cupsParseOptions(argv[5], 0, &options);
1308  
1309 -  _cupsThreadCreate((_cups_thread_func_t)monitor_printer, &monitor);
1310 +#ifdef __APPLE__
1311 +    if (!strcasecmp(final_content_type, "application/pictwps") &&
1312 +        num_files == 1)
1313 +    {
1314 +      if (format_sup != NULL)
1315 +      {
1316 +       for (i = 0; i < format_sup->num_values; i ++)
1317 +         if (!strcasecmp(final_content_type, format_sup->values[i].string.text))
1318 +           break;
1319 +      }
1320  
1321 - /*
1322 -  * Validate access to the printer...
1323 -  */
1324 +      if (format_sup == NULL || i >= format_sup->num_values)
1325 +      {
1326 +       /*
1327 +       * Remote doesn't support "application/pictwps" (i.e. it's not MacOS X)
1328 +       * so convert the document to PostScript...
1329 +       */
1330  
1331 -  while (!job_canceled && validate_job)
1332 -  {
1333 -    request = new_request(IPP_VALIDATE_JOB, version, uri, argv[2], argv[3],
1334 -                          num_options, options, compression,
1335 -                         copies_sup ? copies : 1, document_format, pc,
1336 -                         media_col_sup, doc_handling_sup);
1337 +       if (run_pictwps_filter(argv, files[0]))
1338 +       {
1339 +         if (pstmpname[0])
1340 +           unlink(pstmpname);
1341  
1342 -    ippDelete(cupsDoRequest(http, request, resource));
1343 +         if (tmpfilename[0])
1344 +           unlink(tmpfilename);
1345  
1346 -    ipp_status = cupsLastError();
1347 +         return (CUPS_BACKEND_FAILED);
1348 +        }
1349  
1350 -    fprintf(stderr, "DEBUG: Validate-Job: %s (%s)\n",
1351 -            ippErrorString(ipp_status), cupsLastErrorString());
1352 +        files[0] = pstmpname;
1353  
1354 -    if (job_canceled)
1355 -      break;
1356 +       /*
1357 +       * Change the MIME type to application/postscript and change the
1358 +       * number of copies to 1...
1359 +       */
1360  
1361 -    if (ipp_status == IPP_SERVICE_UNAVAILABLE || ipp_status == IPP_PRINTER_BUSY)
1362 -    {
1363 -      _cupsLangPrintFilter(stderr, "INFO", _("The printer is busy."));
1364 -      sleep(10);
1365 +       final_content_type = "application/postscript";
1366 +       copies             = 1;
1367 +       copies_remaining   = 1;
1368 +        send_options       = 0;
1369 +      }
1370      }
1371 -    else if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
1372 -            ipp_status == IPP_AUTHENTICATION_CANCELED)
1373 +#endif /* __APPLE__ */
1374 +
1375 +    if (format_sup != NULL)
1376      {
1377 -     /*
1378 -      * Update auth-info-required as needed...
1379 -      */
1380 +      for (i = 0; i < format_sup->num_values; i ++)
1381 +        if (!strcasecmp(final_content_type, format_sup->values[i].string.text))
1382 +          break;
1383  
1384 -      fprintf(stderr, "DEBUG: WWW-Authenticate=\"%s\"\n",
1385 -             httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE));
1386 +      if (i < format_sup->num_values)
1387 +        ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1388 +                    "document-format", NULL, final_content_type);
1389 +    }
1390  
1391 +    if (copies_sup && version > 10 && send_options)
1392 +    {
1393       /*
1394 -      * Normal authentication goes through the password callback, which sets
1395 -      * auth_info_required to "username,password".  Kerberos goes directly
1396 -      * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
1397 -      * here and set auth_info_required as needed...
1398 +      * Only send options if the destination printer supports the copies
1399 +      * attribute and IPP/1.1.  This is a hack for the HP and Lexmark
1400 +      * implementations of IPP, which do not accept extension attributes
1401 +      * and incorrectly report a client-error-bad-request error instead of
1402 +      * the successful-ok-unsupported-attributes status.  In short, at least
1403 +      * some HP and Lexmark implementations of IPP are non-compliant.
1404        */
1405  
1406 -      if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
1407 -                  "Negotiate", 9))
1408 -       auth_info_required = "negotiate";
1409 +      cupsEncodeOptions(request, num_options, options);
1410  
1411 -      goto cleanup;
1412 +      ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies",
1413 +                    copies);
1414      }
1415 -    else if (ipp_status == IPP_OPERATION_NOT_SUPPORTED)
1416 -    {
1417 -     /*
1418 -      * This is all too common...
1419 -      */
1420  
1421 -      update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1422 -                          "cups-ipp-missing-validate-job");
1423 -      break;
1424 -    }
1425 -    else if (ipp_status < IPP_REDIRECTION_OTHER_SITE)
1426 -      break;
1427 -  }
1428 +    cupsFreeOptions(num_options, options);
1429  
1430 - /*
1431 -  * Then issue the print-job request...
1432 -  */
1433 -
1434 -  job_id = 0;
1435 -
1436 -  while (!job_canceled && copies_remaining > 0)
1437 -  {
1438     /*
1439 -    * Check for side-channel requests...
1440 +    * If copies aren't supported, then we are likely dealing with an HP
1441 +    * JetDirect.  The HP IPP implementation seems to close the connection
1442 +    * after every request - that is, it does *not* implement HTTP Keep-
1443 +    * Alive, which is REQUIRED by HTTP/1.1...
1444      */
1445  
1446 -    backendCheckSideChannel(snmp_fd, http->hostaddr);
1447 +    if (!copies_sup)
1448 +      httpReconnect(http);
1449  
1450     /*
1451 -    * Build the IPP job creation request...
1452 +    * Do the request...
1453      */
1454  
1455 -    if (job_canceled)
1456 -      break;
1457 +    if (http->version < HTTP_1_1)
1458 +      httpReconnect(http);
1459  
1460 -    request = new_request(num_files > 1 ? IPP_CREATE_JOB : IPP_PRINT_JOB,
1461 -                         version, uri, argv[2], argv[3], num_options, options,
1462 -                         compression, copies_sup ? copies : 1, document_format,
1463 -                         pc, media_col_sup, doc_handling_sup);
1464 -
1465 -   /*
1466 -    * Do the request...
1467 -    */
1468 -
1469      if (num_files > 1)
1470        response = cupsDoRequest(http, request, resource);
1471      else
1472 -    {
1473 -      size_t   length = 0;             /* Length of request */
1474 +      response = cupsDoFileRequest(http, request, resource, files[0]);
1475  
1476 -      if (compatsize > 0)
1477 -      {
1478 -        fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr);
1479 -        length = ippLength(request) + (size_t)compatsize;
1480 -      }
1481 -      else
1482 -        fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr);
1483 -
1484 -      http_status = cupsSendRequest(http, request, resource, length);
1485 -      if (http_status == HTTP_CONTINUE && request->state == IPP_DATA)
1486 -      {
1487 -        if (num_files == 1)
1488 -         fd = open(files[0], O_RDONLY);
1489 -       else
1490 -       {
1491 -         fd          = 0;
1492 -         http_status = cupsWriteRequestData(http, buffer, bytes);
1493 -        }
1494 -
1495 -        while (http_status == HTTP_CONTINUE &&
1496 -               (!job_canceled || compatsize > 0))
1497 -       {
1498 -        /*
1499 -         * Check for side-channel requests and more print data...
1500 -         */
1501 -
1502 -          FD_ZERO(&input);
1503 -         FD_SET(fd, &input);
1504 -         FD_SET(snmp_fd, &input);
1505 -
1506 -          while (select(fd > snmp_fd ? fd + 1 : snmp_fd + 1, &input, NULL, NULL,
1507 -                       NULL) <= 0 && !job_canceled);
1508 -
1509 -         if (FD_ISSET(snmp_fd, &input))
1510 -           backendCheckSideChannel(snmp_fd, http->hostaddr);
1511 -
1512 -          if (FD_ISSET(fd, &input))
1513 -          {
1514 -            if ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
1515 -            {
1516 -             fprintf(stderr, "DEBUG: Read %d bytes...\n", (int)bytes);
1517 -
1518 -             if (cupsWriteRequestData(http, buffer, bytes) != HTTP_CONTINUE)
1519 -               break;
1520 -           }
1521 -           else if (bytes == 0 || (errno != EINTR && errno != EAGAIN))
1522 -             break;
1523 -         }
1524 -       }
1525 -
1526 -        if (num_files == 1)
1527 -         close(fd);
1528 -      }
1529 -
1530 -      response = cupsGetResponse(http, resource);
1531 -      ippDelete(request);
1532 -    }
1533 -
1534      ipp_status = cupsLastError();
1535  
1536 -    fprintf(stderr, "DEBUG: %s: %s (%s)\n",
1537 -            num_files > 1 ? "Create-Job" : "Print-Job",
1538 -            ippErrorString(ipp_status), cupsLastErrorString());
1539 -
1540      if (ipp_status > IPP_OK_CONFLICT)
1541      {
1542        job_id = 0;
1543  
1544 -      if (job_canceled)
1545 +      if (job_cancelled)
1546          break;
1547  
1548        if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
1549 -          ipp_status == IPP_NOT_POSSIBLE ||
1550           ipp_status == IPP_PRINTER_BUSY)
1551        {
1552 -       _cupsLangPrintFilter(stderr, "INFO", _("The printer is busy."));
1553 +        _cupsLangPuts(stderr,
1554 +                     _("INFO: Printer busy; will retry in 10 seconds...\n"));
1555         sleep(10);
1556 +      }
1557 +      else if ((ipp_status == IPP_BAD_REQUEST ||
1558 +               ipp_status == IPP_VERSION_NOT_SUPPORTED) && version > 10)
1559 +      {
1560 +       /*
1561 +       * Switch to IPP/1.0...
1562 +       */
1563  
1564 -       if (num_files == 0)
1565 -       {
1566 -        /*
1567 -         * We can't re-submit when we have no files to print, so exit
1568 -         * immediately with the right status code...
1569 -         */
1570 -
1571 -         goto cleanup;
1572 -       }
1573 +       _cupsLangPrintf(stderr,
1574 +                       _("INFO: Printer does not support IPP/%d.%d, trying "
1575 +                         "IPP/1.0...\n"), version / 10, version % 10);
1576 +       version = 10;
1577 +       httpReconnect(http);
1578        }
1579 -      else if (ipp_status == IPP_ERROR_JOB_CANCELED)
1580 -        goto cleanup;
1581        else
1582        {
1583         /*
1584         * Update auth-info-required as needed...
1585         */
1586  
1587 -        _cupsLangPrintFilter(stderr, "ERROR",
1588 -                            _("Print file was not accepted."));
1589 +        _cupsLangPrintf(stderr, _("ERROR: Print file was not accepted (%s)!\n"),
1590 +                       cupsLastErrorString());
1591  
1592         if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
1593         {
1594 @@ -1436,39 +1076,25 @@
1595                        "Negotiate", 9))
1596             auth_info_required = "negotiate";
1597         }
1598 -       else
1599 -         sleep(10);
1600 -
1601 -       if (num_files == 0)
1602 -       {
1603 -        /*
1604 -         * We can't re-submit when we have no files to print, so exit
1605 -         * immediately with the right status code...
1606 -         */
1607 -
1608 -         goto cleanup;
1609 -       }
1610        }
1611      }
1612      else if ((job_id_attr = ippFindAttribute(response, "job-id",
1613                                               IPP_TAG_INTEGER)) == NULL)
1614      {
1615 -      _cupsLangPrintFilter(stderr, "INFO",
1616 -                          _("Print file accepted - job ID unknown."));
1617 -      update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1618 -                          "cups-ipp-missing-job-id");
1619 +      _cupsLangPuts(stderr,
1620 +                    _("NOTICE: Print file accepted - job ID unknown.\n"));
1621        job_id = 0;
1622      }
1623      else
1624      {
1625 -      monitor.job_id = job_id = job_id_attr->values[0].integer;
1626 -      _cupsLangPrintFilter(stderr, "INFO",
1627 -                           _("Print file accepted - job ID %d."), job_id);
1628 +      job_id = job_id_attr->values[0].integer;
1629 +      _cupsLangPrintf(stderr, _("NOTICE: Print file accepted - job ID %d.\n"),
1630 +                      job_id);
1631      }
1632  
1633      ippDelete(response);
1634  
1635 -    if (job_canceled)
1636 +    if (job_cancelled)
1637        break;
1638  
1639      if (job_id && num_files > 1)
1640 @@ -1505,41 +1131,18 @@
1641          ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1642                      "document-format", NULL, content_type);
1643  
1644 -       fprintf(stderr, "DEBUG: Sending file %d using chunking...\n", i + 1);
1645 -       http_status = cupsSendRequest(http, request, resource, 0);
1646 -       if (http_status == HTTP_CONTINUE && request->state == IPP_DATA &&
1647 -           (fd = open(files[i], O_RDONLY)) >= 0)
1648 -       {
1649 -         while (!job_canceled &&
1650 -                (bytes = read(fd, buffer, sizeof(buffer))) > 0)
1651 -         {
1652 -           if (cupsWriteRequestData(http, buffer, bytes) != HTTP_CONTINUE)
1653 -             break;
1654 -           else
1655 -           {
1656 -            /*
1657 -             * Check for side-channel requests...
1658 -             */
1659 +       if (http->version < HTTP_1_1)
1660 +         httpReconnect(http);
1661  
1662 -             backendCheckSideChannel(snmp_fd, http->hostaddr);
1663 -           }
1664 -         }
1665 +        ippDelete(cupsDoFileRequest(http, request, resource, files[i]));
1666  
1667 -         close(fd);
1668 -       }
1669 -
1670 -       ippDelete(cupsGetResponse(http, resource));
1671 -       ippDelete(request);
1672 -
1673 -       fprintf(stderr, "DEBUG: Send-Document: %s (%s)\n",
1674 -               ippErrorString(cupsLastError()), cupsLastErrorString());
1675 -
1676         if (cupsLastError() > IPP_OK_CONFLICT)
1677         {
1678           ipp_status = cupsLastError();
1679  
1680 -         _cupsLangPrintFilter(stderr, "ERROR",
1681 -                              _("Unable to add document to print job."));
1682 +         _cupsLangPrintf(stderr,
1683 +                         _("ERROR: Unable to add file %d to job: %s\n"),
1684 +                         job_id, cupsLastErrorString());
1685           break;
1686         }
1687        }
1688 @@ -1551,7 +1154,6 @@
1689        copies_remaining --;
1690      }
1691      else if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
1692 -             ipp_status == IPP_NOT_POSSIBLE ||
1693              ipp_status == IPP_PRINTER_BUSY)
1694        continue;
1695      else
1696 @@ -1564,9 +1166,9 @@
1697      if (!job_id || !waitjob)
1698        continue;
1699  
1700 -    _cupsLangPrintFilter(stderr, "INFO", _("Waiting for job to complete."));
1701 +    _cupsLangPuts(stderr, _("INFO: Waiting for job to complete...\n"));
1702  
1703 -    for (delay = _cupsNextDelay(0, &prev_delay); !job_canceled;)
1704 +    for (delay = 1; !job_cancelled;)
1705      {
1706       /*
1707        * Check for side-channel requests...
1708 @@ -1600,7 +1202,9 @@
1709        * Do the request...
1710        */
1711  
1712 -      httpReconnect(http);
1713 +      if (!copies_sup || http->version < HTTP_1_1)
1714 +       httpReconnect(http);
1715 +
1716        response   = cupsDoRequest(http, request, resource);
1717        ipp_status = cupsLastError();
1718  
1719 @@ -1610,27 +1214,22 @@
1720          * Job has gone away and/or the server has no job history...
1721         */
1722  
1723 -       update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1724 -                            "cups-ipp-missing-job-history");
1725          ippDelete(response);
1726  
1727         ipp_status = IPP_OK;
1728          break;
1729        }
1730  
1731 -      fprintf(stderr, "DEBUG: Get-Job-Attributes: %s (%s)\n",
1732 -             ippErrorString(ipp_status), cupsLastErrorString());
1733 -
1734        if (ipp_status > IPP_OK_CONFLICT)
1735        {
1736         if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
1737 -           ipp_status != IPP_NOT_POSSIBLE &&
1738             ipp_status != IPP_PRINTER_BUSY)
1739         {
1740           ippDelete(response);
1741  
1742 -          _cupsLangPrintFilter(stderr, "ERROR",
1743 -                              _("Unable to get print job status."));
1744 +          _cupsLangPrintf(stderr,
1745 +                         _("ERROR: Unable to get job %d attributes (%s)!\n"),
1746 +                         job_id, cupsLastErrorString());
1747            break;
1748         }
1749        }
1750 @@ -1640,41 +1239,23 @@
1751         if ((job_state = ippFindAttribute(response, "job-state",
1752                                           IPP_TAG_ENUM)) != NULL)
1753         {
1754 -         /*
1755 -         * Reflect the remote job state in the local queue...
1756 -         */
1757 -
1758 -         if (cups_version &&
1759 -             job_state->values[0].integer >= IPP_JOB_PENDING &&
1760 -             job_state->values[0].integer <= IPP_JOB_COMPLETED)
1761 -           update_reasons(NULL,
1762 -                          remote_job_states[job_state->values[0].integer -
1763 -                                            IPP_JOB_PENDING]);
1764 -
1765 -         if ((job_sheets = ippFindAttribute(response,
1766 -                                            "job-media-sheets-completed",
1767 -                                            IPP_TAG_INTEGER)) == NULL)
1768 -           job_sheets = ippFindAttribute(response,
1769 -                                         "job-impressions-completed",
1770 -                                         IPP_TAG_INTEGER);
1771 -
1772 -         if (job_sheets)
1773 -           fprintf(stderr, "PAGE: total %d\n",
1774 -                   job_sheets->values[0].integer);
1775 -
1776          /*
1777            * Stop polling if the job is finished or pending-held...
1778           */
1779  
1780            if (job_state->values[0].integer > IPP_JOB_STOPPED)
1781           {
1782 +           if ((job_sheets = ippFindAttribute(response, 
1783 +                                              "job-media-sheets-completed",
1784 +                                              IPP_TAG_INTEGER)) != NULL)
1785 +             fprintf(stderr, "PAGE: total %d\n",
1786 +                     job_sheets->values[0].integer);
1787 +
1788             ippDelete(response);
1789             break;
1790           }
1791         }
1792 -       else if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
1793 -                ipp_status != IPP_NOT_POSSIBLE &&
1794 -                ipp_status != IPP_PRINTER_BUSY)
1795 +       else
1796         {
1797          /*
1798           * If the printer does not return a job-state attribute, it does not
1799 @@ -1682,8 +1263,8 @@
1800           * the job...
1801           */
1802  
1803 -         update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1804 -                              "cups-ipp-missing-job-state");
1805 +          fputs("DEBUG: No job-state available from printer - stopping queue.\n",
1806 +               stderr);
1807           ipp_status = IPP_INTERNAL_ERROR;
1808           break;
1809         }
1810 @@ -1692,12 +1273,20 @@
1811        ippDelete(response);
1812  
1813       /*
1814 -      * Wait before polling again...
1815 +      * Check the printer state and report it if necessary...
1816        */
1817  
1818 +      check_printer_state(http, uri, resource, argv[2], version, job_id);
1819 +
1820 +     /*
1821 +      * Wait 1-10 seconds before polling again...
1822 +      */
1823 +
1824        sleep(delay);
1825  
1826 -      delay = _cupsNextDelay(delay, &prev_delay);
1827 +      delay ++;
1828 +      if (delay > 10)
1829 +        delay = 1;
1830      }
1831    }
1832  
1833 @@ -1705,20 +1294,20 @@
1834    * Cancel the job as needed...
1835    */
1836  
1837 -  if (job_canceled && job_id)
1838 +  if (job_cancelled && job_id)
1839      cancel_job(http, uri, job_id, resource, argv[2], version);
1840  
1841   /*
1842    * Check the printer state and report it if necessary...
1843    */
1844  
1845 -  check_printer_state(http, uri, resource, argv[2], version);
1846 +  check_printer_state(http, uri, resource, argv[2], version, job_id);
1847  
1848   /*
1849    * Collect the final page count as needed...
1850    */
1851  
1852 -  if (have_supplies &&
1853 +  if (have_supplies && 
1854        !backendSNMPSupplies(snmp_fd, http->hostaddr, &page_count, NULL) &&
1855        page_count > start_count)
1856      fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
1857 @@ -1736,11 +1325,6 @@
1858    * Free memory...
1859    */
1860  
1861 -  cleanup:
1862 -
1863 -  cupsFreeOptions(num_options, options);
1864 -  _ppdCacheDestroy(pc);
1865 -
1866    httpClose(http);
1867  
1868    ippDelete(supported);
1869 @@ -1760,28 +1344,26 @@
1870    }
1871  #endif /* HAVE_LIBZ */
1872  
1873 +#ifdef __APPLE__
1874 +  if (pstmpname[0])
1875 +    unlink(pstmpname);
1876 +#endif /* __APPLE__ */
1877 +
1878   /*
1879    * Return the queue status...
1880    */
1881  
1882 -  if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
1883 -      ipp_status == IPP_AUTHENTICATION_CANCELED ||
1884 -      ipp_status <= IPP_OK_CONFLICT)
1885 -    fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
1886 +  fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
1887  
1888 -  if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN ||
1889 -      ipp_status == IPP_AUTHENTICATION_CANCELED)
1890 +  if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
1891      return (CUPS_BACKEND_AUTH_REQUIRED);
1892    else if (ipp_status == IPP_INTERNAL_ERROR)
1893      return (CUPS_BACKEND_STOP);
1894 -  else if (ipp_status == IPP_DOCUMENT_FORMAT ||
1895 -           ipp_status == IPP_CONFLICT)
1896 +  else if (ipp_status > IPP_OK_CONFLICT)
1897      return (CUPS_BACKEND_FAILED);
1898 -  else if (ipp_status > IPP_OK_CONFLICT && ipp_status != IPP_ERROR_JOB_CANCELED)
1899 -    return (CUPS_BACKEND_RETRY_CURRENT);
1900    else
1901    {
1902 -    _cupsLangPrintFilter(stderr, "INFO", _("Ready to print."));
1903 +    _cupsLangPuts(stderr, _("INFO: Ready to print.\n"));
1904      return (CUPS_BACKEND_OK);
1905    }
1906  }
1907 @@ -1802,7 +1384,7 @@
1908    ipp_t        *request;                       /* Cancel-Job request */
1909  
1910  
1911 -  _cupsLangPrintFilter(stderr, "INFO", _("Canceling print job."));
1912 +  _cupsLangPuts(stderr, _("INFO: Canceling print job...\n"));
1913  
1914    request = ippNewRequest(IPP_CANCEL_JOB);
1915    request->request.op.version[0] = version / 10;
1916 @@ -1820,34 +1402,47 @@
1917    * Do the request...
1918    */
1919  
1920 +  if (http->version < HTTP_1_1)
1921 +    httpReconnect(http);
1922 +
1923    ippDelete(cupsDoRequest(http, request, resource));
1924  
1925    if (cupsLastError() > IPP_OK_CONFLICT)
1926 -    _cupsLangPrintFilter(stderr, "ERROR", _("Unable to cancel print job."));
1927 +    _cupsLangPrintf(stderr, _("ERROR: Unable to cancel job %d: %s\n"), id,
1928 +                   cupsLastErrorString());
1929  }
1930  
1931  
1932  /*
1933 - * 'check_printer_state()' - Check the printer state.
1934 + * 'check_printer_state()' - Check the printer state...
1935   */
1936  
1937 -static ipp_pstate_t                    /* O - Current printer-state */
1938 +static void
1939  check_printer_state(
1940      http_t      *http,                 /* I - HTTP connection */
1941      const char  *uri,                  /* I - Printer URI */
1942      const char  *resource,             /* I - Resource path */
1943      const char  *user,                 /* I - Username, if any */
1944 -    int         version)               /* I - IPP version */
1945 - {
1946 -  ipp_t                *request,               /* IPP request */
1947 -               *response;              /* IPP response */
1948 -  ipp_attribute_t *attr;               /* Attribute in response */
1949 -  ipp_pstate_t printer_state = IPP_PRINTER_STOPPED;
1950 -                                       /* Current printer-state */
1951 +    int         version,               /* I - IPP version */
1952 +    int         job_id)                        /* I - Current job ID */
1953 +{
1954 +  ipp_t        *request,                       /* IPP request */
1955 +       *response;                      /* IPP response */
1956 +  static const char * const attrs[] =  /* Attributes we want */
1957 +  {
1958 +    "com.apple.print.recoverable-message",
1959 +    "marker-colors",
1960 +    "marker-levels",
1961 +    "marker-message",
1962 +    "marker-names",
1963 +    "marker-types",
1964 +    "printer-state-message",
1965 +    "printer-state-reasons"
1966 +  };
1967  
1968  
1969   /*
1970 -  * Send a Get-Printer-Attributes request and log the results...
1971 +  * Check on the printer state...
1972    */
1973  
1974    request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
1975 @@ -1855,41 +1450,34 @@
1976    request->request.op.version[1] = version % 10;
1977  
1978    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1979 -              NULL, uri);
1980 +               NULL, uri);
1981  
1982    if (user && user[0])
1983      ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1984 -                "requesting-user-name", NULL, user);
1985 +                 "requesting-user-name", NULL, user);
1986  
1987    ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1988 -               "requested-attributes",
1989 -               (int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
1990 +                "requested-attributes",
1991 +               (int)(sizeof(attrs) / sizeof(attrs[0])), NULL, attrs);
1992  
1993 + /*
1994 +  * Do the request...
1995 +  */
1996 +
1997 +  if (http->version < HTTP_1_1)
1998 +    httpReconnect(http);
1999 +
2000    if ((response = cupsDoRequest(http, request, resource)) != NULL)
2001    {
2002 -    report_printer_state(response);
2003 -
2004 -    if ((attr = ippFindAttribute(response, "printer-state",
2005 -                                IPP_TAG_ENUM)) != NULL)
2006 -      printer_state = (ipp_pstate_t)attr->values[0].integer;
2007 -
2008 +    report_printer_state(response, job_id);
2009      ippDelete(response);
2010    }
2011 -
2012 -  fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
2013 -         ippErrorString(cupsLastError()), cupsLastErrorString());
2014 -
2015 - /*
2016 -  * Return the printer-state value...
2017 -  */
2018 -
2019 -  return (printer_state);
2020  }
2021  
2022  
2023  #ifdef HAVE_LIBZ
2024  /*
2025 - * 'compress_files()' - Compress print files.
2026 + * 'compress_files()' - Compress print files...
2027   */
2028  
2029  static void
2030 @@ -1912,19 +1500,25 @@
2031    {
2032      if ((fd = cupsTempFd(filename, sizeof(filename))) < 0)
2033      {
2034 -      _cupsLangPrintError("ERROR", _("Unable to create compressed print file"));
2035 +      _cupsLangPrintf(stderr,
2036 +                     _("ERROR: Unable to create temporary compressed print "
2037 +                       "file: %s\n"), strerror(errno));
2038        exit(CUPS_BACKEND_FAILED);
2039      }
2040  
2041      if ((out = cupsFileOpenFd(fd, "w9")) == NULL)
2042      {
2043 -      _cupsLangPrintError("ERROR", _("Unable to open compressed print file"));
2044 +      _cupsLangPrintf(stderr,
2045 +                     _("ERROR: Unable to open temporary compressed print "
2046 +                       "file: %s\n"), strerror(errno));
2047        exit(CUPS_BACKEND_FAILED);
2048      }
2049  
2050      if ((in = cupsFileOpen(files[i], "r")) == NULL)
2051      {
2052 -      _cupsLangPrintError("ERROR", _("Unable to open print file"));
2053 +      _cupsLangPrintf(stderr,
2054 +                      _("ERROR: Unable to open print file \"%s\": %s\n"),
2055 +                     files[i], strerror(errno));
2056        cupsFileClose(out);
2057        exit(CUPS_BACKEND_FAILED);
2058      }
2059 @@ -1933,8 +1527,9 @@
2060      while ((bytes = cupsFileRead(in, buffer, sizeof(buffer))) > 0)
2061        if (cupsFileWrite(out, buffer, bytes) < bytes)
2062        {
2063 -       _cupsLangPrintError("ERROR",
2064 -                           _("Unable to generate compressed print file"));
2065 +        _cupsLangPrintf(stderr,
2066 +                       _("ERROR: Unable to write %d bytes to \"%s\": %s\n"),
2067 +                       (int)bytes, filename, strerror(errno));
2068          cupsFileClose(in);
2069          cupsFileClose(out);
2070         exit(CUPS_BACKEND_FAILED);
2071 @@ -1959,365 +1554,6 @@
2072  
2073  
2074  /*
2075 - * 'monitor_printer()' - Monitor the printer state.
2076 - */
2077 -
2078 -static void *                          /* O - Thread exit code */
2079 -monitor_printer(
2080 -    _cups_monitor_t *monitor)          /* I - Monitoring data */
2081 -{
2082 -  http_t       *http;                  /* Connection to printer */
2083 -  ipp_t                *request,               /* IPP request */
2084 -               *response;              /* IPP response */
2085 -  ipp_attribute_t *attr;               /* Attribute in response */
2086 -  int          delay,                  /* Current delay */
2087 -               prev_delay;             /* Previous delay */
2088 -
2089 -
2090 - /*
2091 -  * Make a copy of the printer connection...
2092 -  */
2093 -
2094 -  http = _httpCreate(monitor->hostname, monitor->port, NULL, monitor->encryption,
2095 -                     AF_UNSPEC);
2096 -  httpSetTimeout(http, 30.0, timeout_cb, NULL);
2097 -  cupsSetPasswordCB(password_cb);
2098 -
2099 - /*
2100 -  * Loop until the job is canceled, aborted, or completed.
2101 -  */
2102 -
2103 -  delay = _cupsNextDelay(0, &prev_delay);
2104 -
2105 -  while (monitor->job_state < IPP_JOB_CANCELED && !job_canceled)
2106 -  {
2107 -   /*
2108 -    * Reconnect to the printer...
2109 -    */
2110 -
2111 -    if (!httpReconnect(http))
2112 -    {
2113 -     /*
2114 -      * Connected, so check on the printer state...
2115 -      */
2116 -
2117 -      monitor->printer_state = check_printer_state(http, monitor->uri,
2118 -                                                   monitor->resource,
2119 -                                                  monitor->user,
2120 -                                                  monitor->version);
2121 -
2122 -      if (monitor->job_id > 0)
2123 -      {
2124 -       /*
2125 -        * Check the status of the job itself...
2126 -       */
2127 -
2128 -       request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES);
2129 -       request->request.op.version[0] = monitor->version / 10;
2130 -       request->request.op.version[1] = monitor->version % 10;
2131 -
2132 -       ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2133 -                    NULL, monitor->uri);
2134 -       ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
2135 -                     monitor->job_id);
2136 -
2137 -       if (monitor->user && monitor->user[0])
2138 -         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2139 -                      "requesting-user-name", NULL, monitor->user);
2140 -
2141 -       ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2142 -                     "requested-attributes",
2143 -                     (int)(sizeof(jattrs) / sizeof(jattrs[0])), NULL, jattrs);
2144 -
2145 -       /*
2146 -       * Do the request...
2147 -       */
2148 -
2149 -       response = cupsDoRequest(http, request, monitor->resource);
2150 -
2151 -       fprintf(stderr, "DEBUG: Get-Job-Attributes: %s (%s)\n",
2152 -               ippErrorString(cupsLastError()), cupsLastErrorString());
2153 -
2154 -       if ((attr = ippFindAttribute(response, "job-state",
2155 -                                    IPP_TAG_ENUM)) != NULL)
2156 -         monitor->job_state = (ipp_jstate_t)attr->values[0].integer;
2157 -       else
2158 -         monitor->job_state = IPP_JOB_COMPLETED;
2159 -
2160 -       ippDelete(response);
2161 -      }
2162 -
2163 -     /*
2164 -      * Disconnect from the printer - we'll reconnect on the next poll...
2165 -      */
2166 -
2167 -      _httpDisconnect(http);
2168 -    }
2169 -
2170 -   /*
2171 -    * Sleep for N seconds...
2172 -    */
2173 -
2174 -    sleep(delay);
2175 -
2176 -    delay = _cupsNextDelay(delay, &prev_delay);
2177 -  }
2178 -
2179 - /*
2180 -  * Cleanup and return...
2181 -  */
2182 -
2183 -  httpClose(http);
2184 -
2185 -  return (NULL);
2186 -}
2187 -
2188 -
2189 -/*
2190 - * 'new_request()' - Create a new print creation or validation request.
2191 - */
2192 -
2193 -static ipp_t *                         /* O - Request data */
2194 -new_request(
2195 -    ipp_op_t        op,                        /* I - IPP operation code */
2196 -    int             version,           /* I - IPP version number */
2197 -    const char      *uri,              /* I - printer-uri value */
2198 -    const char      *user,             /* I - requesting-user-name value */
2199 -    const char      *title,            /* I - job-name value */
2200 -    int             num_options,       /* I - Number of options to send */
2201 -    cups_option_t   *options,          /* I - Options to send */
2202 -    const char      *compression,      /* I - compression value or NULL */
2203 -    int             copies,            /* I - copies value or 0 */
2204 -    const char      *format,           /* I - documet-format value or NULL */
2205 -    _ppd_cache_t    *pc,               /* I - PPD cache and mapping data */
2206 -    ipp_attribute_t *media_col_sup,    /* I - media-col-supported values */
2207 -    ipp_attribute_t *doc_handling_sup)  /* I - multiple-document-handling-supported values */
2208 -{
2209 -  int          i;                      /* Looping var */
2210 -  ipp_t                *request;               /* Request data */
2211 -  const char   *keyword;               /* PWG keyword */
2212 -  _pwg_size_t  *size;                  /* PWG media size */
2213 -  ipp_t                *media_col,             /* media-col value */
2214 -               *media_size;            /* media-size value */
2215 -  const char   *media_source,          /* media-source value */
2216 -               *media_type,            /* media-type value */
2217 -               *collate_str;           /* multiple-document-handling value */
2218 -
2219 -
2220 - /*
2221 -  * Create the IPP request...
2222 -  */
2223 -
2224 -  request                        = ippNewRequest(op);
2225 -  request->request.op.version[0] = version / 10;
2226 -  request->request.op.version[1] = version % 10;
2227 -
2228 -  fprintf(stderr, "DEBUG: %s IPP/%d.%d\n",
2229 -         ippOpString(request->request.op.operation_id),
2230 -         request->request.op.version[0],
2231 -         request->request.op.version[1]);
2232 -
2233 - /*
2234 -  * Add standard attributes...
2235 -  */
2236 -
2237 -  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2238 -              NULL, uri);
2239 -  fprintf(stderr, "DEBUG: printer-uri=\"%s\"\n", uri);
2240 -
2241 -  if (user && *user)
2242 -  {
2243 -    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2244 -                "requesting-user-name", NULL, user);
2245 -    fprintf(stderr, "DEBUG: requesting-user-name=\"%s\"\n", user);
2246 -  }
2247 -
2248 -  if (title && *title)
2249 -  {
2250 -    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
2251 -                title);
2252 -    fprintf(stderr, "DEBUG: job-name=\"%s\"\n", title);
2253 -  }
2254 -
2255 -  if (format)
2256 -  {
2257 -    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
2258 -                "document-format", NULL, format);
2259 -    fprintf(stderr, "DEBUG: document-format=\"%s\"\n", format);
2260 -  }
2261 -
2262 -#ifdef HAVE_LIBZ
2263 -  if (compression)
2264 -  {
2265 -    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2266 -                "compression", NULL, compression);
2267 -    fprintf(stderr, "DEBUG: compression=\"%s\"\n", compression);
2268 -  }
2269 -#endif /* HAVE_LIBZ */
2270 -
2271 - /*
2272 -  * Handle options on the command-line...
2273 -  */
2274 -
2275 -  if (num_options > 0)
2276 -  {
2277 -    if (pc)
2278 -    {
2279 -     /*
2280 -      * Send standard IPP attributes...
2281 -      */
2282 -
2283 -      if ((keyword = cupsGetOption("PageSize", num_options, options)) == NULL)
2284 -       keyword = cupsGetOption("media", num_options, options);
2285 -
2286 -      if ((size = _ppdCacheGetSize(pc, keyword)) != NULL)
2287 -      {
2288 -       /*
2289 -        * Add a media-col value...
2290 -       */
2291 -
2292 -       media_size = ippNew();
2293 -       ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2294 -                     "x-dimension", size->width);
2295 -       ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2296 -                     "y-dimension", size->length);
2297 -
2298 -       media_col = ippNew();
2299 -       ippAddCollection(media_col, IPP_TAG_ZERO, "media-size", media_size);
2300 -
2301 -       media_source = _ppdCacheGetSource(pc, cupsGetOption("InputSlot",
2302 -                                                           num_options,
2303 -                                                           options));
2304 -       media_type   = _ppdCacheGetType(pc, cupsGetOption("MediaType",
2305 -                                                         num_options,
2306 -                                                         options));
2307 -
2308 -       for (i = 0; i < media_col_sup->num_values; i ++)
2309 -       {
2310 -         if (!strcmp(media_col_sup->values[i].string.text,
2311 -                     "media-left-margin"))
2312 -           ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2313 -                         "media-left-margin", size->left);
2314 -         else if (!strcmp(media_col_sup->values[i].string.text,
2315 -                          "media-bottom-margin"))
2316 -           ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2317 -                         "media-bottom-margin", size->bottom);
2318 -         else if (!strcmp(media_col_sup->values[i].string.text,
2319 -                          "media-right-margin"))
2320 -           ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2321 -                         "media-right-margin", size->right);
2322 -         else if (!strcmp(media_col_sup->values[i].string.text,
2323 -                          "media-top-margin"))
2324 -           ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER,
2325 -                         "media-top-margin", size->top);
2326 -         else if (!strcmp(media_col_sup->values[i].string.text,
2327 -                          "media-source") && media_source)
2328 -           ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
2329 -                        "media-source", NULL, media_source);
2330 -         else if (!strcmp(media_col_sup->values[i].string.text,
2331 -                          "media-type") && media_type)
2332 -           ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD,
2333 -                        "media-type", NULL, media_type);
2334 -       }
2335 -
2336 -       ippAddCollection(request, IPP_TAG_JOB, "media-col", media_col);
2337 -      }
2338 -
2339 -      if ((keyword = cupsGetOption("output-bin", num_options,
2340 -                                  options)) == NULL)
2341 -       keyword = _ppdCacheGetBin(pc, cupsGetOption("OutputBin", num_options,
2342 -                                                   options));
2343 -
2344 -      if (keyword)
2345 -       ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-bin",
2346 -                    NULL, keyword);
2347 -
2348 -      if ((keyword = cupsGetOption("output-mode", num_options,
2349 -                                  options)) != NULL)
2350 -       ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-mode",
2351 -                    NULL, keyword);
2352 -      else if ((keyword = cupsGetOption("ColorModel", num_options,
2353 -                                       options)) != NULL)
2354 -      {
2355 -       if (!_cups_strcasecmp(keyword, "Gray"))
2356 -         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-mode",
2357 -                              NULL, "monochrome");
2358 -       else
2359 -         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-mode",
2360 -                          NULL, "color");
2361 -      }
2362 -
2363 -      if ((keyword = cupsGetOption("print-quality", num_options,
2364 -                                  options)) != NULL)
2365 -       ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2366 -                     atoi(keyword));
2367 -      else if ((keyword = cupsGetOption("cupsPrintQuality", num_options,
2368 -                                       options)) != NULL)
2369 -      {
2370 -       if (!_cups_strcasecmp(keyword, "draft"))
2371 -         ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2372 -                       IPP_QUALITY_DRAFT);
2373 -       else if (!_cups_strcasecmp(keyword, "normal"))
2374 -         ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2375 -                       IPP_QUALITY_NORMAL);
2376 -       else if (!_cups_strcasecmp(keyword, "high"))
2377 -         ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality",
2378 -                       IPP_QUALITY_HIGH);
2379 -      }
2380 -
2381 -      if ((keyword = cupsGetOption("sides", num_options, options)) != NULL)
2382 -       ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2383 -                    NULL, keyword);
2384 -      else if (pc->sides_option &&
2385 -               (keyword = cupsGetOption(pc->sides_option, num_options,
2386 -                                       options)) != NULL)
2387 -      {
2388 -       if (!_cups_strcasecmp(keyword, pc->sides_1sided))
2389 -         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2390 -                      NULL, "one-sided");
2391 -       else if (!_cups_strcasecmp(keyword, pc->sides_2sided_long))
2392 -         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2393 -                      NULL, "two-sided-long-edge");
2394 -       if (!_cups_strcasecmp(keyword, pc->sides_2sided_short))
2395 -         ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides",
2396 -                      NULL, "two-sided-short-edge");
2397 -      }
2398 -
2399 -      if (doc_handling_sup &&
2400 -         (keyword = cupsGetOption("collate", num_options, options)) != NULL)
2401 -      {
2402 -        if (!_cups_strcasecmp(keyword, "true"))
2403 -         collate_str = "separate-documents-collated-copies";
2404 -       else
2405 -         collate_str = "separate-documents-uncollated-copies";
2406 -
2407 -        for (i = 0; i < doc_handling_sup->num_values; i ++)
2408 -         if (!strcmp(doc_handling_sup->values[i].string.text, collate_str))
2409 -         {
2410 -           ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD,
2411 -                        "multiple-document-handling", NULL, collate_str);
2412 -           break;
2413 -          }
2414 -      }
2415 -    }
2416 -    else
2417 -    {
2418 -     /*
2419 -      * When talking to another CUPS server, send all options...
2420 -      */
2421 -
2422 -      cupsEncodeOptions(request, num_options, options);
2423 -    }
2424 -
2425 -    if (copies > 1)
2426 -      ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", copies);
2427 -  }
2428 -
2429 -  return (request);
2430 -}
2431 -
2432 -
2433 -/*
2434   * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2435   */
2436  
2437 @@ -2356,11 +1592,10 @@
2438  static void
2439  report_attr(ipp_attribute_t *attr)     /* I - Attribute */
2440  {
2441 -  int          i;                      /* Looping var */
2442 -  char         value[1024],            /* Value string */
2443 -               *valptr,                /* Pointer into value string */
2444 -               *attrptr;               /* Pointer into attribute value */
2445 -  const char   *cached;                /* Cached attribute */
2446 +  int  i;                              /* Looping var */
2447 +  char value[1024],                    /* Value string */
2448 +       *valptr,                        /* Pointer into value string */
2449 +       *attrptr;                       /* Pointer into attribute value */
2450  
2451  
2452   /*
2453 @@ -2410,21 +1645,11 @@
2454  
2455    *valptr = '\0';
2456  
2457 -  _cupsMutexLock(&report_mutex);
2458 + /*
2459 +  * Tell the scheduler about the new values...
2460 +  */
2461  
2462 -  if ((cached = cupsGetOption(attr->name, num_attr_cache,
2463 -                              attr_cache)) == NULL || strcmp(cached, value))
2464 -  {
2465 -   /*
2466 -    * Tell the scheduler about the new values...
2467 -    */
2468 -
2469 -    num_attr_cache = cupsAddOption(attr->name, value, num_attr_cache,
2470 -                                   &attr_cache);
2471 -    fprintf(stderr, "ATTR: %s=%s\n", attr->name, value);
2472 -  }
2473 -
2474 -  _cupsMutexUnlock(&report_mutex);
2475 +  fprintf(stderr, "ATTR: %s=%s\n", attr->name, value);
2476  }
2477  
2478  
2479 @@ -2432,561 +1657,297 @@
2480   * 'report_printer_state()' - Report the printer state.
2481   */
2482  
2483 -static void
2484 -report_printer_state(ipp_t *ipp)       /* I - IPP response */
2485 +static int                             /* O - Number of reasons shown */
2486 +report_printer_state(ipp_t *ipp,       /* I - IPP response */
2487 +                     int   job_id)     /* I - Current job ID */
2488  {
2489 -  ipp_attribute_t      *pa,            /* printer-alert */
2490 -                       *pam,           /* printer-alert-message */
2491 +  int                  i;              /* Looping var */
2492 +  int                  count;          /* Count of reasons shown... */
2493 +  ipp_attribute_t      *caprm,         /* com.apple.print.recoverable-message */
2494                         *psm,           /* printer-state-message */
2495                         *reasons,       /* printer-state-reasons */
2496                         *marker;        /* marker-* attributes */
2497 -  char                 value[1024],    /* State/message string */
2498 -                       *valptr;        /* Pointer into string */
2499 -  static int           ipp_supplies = -1;
2500 -                                       /* Report supply levels? */
2501 +  const char           *reason;        /* Current reason */
2502 +  const char           *prefix;        /* Prefix for STATE: line */
2503 +  char                 state[1024];    /* State string */
2504 +  int                  saw_caprw;      /* Saw com.apple.print.recoverable-warning state */
2505  
2506  
2507 - /*
2508 -  * Report alerts and messages...
2509 -  */
2510 +  if ((psm = ippFindAttribute(ipp, "printer-state-message",
2511 +                              IPP_TAG_TEXT)) != NULL)
2512 +    fprintf(stderr, "INFO: %s\n", psm->values[0].string.text);
2513  
2514 -  if ((pa = ippFindAttribute(ipp, "printer-alert", IPP_TAG_TEXT)) != NULL)
2515 -    report_attr(pa);
2516 +  if ((reasons = ippFindAttribute(ipp, "printer-state-reasons",
2517 +                                  IPP_TAG_KEYWORD)) == NULL)
2518 +    return (0);
2519  
2520 -  if ((pam = ippFindAttribute(ipp, "printer-alert-message",
2521 -                              IPP_TAG_TEXT)) != NULL)
2522 -    report_attr(pam);
2523 +  saw_caprw = 0;
2524 +  state[0]  = '\0';
2525 +  prefix    = "STATE: ";
2526  
2527 -  if ((psm = ippFindAttribute(ipp, "printer-state-message",
2528 -                              IPP_TAG_TEXT)) != NULL)
2529 +  for (i = 0, count = 0; i < reasons->num_values; i ++)
2530    {
2531 -    char       *ptr;                   /* Pointer into message */
2532 +    reason = reasons->values[i].string.text;
2533  
2534 -
2535 -    strlcpy(value, "INFO: ", sizeof(value));
2536 -    for (ptr = psm->values[0].string.text, valptr = value + 6;
2537 -         *ptr && valptr < (value + sizeof(value) - 6);
2538 -        ptr ++)
2539 +    if (!strcmp(reason, "com.apple.print.recoverable-warning"))
2540 +      saw_caprw = 1;
2541 +    else if (strcmp(reason, "paused"))
2542      {
2543 -      if (*ptr < ' ' && *ptr > 0 && *ptr != '\t')
2544 -      {
2545 -       /*
2546 -        * Substitute "<XX>" for the control character; sprintf is safe because
2547 -       * we always leave 6 chars free at the end...
2548 -       */
2549 +      strlcat(state, prefix, sizeof(state));
2550 +      strlcat(state, reason, sizeof(state));
2551  
2552 -        sprintf(valptr, "<%02X>", *ptr);
2553 -       valptr += 4;
2554 -      }
2555 -      else
2556 -        *valptr++ = *ptr;
2557 +      prefix  = ",";
2558      }
2559 +  }
2560  
2561 -    *valptr++ = '\n';
2562 -    *valptr   = '\0';
2563 +  if (state[0])
2564 +    fprintf(stderr, "%s\n", state);
2565  
2566 -    fputs(value, stderr);
2567 -  }
2568 -
2569   /*
2570 -  * Now report printer-state-reasons, filtering out some of the reasons we never
2571 -  * want to set...
2572 +  * Relay com.apple.print.recoverable-message...
2573    */
2574  
2575 -  if ((reasons = ippFindAttribute(ipp, "printer-state-reasons",
2576 -                                  IPP_TAG_KEYWORD)) == NULL)
2577 -    return;
2578 +  if ((caprm = ippFindAttribute(ipp, "com.apple.print.recoverable-message",
2579 +                                IPP_TAG_TEXT)) != NULL)
2580 +    fprintf(stderr, "WARNING: %s: %s\n",
2581 +            saw_caprw ? "recoverable" : "recovered",
2582 +           caprm->values[0].string.text);
2583  
2584 -  update_reasons(reasons, NULL);
2585 -
2586   /*
2587    * Relay the current marker-* attribute values...
2588    */
2589  
2590 -  if (ipp_supplies < 0)
2591 -  {
2592 -    ppd_file_t *ppd;                   /* PPD file */
2593 -    ppd_attr_t *ppdattr;               /* Attribute in PPD file */
2594 +  if ((marker = ippFindAttribute(ipp, "marker-colors", IPP_TAG_NAME)) != NULL)
2595 +    report_attr(marker);
2596 +  if ((marker = ippFindAttribute(ipp, "marker-high-levels",
2597 +                                 IPP_TAG_INTEGER)) != NULL)
2598 +    report_attr(marker);
2599 +  if ((marker = ippFindAttribute(ipp, "marker-levels",
2600 +                                 IPP_TAG_INTEGER)) != NULL)
2601 +    report_attr(marker);
2602 +  if ((marker = ippFindAttribute(ipp, "marker-low-levels",
2603 +                                 IPP_TAG_INTEGER)) != NULL)
2604 +    report_attr(marker);
2605 +  if ((marker = ippFindAttribute(ipp, "marker-message", IPP_TAG_TEXT)) != NULL)
2606 +    report_attr(marker);
2607 +  if ((marker = ippFindAttribute(ipp, "marker-names", IPP_TAG_NAME)) != NULL)
2608 +    report_attr(marker);
2609 +  if ((marker = ippFindAttribute(ipp, "marker-types", IPP_TAG_KEYWORD)) != NULL)
2610 +    report_attr(marker);
2611  
2612 -    if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL &&
2613 -        (ppdattr = ppdFindAttr(ppd, "cupsIPPSupplies", NULL)) != NULL &&
2614 -        ppdattr->value && _cups_strcasecmp(ppdattr->value, "true"))
2615 -      ipp_supplies = 0;
2616 -    else
2617 -      ipp_supplies = 1;
2618 -
2619 -    ppdClose(ppd);
2620 -  }
2621 -
2622 -  if (ipp_supplies > 0)
2623 -  {
2624 -    if ((marker = ippFindAttribute(ipp, "marker-colors", IPP_TAG_NAME)) != NULL)
2625 -      report_attr(marker);
2626 -    if ((marker = ippFindAttribute(ipp, "marker-high-levels",
2627 -                                   IPP_TAG_INTEGER)) != NULL)
2628 -      report_attr(marker);
2629 -    if ((marker = ippFindAttribute(ipp, "marker-levels",
2630 -                                   IPP_TAG_INTEGER)) != NULL)
2631 -      report_attr(marker);
2632 -    if ((marker = ippFindAttribute(ipp, "marker-low-levels",
2633 -                                   IPP_TAG_INTEGER)) != NULL)
2634 -      report_attr(marker);
2635 -    if ((marker = ippFindAttribute(ipp, "marker-message",
2636 -                                   IPP_TAG_TEXT)) != NULL)
2637 -      report_attr(marker);
2638 -    if ((marker = ippFindAttribute(ipp, "marker-names", IPP_TAG_NAME)) != NULL)
2639 -      report_attr(marker);
2640 -    if ((marker = ippFindAttribute(ipp, "marker-types",
2641 -                                   IPP_TAG_KEYWORD)) != NULL)
2642 -      report_attr(marker);
2643 -  }
2644 +  return (count);
2645  }
2646  
2647  
2648 -#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2649 +#ifdef __APPLE__
2650  /*
2651 - * 'run_as_user()' - Run the IPP backend as the printing user.
2652 + * 'run_pictwps_filter()' - Convert PICT files to PostScript when printing
2653 + *                          remotely.
2654   *
2655 - * This function uses an XPC-based user agent to run the backend as the printing
2656 - * user. We need to do this in order to have access to the user's Kerberos
2657 - * credentials.
2658 + * This step is required because the PICT format is not documented and
2659 + * subject to change, so developing a filter for other OS's is infeasible.
2660 + * Also, fonts required by the PICT file need to be embedded on the
2661 + * client side (which has the fonts), so we run the filter to get a
2662 + * PostScript file for printing...
2663   */
2664  
2665 -static int                             /* O - Exit status */
2666 -run_as_user(int        argc,           /* I - Number of command-line args */
2667 -           char       *argv[],         /* I - Command-line arguments */
2668 -           uid_t      uid,             /* I - User ID */
2669 -           const char *device_uri,     /* I - Device URI */
2670 -           int        fd)              /* I - File to print */
2671 +static int                             /* O - Exit status of filter */
2672 +run_pictwps_filter(char       **argv,  /* I - Command-line arguments */
2673 +                   const char *filename)/* I - Filename */
2674  {
2675 -  const char           *auth_negotiate;/* AUTH_NEGOTIATE env var */
2676 -  xpc_connection_t     conn;           /* Connection to XPC service */
2677 -  xpc_object_t         request;        /* Request message dictionary */
2678 -  __block xpc_object_t response;       /* Response message dictionary */
2679 -  dispatch_semaphore_t sem;            /* Semaphore for waiting for response */
2680 -  int                  status = CUPS_BACKEND_FAILED;
2681 -                                       /* Status of request */
2682 +  struct stat  fileinfo;               /* Print file information */
2683 +  const char   *ppdfile;               /* PPD file for destination printer */
2684 +  int          pid;                    /* Child process ID */
2685 +  int          fd;                     /* Temporary file descriptor */
2686 +  int          status;                 /* Exit status of filter */
2687 +  const char   *printer;               /* PRINTER env var */
2688 +  static char  ppdenv[1024];           /* PPD environment variable */
2689  
2690  
2691 -  fprintf(stderr, "DEBUG: Running IPP backend as UID %d.\n", (int)uid);
2692 -
2693   /*
2694 -  * Connect to the user agent for the specified UID...
2695 +  * First get the PPD file for the printer...
2696    */
2697  
2698 -  conn = xpc_connection_create_mach_service(kPMPrintUIToolAgent,
2699 -                                            dispatch_get_global_queue(0, 0), 0);
2700 -  if (!conn)
2701 +  printer = getenv("PRINTER");
2702 +  if (!printer)
2703    {
2704 -    _cupsLangPrintFilter(stderr, "ERROR",
2705 -                         _("Unable to start backend process."));
2706 -    fputs("DEBUG: Unable to create connection to agent.\n", stderr);
2707 -    goto cleanup;
2708 +    _cupsLangPuts(stderr,
2709 +                  _("ERROR: PRINTER environment variable not defined!\n"));
2710 +    return (-1);
2711    }
2712  
2713 -  xpc_connection_set_event_handler(conn,
2714 -                                   ^(xpc_object_t event)
2715 -                                  {
2716 -                                    xpc_type_t messageType = xpc_get_type(event);
2717 -
2718 -                                    if (messageType == XPC_TYPE_ERROR)
2719 -                                    {
2720 -                                      if (event == XPC_ERROR_CONNECTION_INTERRUPTED)
2721 -                                        fprintf(stderr, "DEBUG: Interrupted connection to service %s.\n",
2722 -                                                xpc_connection_get_name(conn));
2723 -                                      else if (event == XPC_ERROR_CONNECTION_INVALID)
2724 -                                        fprintf(stderr, "DEBUG: Connection invalid for service %s.\n",
2725 -                                                xpc_connection_get_name(conn));
2726 -                                      else
2727 -                                        fprintf(stderr, "DEBUG: Unxpected error for service %s: %s\n",
2728 -                                                xpc_connection_get_name(conn),
2729 -                                                xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
2730 -                                    }
2731 -                                  });
2732 -  xpc_connection_set_target_uid(conn, uid);
2733 -  xpc_connection_resume(conn);
2734 -
2735 - /*
2736 -  * Try starting the backend...
2737 -  */
2738 -
2739 -  request = xpc_dictionary_create(NULL, NULL, 0);
2740 -  xpc_dictionary_set_int64(request, "command", kPMStartJob);
2741 -  xpc_dictionary_set_string(request, "device-uri", device_uri);
2742 -  xpc_dictionary_set_string(request, "job-id", argv[1]);
2743 -  xpc_dictionary_set_string(request, "user", argv[2]);
2744 -  xpc_dictionary_set_string(request, "title", argv[3]);
2745 -  xpc_dictionary_set_string(request, "copies", argv[4]);
2746 -  xpc_dictionary_set_string(request, "options", argv[5]);
2747 -  xpc_dictionary_set_string(request, "auth-info-required",
2748 -                            getenv("AUTH_INFO_REQUIRED"));
2749 -  if ((auth_negotiate = getenv("AUTH_NEGOTIATE")) != NULL)
2750 -    xpc_dictionary_set_string(request, "auth-negotiate", auth_negotiate);
2751 -  xpc_dictionary_set_fd(request, "stdin", fd);
2752 -  xpc_dictionary_set_fd(request, "stderr", 2);
2753 -  xpc_dictionary_set_fd(request, "side-channel", CUPS_SC_FD);
2754 -
2755 -  sem      = dispatch_semaphore_create(0);
2756 -  response = NULL;
2757 -
2758 -  xpc_connection_send_message_with_reply(conn, request,
2759 -                                         dispatch_get_global_queue(0,0),
2760 -                                        ^(xpc_object_t reply)
2761 -                                        {
2762 -                                          /* Save the response and wake up */
2763 -                                          if (xpc_get_type(reply)
2764 -                                                  == XPC_TYPE_DICTIONARY)
2765 -                                            response = xpc_retain(reply);
2766 -
2767 -                                          dispatch_semaphore_signal(sem);
2768 -                                        });
2769 -
2770 -  dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
2771 -  xpc_release(request);
2772 -  dispatch_release(sem);
2773 -
2774 -  if (response)
2775 +  if ((ppdfile = cupsGetPPD(printer)) == NULL)
2776    {
2777 -    child_pid = xpc_dictionary_get_int64(response, "child-pid");
2778 -
2779 -    xpc_release(response);
2780 -
2781 -    if (child_pid)
2782 -      fprintf(stderr, "DEBUG: Child PID=%d.\n", child_pid);
2783 -    else
2784 -    {
2785 -      _cupsLangPrintFilter(stderr, "ERROR",
2786 -                           _("Unable to start backend process."));
2787 -      fputs("DEBUG: No child PID.\n", stderr);
2788 -      goto cleanup;
2789 -    }
2790 +    _cupsLangPrintf(stderr,
2791 +                   _("ERROR: Unable to get PPD file for printer \"%s\" - "
2792 +                     "%s.\n"), printer, cupsLastErrorString());
2793    }
2794    else
2795    {
2796 -    _cupsLangPrintFilter(stderr, "ERROR",
2797 -                         _("Unable to start backend process."));
2798 -    fputs("DEBUG: No reply from agent.\n", stderr);
2799 -    goto cleanup;
2800 +    snprintf(ppdenv, sizeof(ppdenv), "PPD=%s", ppdfile);
2801 +    putenv(ppdenv);
2802    }
2803  
2804   /*
2805 -  * Then wait for the backend to finish...
2806 +  * Then create a temporary file for printing...
2807    */
2808  
2809 -  request = xpc_dictionary_create(NULL, NULL, 0);
2810 -  xpc_dictionary_set_int64(request, "command", kPMWaitForJob);
2811 -  xpc_dictionary_set_fd(request, "stderr", 2);
2812 -
2813 -  sem      = dispatch_semaphore_create(0);
2814 -  response = NULL;
2815 -
2816 -  xpc_connection_send_message_with_reply(conn, request,
2817 -                                         dispatch_get_global_queue(0,0),
2818 -                                        ^(xpc_object_t reply)
2819 -                                        {
2820 -                                          /* Save the response and wake up */
2821 -                                          if (xpc_get_type(reply)
2822 -                                                  == XPC_TYPE_DICTIONARY)
2823 -                                            response = xpc_retain(reply);
2824 -
2825 -                                          dispatch_semaphore_signal(sem);
2826 -                                        });
2827 -
2828 -  dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
2829 -  xpc_release(request);
2830 -  dispatch_release(sem);
2831 -
2832 -  if (response)
2833 +  if ((fd = cupsTempFd(pstmpname, sizeof(pstmpname))) < 0)
2834    {
2835 -    status = xpc_dictionary_get_int64(response, "status");
2836 +    _cupsLangPrintError("ERROR", _("Unable to create temporary file"));
2837 +    if (ppdfile)
2838 +      unlink(ppdfile);
2839 +    return (-1);
2840 +  }
2841  
2842 -    if (status == SIGTERM || status == SIGKILL || status == SIGPIPE)
2843 -    {
2844 -      fprintf(stderr, "DEBUG: Child terminated on signal %d.\n", status);
2845 -      status = CUPS_BACKEND_FAILED;
2846 -    }
2847 -    else if (WIFSIGNALED(status))
2848 -    {
2849 -      fprintf(stderr, "DEBUG: Child crashed on signal %d.\n", status);
2850 -      status = CUPS_BACKEND_STOP;
2851 -    }
2852 -    else if (WIFEXITED(status))
2853 -    {
2854 -      status = WEXITSTATUS(status);
2855 -      fprintf(stderr, "DEBUG: Child exited with status %d.\n", status);
2856 -    }
2857 + /*
2858 +  * Get the owner of the spool file - it is owned by the user we want to run
2859 +  * as...
2860 +  */
2861  
2862 -    xpc_release(response);
2863 -  }
2864 +  if (argv[6])
2865 +    stat(argv[6], &fileinfo);
2866    else
2867 -    _cupsLangPrintFilter(stderr, "ERROR",
2868 -                         _("Unable to get backend exit status."));
2869 +  {
2870 +   /*
2871 +    * Use the OSX defaults, as an up-stream filter created the PICT
2872 +    * file...
2873 +    */
2874  
2875 -  cleanup:
2876 -
2877 -  if (conn)
2878 -  {
2879 -    xpc_connection_suspend(conn);
2880 -    xpc_connection_cancel(conn);
2881 -    xpc_release(conn);
2882 +    fileinfo.st_uid = 1;
2883 +    fileinfo.st_gid = 80;
2884    }
2885  
2886 -  return (status);
2887 -}
2888 -#endif /* HAVE_GSSAPI && HAVE_XPC */
2889 +  if (ppdfile)
2890 +    chown(ppdfile, fileinfo.st_uid, fileinfo.st_gid);
2891  
2892 +  fchown(fd, fileinfo.st_uid, fileinfo.st_gid);
2893  
2894 -/*
2895 - * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
2896 - */
2897 + /*
2898 +  * Finally, run the filter to convert the file...
2899 +  */
2900  
2901 -static void
2902 -sigterm_handler(int sig)               /* I - Signal */
2903 -{
2904 -  (void)sig;   /* remove compiler warnings... */
2905 -
2906 -#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
2907 -  if (child_pid)
2908 +  if ((pid = fork()) == 0)
2909    {
2910 -    kill(child_pid, sig);
2911 -    child_pid = 0;
2912 -  }
2913 -#endif /* HAVE_GSSAPI && HAVE_XPC */
2914 -
2915 -  if (!job_canceled)
2916 -  {
2917     /*
2918 -    * Flag that the job should be canceled...
2919 +    * Child process for pictwpstops...  Redirect output of pictwpstops to a
2920 +    * file...
2921      */
2922  
2923 -    job_canceled = 1;
2924 -    return;
2925 -  }
2926 +    dup2(fd, 1);
2927 +    close(fd);
2928  
2929 - /*
2930 -  * The scheduler already tried to cancel us once, now just terminate
2931 -  * after removing our temp file!
2932 -  */
2933 +    if (!getuid())
2934 +    {
2935 +     /*
2936 +      * Change to an unpriviledged user...
2937 +      */
2938  
2939 -  if (tmpfilename[0])
2940 -    unlink(tmpfilename);
2941 +      if (setgid(fileinfo.st_gid))
2942 +        return (errno);
2943  
2944 -  exit(1);
2945 -}
2946 +      if (setuid(fileinfo.st_uid))
2947 +        return (errno);
2948 +    }
2949  
2950 +    execlp("pictwpstops", printer, argv[1], argv[2], argv[3], argv[4], argv[5],
2951 +           filename, NULL);
2952 +    _cupsLangPrintf(stderr, _("ERROR: Unable to exec pictwpstops: %s\n"),
2953 +                   strerror(errno));
2954 +    return (errno);
2955 +  }
2956  
2957 -/*
2958 - * 'timeout_cb()' - Handle HTTP timeouts.
2959 - */
2960 +  close(fd);
2961  
2962 -static int                             /* O - 1 to continue, 0 to cancel */
2963 -timeout_cb(http_t *http,               /* I - Connection to server (unused) */
2964 -           void   *user_data)          /* I - User data (unused) */
2965 -{
2966 -  (void)http;
2967 -  (void)user_data;
2968 +  if (pid < 0)
2969 +  {
2970 +   /*
2971 +    * Error!
2972 +    */
2973  
2974 -  return (!job_canceled);
2975 -}
2976 +    _cupsLangPrintf(stderr, _("ERROR: Unable to fork pictwpstops: %s\n"),
2977 +                   strerror(errno));
2978 +    if (ppdfile)
2979 +      unlink(ppdfile);
2980 +    return (-1);
2981 +  }
2982  
2983 -
2984 -/*
2985 - * 'update_reasons()' - Update the printer-state-reasons values.
2986 - */
2987 -
2988 -static void
2989 -update_reasons(ipp_attribute_t *attr,  /* I - printer-state-reasons or NULL */
2990 -               const char      *s)     /* I - STATE: string or NULL */
2991 -{
2992 -  char         op;                     /* Add (+), remove (-), replace (\0) */
2993 -  cups_array_t *new_reasons;           /* New reasons array */
2994 -  char         *reason,                /* Current reason */
2995 -               add[2048],              /* Reasons added string */
2996 -               *addptr,                /* Pointer into add string */
2997 -               rem[2048],              /* Reasons removed string */
2998 -               *remptr;                /* Pointer into remove string */
2999 -  const char   *addprefix,             /* Current add string prefix */
3000 -               *remprefix;             /* Current remove string prefix */
3001 -
3002 -
3003 -  fprintf(stderr, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3004 -         attr ? attr->num_values : 0, attr ? attr->values[0].string.text : "",
3005 -         attr && attr->num_values > 1 ? ",..." : "", s ? s : "(null)");
3006 -
3007   /*
3008 -  * Create an array of new reason keyword strings...
3009 +  * Now wait for the filter to complete...
3010    */
3011  
3012 -  if (attr)
3013 +  if (wait(&status) < 0)
3014    {
3015 -    int        i;                              /* Looping var */
3016 +    _cupsLangPrintf(stderr, _("ERROR: Unable to wait for pictwpstops: %s\n"),
3017 +                   strerror(errno));
3018 +    close(fd);
3019 +    if (ppdfile)
3020 +      unlink(ppdfile);
3021 +    return (-1);
3022 +  }
3023  
3024 -    new_reasons = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3025 -    op          = '\0';
3026 +  if (ppdfile)
3027 +    unlink(ppdfile);
3028  
3029 -    for (i = 0; i < attr->num_values; i ++)
3030 -    {
3031 -      reason = attr->values[i].string.text;
3032 +  close(fd);
3033  
3034 -      if (strcmp(reason, "none") &&
3035 -         strcmp(reason, "none-report") &&
3036 -         strcmp(reason, "paused") &&
3037 -         strncmp(reason, "spool-area-full", 15) &&
3038 -         strcmp(reason, "com.apple.print.recoverable-warning") &&
3039 -         strncmp(reason, "cups-", 5))
3040 -       cupsArrayAdd(new_reasons, reason);
3041 -    }
3042 -  }
3043 -  else if (s)
3044 +  if (status)
3045    {
3046 -    if (*s == '+' || *s == '-')
3047 -      op = *s++;
3048 +    if (status >= 256)
3049 +      _cupsLangPrintf(stderr, _("ERROR: pictwpstops exited with status %d!\n"),
3050 +                     status / 256);
3051      else
3052 -      op = '\0';
3053 +      _cupsLangPrintf(stderr, _("ERROR: pictwpstops exited on signal %d!\n"),
3054 +                     status);
3055  
3056 -    new_reasons = _cupsArrayNewStrings(s);
3057 +    return (status);
3058    }
3059 -  else
3060 -    return;
3061  
3062   /*
3063 -  * Compute the changes...
3064 +  * Return with no errors..
3065    */
3066  
3067 -  add[0]    = '\0';
3068 -  addprefix = "STATE: +";
3069 -  addptr    = add;
3070 -  rem[0]    = '\0';
3071 -  remprefix = "STATE: -";
3072 -  remptr    = rem;
3073 +  return (0);
3074 +}
3075 +#endif /* __APPLE__ */
3076  
3077 -  fprintf(stderr, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3078 -          op ? op : ' ', cupsArrayCount(new_reasons),
3079 -         cupsArrayCount(state_reasons));
3080  
3081 -  _cupsMutexLock(&report_mutex);
3082 +/*
3083 + * 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
3084 + */
3085  
3086 -  if (op == '+')
3087 -  {
3088 -   /*
3089 -    * Add reasons...
3090 -    */
3091 +static void
3092 +sigterm_handler(int sig)               /* I - Signal */
3093 +{
3094 +  (void)sig;   /* remove compiler warnings... */
3095  
3096 -    for (reason = (char *)cupsArrayFirst(new_reasons);
3097 -        reason;
3098 -        reason = (char *)cupsArrayNext(new_reasons))
3099 -    {
3100 -      if (!cupsArrayFind(state_reasons, reason))
3101 -      {
3102 -        if (!strncmp(reason, "cups-remote-", 12))
3103 -       {
3104 -        /*
3105 -         * If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3106 -         * keywords...
3107 -         */
3108 -
3109 -         char  *temp;          /* Current reason in state_reasons */
3110 -
3111 -         cupsArraySave(state_reasons);
3112 -
3113 -         for (temp = (char *)cupsArrayFirst(state_reasons);
3114 -              temp;
3115 -              temp = (char *)cupsArrayNext(state_reasons))
3116 -           if (!strncmp(temp, "cups-remote-", 12))
3117 -           {
3118 -             snprintf(remptr, sizeof(rem) - (remptr - rem), "%s%s", remprefix,
3119 -                      temp);
3120 -             remptr    += strlen(remptr);
3121 -             remprefix = ",";
3122 -
3123 -             cupsArrayRemove(state_reasons, temp);
3124 -             break;
3125 -           }
3126 -
3127 -         cupsArrayRestore(state_reasons);
3128 -       }
3129 -
3130 -        cupsArrayAdd(state_reasons, reason);
3131 -
3132 -        snprintf(addptr, sizeof(add) - (addptr - add), "%s%s", addprefix,
3133 -                reason);
3134 -       addptr    += strlen(addptr);
3135 -       addprefix = ",";
3136 -      }
3137 -    }
3138 -  }
3139 -  else if (op == '-')
3140 +  if (!job_cancelled)
3141    {
3142     /*
3143 -    * Remove reasons...
3144 +    * Flag that the job should be cancelled...
3145      */
3146  
3147 -    for (reason = (char *)cupsArrayFirst(new_reasons);
3148 -        reason;
3149 -        reason = (char *)cupsArrayNext(new_reasons))
3150 -    {
3151 -      if (cupsArrayFind(state_reasons, reason))
3152 -      {
3153 -       snprintf(remptr, sizeof(rem) - (remptr - rem), "%s%s", remprefix,
3154 -                reason);
3155 -       remptr    += strlen(remptr);
3156 -       remprefix = ",";
3157 -
3158 -        cupsArrayRemove(state_reasons, reason);
3159 -      }
3160 -    }
3161 +    job_cancelled = 1;
3162 +    return;
3163    }
3164 -  else
3165 -  {
3166 -   /*
3167 -    * Replace reasons...
3168 -    */
3169  
3170 -    for (reason = (char *)cupsArrayFirst(state_reasons);
3171 -        reason;
3172 -        reason = (char *)cupsArrayNext(state_reasons))
3173 -    {
3174 -      if (strncmp(reason, "cups-", 5) && !cupsArrayFind(new_reasons, reason))
3175 -      {
3176 -       snprintf(remptr, sizeof(rem) - (remptr - rem), "%s%s", remprefix,
3177 -                reason);
3178 -       remptr    += strlen(remptr);
3179 -       remprefix = ",";
3180 + /*
3181 +  * The scheduler already tried to cancel us once, now just terminate
3182 +  * after removing our temp files!
3183 +  */
3184  
3185 -        cupsArrayRemove(state_reasons, reason);
3186 -      }
3187 -    }
3188 +  if (tmpfilename[0])
3189 +    unlink(tmpfilename);
3190  
3191 -    for (reason = (char *)cupsArrayFirst(new_reasons);
3192 -        reason;
3193 -        reason = (char *)cupsArrayNext(new_reasons))
3194 -    {
3195 -      if (!cupsArrayFind(state_reasons, reason))
3196 -      {
3197 -        cupsArrayAdd(state_reasons, reason);
3198 +#ifdef __APPLE__
3199 +  if (pstmpname[0])
3200 +    unlink(pstmpname);
3201 +#endif /* __APPLE__ */
3202  
3203 -        snprintf(addptr, sizeof(add) - (addptr - add), "%s%s", addprefix,
3204 -                reason);
3205 -       addptr    += strlen(addptr);
3206 -       addprefix = ",";
3207 -      }
3208 -    }
3209 -  }
3210 +  exit(1);
3211 +}
3212  
3213 -  _cupsMutexUnlock(&report_mutex);
3214  
3215 - /*
3216 -  * Report changes and return...
3217 -  */
3218 -
3219 -  if (add[0] && rem[0])
3220 -    fprintf(stderr, "%s\n%s\n", add, rem);
3221 -  else if (add[0])
3222 -    fprintf(stderr, "%s\n", add);
3223 -  else if (rem[0])
3224 -    fprintf(stderr, "%s\n", rem);
3225 -}
3226 -
3227  /*
3228   * End of "$Id$".
3229   */
This page took 0.465684 seconds and 3 git commands to generate.