]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/ipp.c
Merge changes from CUPS 1.4svn-r8177 (tentative CUPS 1.4b2)
[thirdparty/cups.git] / backend / ipp.c
index 4704741de49f9091e6b04a83b3b213d0a974e9cb..78b3ddb3bba0b0d50a9c510ab0f4e9df90a450c9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: ipp.c 7018 2007-10-10 22:14:03Z mike $"
+ * "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $"
  *
  *   IPP backend for the Common UNIX Printing System (CUPS).
  *
@@ -22,6 +22,7 @@
  *   compress_files()       - Compress print files...
  *   password_cb()          - Disable the password prompt for
  *                            cupsDoFileRequest().
+ *   report_attr()          - Report an IPP attribute value.
  *   report_printer_state() - Report the printer state.
  *   run_pictwps_filter()   - Convert PICT files to PostScript when printing
  *                            remotely.
@@ -64,6 +65,7 @@ static void   check_printer_state(http_t *http, const char *uri,
 static void    compress_files(int num_files, char **files);
 #endif /* HAVE_LIBZ */
 static const char *password_cb(const char *);
+static void    report_attr(ipp_attribute_t *attr);
 static int     report_printer_state(ipp_t *ipp, int job_id);
 
 #ifdef __APPLE__
@@ -88,6 +90,7 @@ main(int  argc,                               /* I - Number of command-line args */
   int          send_options;           /* Send job options? */
   int          num_options;            /* Number of printer options */
   cups_option_t        *options;               /* Printer options */
+  const char   *device_uri;            /* Device URI */
   char         method[255],            /* Method in URI */
                hostname[1024],         /* Hostname */
                username[255],          /* Username info */
@@ -99,7 +102,8 @@ main(int  argc,                              /* I - Number of command-line args */
                sep;                    /* Separator character */
   int          snmp_fd,                /* SNMP socket */
                start_count,            /* Page count via SNMP at start */
-               page_count;             /* Page count via SNMP */
+               page_count,             /* Page count via SNMP */
+               have_supplies;          /* Printer supports supply levels? */
   int          num_files;              /* Number of files to print */
   char         **files,                /* Files to print */
                *filename;              /* Pointer to single filename */
@@ -135,8 +139,14 @@ main(int  argc,                            /* I - Number of command-line args */
   int          version;                /* IPP version */
   static const char * const pattrs[] =
                {                       /* Printer attributes we want */
+                  "com.apple.print.recoverable-message",
                  "copies-supported",
                  "document-format-supported",
+                 "marker-colors",
+                 "marker-levels",
+                 "marker-message",
+                 "marker-names",
+                 "marker-types",
                  "printer-is-accepting-jobs",
                  "printer-state",
                  "printer-state-message",
@@ -189,8 +199,9 @@ main(int  argc,                             /* I - Number of command-line args */
     else
       s = argv[0];
 
-    printf("network %s \"Unknown\" \"Internet Printing Protocol (%s)\"\n",
-           s, s);
+    printf("network %s \"Unknown\" \"%s (%s)\"\n",
+           s, _cupsLangString(cupsLangDefault(),
+                             _("Internet Printing Protocol")), s);
     return (CUPS_BACKEND_OK);
   }
   else if (argc < 6)
@@ -220,7 +231,10 @@ main(int  argc,                            /* I - Number of command-line args */
   * Extract the hostname and printer name from the URI...
   */
 
-  if (httpSeparateURI(HTTP_URI_CODING_ALL, backendResolveURI(argv),
+  if ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
+    return (CUPS_BACKEND_FAILED);
+
+  if (httpSeparateURI(HTTP_URI_CODING_ALL, device_uri,
                       method, sizeof(method), username, sizeof(username),
                      hostname, sizeof(hostname), &port,
                      resource, sizeof(resource)) < HTTP_URI_OK)
@@ -398,6 +412,7 @@ main(int  argc,                             /* I - Number of command-line args */
     cups_file_t        *fp;                    /* Temporary file */
     char       buffer[8192];           /* Buffer for copying */
     int                bytes;                  /* Number of bytes read */
+    off_t      tbytes;                 /* Total bytes copied */
 
 
     if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
@@ -414,6 +429,8 @@ main(int  argc,                             /* I - Number of command-line args */
       return (CUPS_BACKEND_FAILED);
     }
 
+    tbytes = 0;
+
     while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
       if (cupsFileWrite(fp, buffer, bytes) < bytes)
       {
@@ -422,9 +439,22 @@ main(int  argc,                            /* I - Number of command-line args */
        unlink(tmpfilename);
        return (CUPS_BACKEND_FAILED);
       }
+      else
+        tbytes += bytes;
 
     cupsFileClose(fp);
 
+   /*
+    * Don't try printing files less than 2 bytes...
+    */
+
+    if (tbytes <= 1)
+    {
+      _cupsLangPuts(stderr, _("ERROR: Empty print file!\n"));
+      unlink(tmpfilename);
+      return (CUPS_BACKEND_FAILED);
+    }
+
    /*
     * Point to the single file from stdin...
     */
@@ -495,8 +525,9 @@ main(int  argc,                             /* I - Number of command-line args */
 
   do
   {
-    _cupsLangPrintf(stderr, _("INFO: Connecting to %s on port %d...\n"),
-                   hostname, port);
+    fprintf(stderr, "DEBUG: Connecting to %s:%d\n",
+           hostname, port);
+    _cupsLangPuts(stderr, _("INFO: Connecting to printer...\n"));
 
     if ((http = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL)
     {
@@ -581,7 +612,7 @@ main(int  argc,                             /* I - Number of command-line args */
   }
 
   fputs("STATE: -connecting-to-device\n", stderr);
-  _cupsLangPrintf(stderr, _("INFO: Connected to %s...\n"), hostname);
+  _cupsLangPuts(stderr, _("INFO: Connected to printer...\n"));
 
 #ifdef AF_INET6
   if (http->hostaddr->addr.sa_family == AF_INET6)
@@ -600,15 +631,10 @@ main(int  argc,                           /* I - Number of command-line args */
   */
 
   if ((snmp_fd = _cupsSNMPOpen(http->hostaddr->addr.sa_family)) >= 0)
-    if (backendSNMPSupplies(snmp_fd, http->hostaddr, &start_count, NULL))
-    {
-     /*
-      * No, close it...
-      */
-
-      _cupsSNMPClose(snmp_fd);
-      snmp_fd = -1;
-    }
+    have_supplies = !backendSNMPSupplies(snmp_fd, http->hostaddr, &start_count,
+                                         NULL);
+  else
+    have_supplies = start_count = 0;
 
  /*
   * Build a URI for the printer and fill the standard IPP attributes for
@@ -1086,7 +1112,7 @@ main(int  argc,                           /* I - Number of command-line args */
     }
     else if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
             ipp_status == IPP_PRINTER_BUSY)
-      break;
+      continue;
     else
       copies_remaining --;
 
@@ -1224,7 +1250,7 @@ main(int  argc,                           /* I - Number of command-line args */
   * Collect the final page count as needed...
   */
 
-  if (snmp_fd >= 0 && 
+  if (have_supplies && 
       !backendSNMPSupplies(snmp_fd, http->hostaddr, &page_count, NULL) &&
       page_count > start_count)
     fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
@@ -1335,6 +1361,12 @@ check_printer_state(
        *response;                      /* IPP response */
   static const char * const attrs[] =  /* Attributes we want */
   {
+    "com.apple.print.recoverable-message",
+    "marker-colors",
+    "marker-levels",
+    "marker-message",
+    "marker-names",
+    "marker-types",
     "printer-state-message",
     "printer-state-reasons"
   };
@@ -1487,6 +1519,74 @@ password_cb(const char *prompt)          /* I - Prompt (not used) */
 }
 
 
+/*
+ * 'report_attr()' - Report an IPP attribute value.
+ */
+
+static void
+report_attr(ipp_attribute_t *attr)     /* I - Attribute */
+{
+  int  i;                              /* Looping var */
+  char value[1024],                    /* Value string */
+       *valptr,                        /* Pointer into value string */
+       *attrptr;                       /* Pointer into attribute value */
+
+
+ /*
+  * Convert the attribute values into quoted strings...
+  */
+
+  for (i = 0, valptr = value;
+       i < attr->num_values && valptr < (value + sizeof(value) - 10);
+       i ++)
+  {
+    if (i > 0)
+      *valptr++ = ',';
+
+    switch (attr->value_tag)
+    {
+      case IPP_TAG_INTEGER :
+      case IPP_TAG_ENUM :
+          snprintf(valptr, sizeof(value) - (valptr - value), "%d",
+                  attr->values[i].integer);
+         valptr += strlen(valptr);
+         break;
+
+      case IPP_TAG_TEXT :
+      case IPP_TAG_NAME :
+      case IPP_TAG_KEYWORD :
+          *valptr++ = '\"';
+         for (attrptr = attr->values[i].string.text;
+              *attrptr && valptr < (value + sizeof(value) - 10);
+              attrptr ++)
+         {
+           if (*attrptr == '\\' || *attrptr == '\"')
+             *valptr++ = '\\';
+
+           *valptr++ = *attrptr;
+         }
+          *valptr++ = '\"';
+          break;
+
+      default :
+         /*
+         * Unsupported value type...
+         */
+
+          return;
+    }
+  }
+
+  *valptr = '\0';
+
+ /*
+  * Tell the scheduler about the new values...
+  */
+
+  fprintf(stderr, "ATTR: %s=%s\n", attr->name, value);
+}
+
+
 /*
  * 'report_printer_state()' - Report the printer state.
  */
@@ -1497,14 +1597,17 @@ report_printer_state(ipp_t *ipp,        /* I - IPP response */
 {
   int                  i;              /* Looping var */
   int                  count;          /* Count of reasons shown... */
-  ipp_attribute_t      *psm,           /* pritner-state-message */
-                       *reasons;       /* printer-state-reasons */
+  ipp_attribute_t      *caprm,         /* com.apple.print.recoverable-message */
+                       *psm,           /* printer-state-message */
+                       *reasons,       /* printer-state-reasons */
+                       *marker;        /* marker-* attributes */
   const char           *reason;        /* Current reason */
   const char           *message;       /* Message to show */
   char                 unknown[1024];  /* Unknown message string */
   const char           *prefix;        /* Prefix for STATE: line */
   char                 state[1024];    /* State string */
   cups_lang_t          *language;      /* Current localization */
+  int                  saw_caprw;      /* Saw com.apple.print.recoverable-warning state */
 
 
   if ((psm = ippFindAttribute(ipp, "printer-state-message",
@@ -1515,15 +1618,16 @@ report_printer_state(ipp_t *ipp,        /* I - IPP response */
                                   IPP_TAG_KEYWORD)) == NULL)
     return (0);
 
-  state[0] = '\0';
-  prefix   = "STATE: ";
-  language = cupsLangDefault();
+  saw_caprw = 0;
+  state[0]  = '\0';
+  prefix    = "STATE: ";
+  language  = cupsLangDefault();
 
   for (i = 0, count = 0; i < reasons->num_values; i ++)
   {
     reason = reasons->values[i].string.text;
 
-    if (job_id == 0 || strcmp(reason, "paused"))
+    if (strcmp(reason, "paused"))
     {
       strlcat(state, prefix, sizeof(state));
       strlcat(state, reason, sizeof(state));
@@ -1538,6 +1642,7 @@ report_printer_state(ipp_t *ipp,  /* I - IPP response */
     else if (!strncmp(reason, "media-jam", 9))
       message = _("Media jam!");
     else if (!strncmp(reason, "moving-to-paused", 16) ||
+             !strncmp(reason, "offline", 7) ||
              !strncmp(reason, "paused", 6) ||
             !strncmp(reason, "shutdown", 8))
       message = _("Printer offline.");
@@ -1583,6 +1688,8 @@ report_printer_state(ipp_t *ipp,  /* I - IPP response */
       message = _("Developer almost empty.");
     else if (!strncmp(reason, "developer-empty", 15))
       message = _("Developer empty!");
+    else if (!strcmp(reason, "com.apple.print.recoverable-warning"))
+      saw_caprw = 1;
     else if (strstr(reason, "error") != NULL)
     {
       message = unknown;
@@ -1605,6 +1712,32 @@ report_printer_state(ipp_t *ipp, /* I - IPP response */
 
   fprintf(stderr, "%s\n", state);
 
+ /*
+  * Relay com.apple.print.recoverable-message...
+  */
+
+  if ((caprm = ippFindAttribute(ipp, "com.apple.print.recoverable-message",
+                                IPP_TAG_TEXT)) != NULL)
+    fprintf(stderr, "WARNING: %s: %s\n",
+            saw_caprw ? "recoverable" : "recovered",
+           caprm->values[0].string.text);
+
+ /*
+  * Relay the current marker-* attribute values...
+  */
+
+  if ((marker = ippFindAttribute(ipp, "marker-colors", IPP_TAG_NAME)) != NULL)
+    report_attr(marker);
+  if ((marker = ippFindAttribute(ipp, "marker-levels",
+                                 IPP_TAG_INTEGER)) != NULL)
+    report_attr(marker);
+  if ((marker = ippFindAttribute(ipp, "marker-message", IPP_TAG_TEXT)) != NULL)
+    report_attr(marker);
+  if ((marker = ippFindAttribute(ipp, "marker-names", IPP_TAG_NAME)) != NULL)
+    report_attr(marker);
+  if ((marker = ippFindAttribute(ipp, "marker-types", IPP_TAG_KEYWORD)) != NULL)
+    report_attr(marker);
+
   return (count);
 }
 
@@ -1818,5 +1951,5 @@ sigterm_handler(int sig)          /* I - Signal */
 
 
 /*
- * End of "$Id: ipp.c 7018 2007-10-10 22:14:03Z mike $".
+ * End of "$Id: ipp.c 7948 2008-09-17 00:04:12Z mike $".
  */