]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/runloop.c
Load cups into easysw/current.
[thirdparty/cups.git] / backend / runloop.c
index dc2890a89997cc39873cb1b6571dca57cc4a8905..a3499e95f219803846480400f357bbd9eba7a976 100644 (file)
@@ -1,31 +1,23 @@
 /*
- * "$Id: runloop.c 6403 2007-03-27 16:00:56Z mike $"
+ * "$Id: runloop.c 6649 2007-07-11 21:46:42Z mike $"
  *
- *   Common run loop API for the Common UNIX Printing System (CUPS).
+ *   Common run loop APIs for the Common UNIX Printing System (CUPS).
  *
+ *   Copyright 2007 by Apple Inc.
  *   Copyright 2006-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
  *   "LICENSE" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  *   This file is subject to the Apple OS-Developed Software exception.
  *
  * Contents:
  *
- *   backendRunLoop() - Read and write print and back-channel data.
+ *   backendDrainOutput() - Drain pending print data to the device.
+ *   backendRunLoop()     - Read and write print and back-channel data.
  */
 
 /*
 #endif /* __hpux */
 
 
+/*
+ * 'backendDrainOutput()' - Drain pending print data to the device.
+ */
+
+int                                    /* O - 0 on success, -1 on error */
+backendDrainOutput(int print_fd,       /* I - Print file descriptor */
+                   int device_fd)      /* I - Device file descriptor */
+{
+  int          nfds;                   /* Maximum file descriptor value + 1 */
+  fd_set       input;                  /* Input set for reading */
+  ssize_t      print_bytes,            /* Print bytes read */
+               bytes;                  /* Bytes written */
+  char         print_buffer[8192],     /* Print data buffer */
+               *print_ptr;             /* Pointer into print data buffer */
+  struct timeval timeout;              /* Timeout for read... */
+
+
+  fprintf(stderr, "DEBUG: backendDrainOutput(print_fd=%d, device_fd=%d)\n",
+          print_fd, device_fd);
+
+ /*
+  * Figure out the maximum file descriptor value to use with select()...
+  */
+
+  nfds = (print_fd > device_fd ? print_fd : device_fd) + 1;
+
+ /*
+  * Now loop until we are out of data from print_fd...
+  */
+
+  for (;;)
+  {
+   /*
+    * Use select() to determine whether we have data to copy around...
+    */
+
+    FD_ZERO(&input);
+    FD_SET(print_fd, &input);
+
+    timeout.tv_sec  = 0;
+    timeout.tv_usec = 0;
+
+    if (select(nfds, &input, NULL, NULL, &timeout) < 0)
+      return (-1);
+
+    if (!FD_ISSET(print_fd, &input))
+      return (0);
+
+    if ((print_bytes = read(print_fd, print_buffer,
+                           sizeof(print_buffer))) < 0)
+    {
+     /*
+      * Read error - bail if we don't see EAGAIN or EINTR...
+      */
+
+      if (errno != EAGAIN || errno != EINTR)
+      {
+       perror("ERROR: Unable to read print data");
+       return (-1);
+      }
+
+      print_bytes = 0;
+    }
+    else if (print_bytes == 0)
+    {
+     /*
+      * End of file, return...
+      */
+
+      return (0);
+    }
+
+    fprintf(stderr, "DEBUG: Read %d bytes of print data...\n",
+           (int)print_bytes);
+
+    for (print_ptr = print_buffer; print_bytes > 0;)
+    {
+      if ((bytes = write(device_fd, print_ptr, print_bytes)) < 0)
+      {
+       /*
+        * Write error - bail if we don't see an error we can retry...
+       */
+
+        if (errno != ENOSPC && errno != ENXIO && errno != EAGAIN &&
+           errno != EINTR && errno != ENOTTY)
+       {
+         fprintf(stderr, _("ERROR: Unable to write print data: %s\n"),
+                 strerror(errno));
+         return (-1);
+       }
+      }
+      else
+      {
+        fprintf(stderr, "DEBUG: Wrote %d bytes of print data...\n", (int)bytes);
+
+        print_bytes -= bytes;
+       print_ptr   += bytes;
+      }
+    }
+  }
+}
+
+
 /*
  * 'backendRunLoop()' - Read and write print and back-channel data.
  */
@@ -69,8 +164,9 @@ backendRunLoop(
 
 
   fprintf(stderr,
-          "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d)\n",
-          print_fd, device_fd, use_bc);
+          "DEBUG: backendRunLoop(print_fd=%d, device_fd=%d, use_bc=%d, "
+         "side_cb=%p)\n",
+          print_fd, device_fd, use_bc, side_cb);
 
  /*
   * If we are printing data from a print driver on stdin, ignore SIGTERM
@@ -104,7 +200,8 @@ backendRunLoop(
   * Now loop until we are out of data from print_fd...
   */
 
-  for (print_bytes = 0, print_ptr = print_buffer, offline = 0, paperout = 0, total_bytes = 0;;)
+  for (print_bytes = 0, print_ptr = print_buffer, offline = -1,
+           paperout = -1, total_bytes = 0;;)
   {
    /*
     * Use select() to determine whether we have data to copy around...
@@ -130,12 +227,18 @@ backendRunLoop(
        * Pause printing to clear any pending errors...
        */
 
-       if (errno == ENXIO && !offline)
+       if (errno == ENXIO && offline != 1)
        {
          fputs("STATE: +offline-error\n", stderr);
          fputs(_("INFO: Printer is currently off-line.\n"), stderr);
          offline = 1;
        }
+       else if (errno == EINTR && total_bytes == 0)
+       {
+         fputs("DEBUG: Received an interrupt before any bytes were "
+               "written, aborting!\n", stderr);
+          return (0);
+       }
 
        sleep(1);
        continue;
@@ -215,7 +318,7 @@ backendRunLoop(
 
         if (errno == ENOSPC)
        {
-         if (!paperout)
+         if (paperout != 1)
          {
            fputs("STATE: +media-empty-error\n", stderr);
            fputs(_("ERROR: Out of paper!\n"), stderr);
@@ -224,7 +327,7 @@ backendRunLoop(
         }
        else if (errno == ENXIO)
        {
-         if (!offline)
+         if (offline != 1)
          {
            fputs("STATE: +offline-error\n", stderr);
            fputs(_("INFO: Printer is currently off-line.\n"), stderr);
@@ -271,5 +374,5 @@ backendRunLoop(
 
 
 /*
- * End of "$Id: runloop.c 6403 2007-03-27 16:00:56Z mike $".
+ * End of "$Id: runloop.c 6649 2007-07-11 21:46:42Z mike $".
  */