]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/ipp.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / backend / ipp.c
index 95e598252a67e8b2a4b3a66f8842ddae96eff1be..5113ac7273eb9b62051e26212380d2d0de7d1eb7 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: ipp.c 7018 2007-10-10 22:14:03Z mike $"
+ * "$Id: ipp.c 7583 2008-05-16 17:47:16Z mike $"
  *
  *   IPP backend for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 2007 by Apple Inc.
+ *   Copyright 2007-2008 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
@@ -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.
  */
 
 #include <cups/http-private.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
+#include "backend-private.h"
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <cups/backend.h>
-#include <cups/cups.h>
-#include <cups/language.h>
-#include <cups/i18n.h>
-#include <cups/string.h>
-#include <signal.h>
 #include <sys/wait.h>
 
 /*
@@ -72,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__
@@ -96,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 */
@@ -105,6 +100,9 @@ main(int  argc,                             /* I - Number of command-line args */
                *name,                  /* Name of option */
                *value,                 /* Value of option */
                sep;                    /* Separator character */
+  int          snmp_fd,                /* SNMP socket */
+               start_count,            /* Page count via SNMP at start */
+               page_count;             /* Page count via SNMP */
   int          num_files;              /* Number of files to print */
   char         **files,                /* Files to print */
                *filename;              /* Pointer to single filename */
@@ -142,6 +140,11 @@ main(int  argc,                            /* I - Number of command-line args */
                {                       /* Printer attributes we want */
                  "copies-supported",
                  "document-format-supported",
+                 "marker-colors",
+                 "marker-levels",
+                 "marker-message",
+                 "marker-names",
+                 "marker-types",
                  "printer-is-accepting-jobs",
                  "printer-state",
                  "printer-state-message",
@@ -225,7 +228,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, cupsBackendDeviceURI(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)
@@ -403,33 +409,49 @@ 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)
     {
-      perror("ERROR: unable to create temporary file");
+      _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
       return (CUPS_BACKEND_FAILED);
     }
 
     if ((fp = cupsFileOpenFd(fd, compression ? "w9" : "w")) == NULL)
     {
-      perror("ERROR: unable to open temporary file");
+      _cupsLangPrintError(_("ERROR: Unable to open temporary file"));
       close(fd);
       unlink(tmpfilename);
       return (CUPS_BACKEND_FAILED);
     }
 
+    tbytes = 0;
+
     while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
       if (cupsFileWrite(fp, buffer, bytes) < bytes)
       {
-        perror("ERROR: unable to write to temporary file");
+        _cupsLangPrintError(_("ERROR: Unable to write to temporary file"));
        cupsFileClose(fp);
        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...
     */
@@ -521,8 +543,8 @@ main(int  argc,                             /* I - Number of command-line args */
                      _("INFO: Unable to contact printer, queuing on next "
                        "printer in class...\n"));
 
-        if (argc == 6 || strcmp(filename, argv[6]))
-         unlink(filename);
+        if (tmpfilename[0])
+         unlink(tmpfilename);
 
        /*
         * Sleep 5 seconds to keep the job from requeuing too rapidly...
@@ -579,8 +601,8 @@ main(int  argc,                             /* I - Number of command-line args */
 
   if (job_cancelled)
   {
-    if (argc == 6 || strcmp(filename, argv[6]))
-      unlink(filename);
+    if (tmpfilename[0])
+      unlink(tmpfilename);
 
     return (CUPS_BACKEND_FAILED);
   }
@@ -600,6 +622,25 @@ main(int  argc,                            /* I - Number of command-line args */
              httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
              ntohs(http->hostaddr->ipv4.sin_port));
 
+ /*
+  * See if the printer supports SNMP...
+  */
+
+  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;
+    }
+  }
+  else
+    start_count = 0;
+
  /*
   * Build a URI for the printer and fill the standard IPP attributes for
   * an IPP_PRINT_FILE request.  We can't use the URI in argv[0] because it
@@ -620,6 +661,12 @@ main(int  argc,                            /* I - Number of command-line args */
 
   do
   {
+   /*
+    * Check for side-channel requests...
+    */
+
+    backendCheckSideChannel(snmp_fd, http->hostaddr);
+
    /*
     * Build the IPP request...
     */
@@ -765,8 +812,8 @@ main(int  argc,                             /* I - Number of command-line args */
       ippDelete(supported);
       httpClose(http);
 
-      if (argc == 6 || strcmp(filename, argv[6]))
-       unlink(filename);
+      if (tmpfilename[0])
+       unlink(tmpfilename);
 
      /*
       * Sleep 5 seconds to keep the job from requeuing too rapidly...
@@ -814,6 +861,12 @@ main(int  argc,                            /* I - Number of command-line args */
 
   while (copies_remaining > 0)
   {
+   /*
+    * Check for side-channel requests...
+    */
+
+    backendCheckSideChannel(snmp_fd, http->hostaddr);
+
    /*
     * Build the IPP request...
     */
@@ -865,12 +918,13 @@ main(int  argc,                           /* I - Number of command-line args */
     num_options = cupsParseOptions(argv[5], 0, &options);
 
 #ifdef __APPLE__
-    if (!strcasecmp(content_type, "application/pictwps") && num_files == 1)
+    if (!strcasecmp(final_content_type, "application/pictwps") &&
+        num_files == 1)
     {
       if (format_sup != NULL)
       {
        for (i = 0; i < format_sup->num_values; i ++)
-         if (!strcasecmp(content_type, format_sup->values[i].string.text))
+         if (!strcasecmp(final_content_type, format_sup->values[i].string.text))
            break;
       }
 
@@ -881,10 +935,18 @@ main(int  argc,                           /* I - Number of command-line args */
        * so convert the document to PostScript...
        */
 
-       if (run_pictwps_filter(argv, filename))
+       if (run_pictwps_filter(argv, files[0]))
+       {
+         if (pstmpname[0])
+           unlink(pstmpname);
+
+         if (tmpfilename[0])
+           unlink(tmpfilename);
+
          return (CUPS_BACKEND_FAILED);
+        }
 
-        filename = pstmpname;
+        files[0] = pstmpname;
 
        /*
        * Change the MIME type to application/postscript and change the
@@ -1004,6 +1066,16 @@ main(int  argc,                          /* I - Number of command-line args */
     {
       for (i = 0; i < num_files; i ++)
       {
+       /*
+       * Check for side-channel requests...
+       */
+
+       backendCheckSideChannel(snmp_fd, http->hostaddr);
+
+       /*
+        * Send the next file in the job...
+       */
+
        request = ippNewRequest(IPP_SEND_DOCUMENT);
 
        request->request.op.version[1] = version;
@@ -1060,6 +1132,12 @@ main(int  argc,                          /* I - Number of command-line args */
 
     for (delay = 1; !job_cancelled;)
     {
+     /*
+      * Check for side-channel requests...
+      */
+
+      backendCheckSideChannel(snmp_fd, http->hostaddr);
+
      /*
       * Build an IPP_GET_JOB_ATTRIBUTES request...
       */
@@ -1173,6 +1251,15 @@ main(int  argc,                          /* I - Number of command-line args */
 
   check_printer_state(http, uri, resource, argv[2], version, job_id);
 
+ /*
+  * Collect the final page count as needed...
+  */
+
+  if (snmp_fd >= 0 && 
+      !backendSNMPSupplies(snmp_fd, http->hostaddr, &page_count, NULL) &&
+      page_count > start_count)
+    fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
+
  /*
   * Free memory...
   */
@@ -1279,6 +1366,11 @@ check_printer_state(
        *response;                      /* IPP response */
   static const char * const attrs[] =  /* Attributes we want */
   {
+    "marker-colors",
+    "marker-levels",
+    "marker-message",
+    "marker-names",
+    "marker-types",
     "printer-state-message",
     "printer-state-reasons"
   };
@@ -1401,7 +1493,7 @@ password_cb(const char *prompt)           /* I - Prompt (not used) */
 {
   (void)prompt;
 
-  if (password && password_tries < 3)
+  if (password && *password && password_tries < 3)
   {
     password_tries ++;
 
@@ -1431,6 +1523,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.
  */
@@ -1441,8 +1601,9 @@ 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      *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 */
@@ -1484,7 +1645,7 @@ report_printer_state(ipp_t *ipp,  /* I - IPP response */
     else if (!strncmp(reason, "moving-to-paused", 16) ||
              !strncmp(reason, "paused", 6) ||
             !strncmp(reason, "shutdown", 8))
-      message = _("Printer off-line.");
+      message = _("Printer offline.");
     else if (!strncmp(reason, "toner-low", 9))
       message = _("Toner low.");
     else if (!strncmp(reason, "toner-empty", 11))
@@ -1549,6 +1710,22 @@ report_printer_state(ipp_t *ipp, /* I - IPP response */
 
   fprintf(stderr, "%s\n", state);
 
+ /*
+  * 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);
 }
 
@@ -1680,7 +1857,6 @@ run_pictwps_filter(char       **argv,     /* I - Command-line arguments */
 
     _cupsLangPrintf(stderr, _("ERROR: Unable to fork pictwpstops: %s\n"),
                    strerror(errno));
-    unlink(filename);
     if (ppdfile)
       unlink(ppdfile);
     return (-1);
@@ -1695,7 +1871,6 @@ run_pictwps_filter(char       **argv,     /* I - Command-line arguments */
     _cupsLangPrintf(stderr, _("ERROR: Unable to wait for pictwpstops: %s\n"),
                    strerror(errno));
     close(fd);
-    unlink(filename);
     if (ppdfile)
       unlink(ppdfile);
     return (-1);
@@ -1715,7 +1890,6 @@ run_pictwps_filter(char       **argv,     /* I - Command-line arguments */
       _cupsLangPrintf(stderr, _("ERROR: pictwpstops exited on signal %d!\n"),
                      status);
 
-    unlink(filename);
     return (status);
   }
 
@@ -1765,5 +1939,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 7583 2008-05-16 17:47:16Z mike $".
  */