]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - filter/rastertoepson.c
Fix source file header text duplication text duplication.
[thirdparty/cups.git] / filter / rastertoepson.c
index f5c8c9d67ace34c4fa6ee326bfa916bbb0164db9..4efe6692b9cb6482b3625d60f7cc1e0d9c0ab159 100644 (file)
@@ -1,29 +1,16 @@
 /*
- * "$Id: rastertoepson.c 6649 2007-07-11 21:46:42Z mike $"
+ * EPSON ESC/P and ESC/P2 filter for CUPS.
  *
- *   EPSON ESC/P and ESC/P2 filter for the Common UNIX Printing System
- *   (CUPS).
+ * Copyright 2007-2015 by Apple Inc.
+ * Copyright 1993-2007 by Easy Software Products.
  *
- *   Copyright 2007 by Apple Inc.
- *   Copyright 1993-2007 by Easy Software Products.
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file.  If this file is
+ * missing or damaged, see the license at "http://www.cups.org/".
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   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:
- *
- *   Setup()        - Prepare the printer for printing.
- *   StartPage()    - Start a page of graphics.
- *   EndPage()      - Finish a page of graphics.
- *   Shutdown()     - Shutdown the printer.
- *   CompressData() - Compress a line of graphics.
- *   OutputLine()   - Output a line of graphics.
- *   main()         - Main entry and processing of driver.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
 
 #include <cups/cups.h>
 #include <cups/ppd.h>
-#include <cups/string.h>
-#include <cups/i18n.h>
-#include "raster.h"
-#include <stdlib.h>
+#include <cups/string-private.h>
+#include <cups/language-private.h>
+#include <cups/raster.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -68,16 +54,17 @@ unsigned char       *Planes[6],             /* Output buffers */
                *CompBuffer,            /* Compression buffer */
                *LineBuffers[2];        /* Line bitmap buffers */
 int            Model,                  /* Model number */
-               NumPlanes,              /* Number of color planes */
+               EjectPage,              /* Eject the page when done? */
+               Shingling,              /* Shingle output? */
+               Canceled;               /* Has the current job been canceled? */
+unsigned       NumPlanes,              /* Number of color planes */
                Feed,                   /* Number of lines to skip */
-               EjectPage;              /* Eject the page when done? */
-int            DotBit,                 /* Bit in buffers */
+               DotBit,                 /* Bit in buffers */
                DotBytes,               /* # bytes in a dot column */
                DotColumns,             /* # columns in 1/60 inch */
                LineCount,              /* # of lines processed */
                EvenOffset,             /* Offset into 'even' buffers */
-               OddOffset,              /* Offset into 'odd' buffers */
-               Shingling;              /* Shingle output? */
+               OddOffset;              /* Offset into 'odd' buffers */
 
 
 /*
@@ -85,15 +72,15 @@ int         DotBit,                 /* Bit in buffers */
  */
 
 void   Setup(void);
-void   StartPage(const ppd_file_t *ppd, const cups_page_header_t *header);
-void   EndPage(const cups_page_header_t *header);
+void   StartPage(const ppd_file_t *ppd, const cups_page_header2_t *header);
+void   EndPage(const cups_page_header2_t *header);
 void   Shutdown(void);
 
 void   CancelJob(int sig);
-void   CompressData(const unsigned char *line, int length, int plane,
-                    int type, int xstep, int ystep);
-void   OutputLine(const cups_page_header_t *header);
-void   OutputRows(const cups_page_header_t *header, int row);
+void   CompressData(const unsigned char *line, unsigned length, unsigned plane,
+                    unsigned type, unsigned xstep, unsigned ystep);
+void   OutputLine(const cups_page_header2_t *header);
+void   OutputRows(const cups_page_header2_t *header, int row);
 
 
 /*
@@ -103,7 +90,7 @@ void OutputRows(const cups_page_header_t *header, int row);
 void
 Setup(void)
 {
-  const char   *device_uri;    /* The device for the printer... */
+  const char   *device_uri;            /* The device for the printer... */
 
 
  /*
@@ -122,38 +109,43 @@ Setup(void)
  */
 
 void
-StartPage(const ppd_file_t         *ppd,       /* I - PPD file */
-          const cups_page_header_t *header)    /* I - Page header */
+StartPage(
+    const ppd_file_t         *ppd,     /* I - PPD file */
+    const cups_page_header2_t *header) /* I - Page header */
 {
-  int  n, t;                                   /* Numbers */
-  int  plane;                                  /* Looping var */
-#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
-  struct sigaction action;                     /* Actions for POSIX signals */
-#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
+  int          n, t;                   /* Numbers */
+  unsigned     plane;                  /* Looping var */
 
 
  /*
-  * Register a signal handler to eject the current page if the
-  * job is cancelled.
+  * Show page device dictionary...
   */
 
-#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
-  sigset(SIGTERM, CancelJob);
-#elif defined(HAVE_SIGACTION)
-  memset(&action, 0, sizeof(action));
-
-  sigemptyset(&action.sa_mask);
-  action.sa_handler = CancelJob;
-  sigaction(SIGTERM, &action, NULL);
-#else
-  signal(SIGTERM, CancelJob);
-#endif /* HAVE_SIGSET */
+  fprintf(stderr, "DEBUG: StartPage...\n");
+  fprintf(stderr, "DEBUG: Duplex = %d\n", header->Duplex);
+  fprintf(stderr, "DEBUG: HWResolution = [ %d %d ]\n", header->HWResolution[0], header->HWResolution[1]);
+  fprintf(stderr, "DEBUG: ImagingBoundingBox = [ %d %d %d %d ]\n", header->ImagingBoundingBox[0], header->ImagingBoundingBox[1], header->ImagingBoundingBox[2], header->ImagingBoundingBox[3]);
+  fprintf(stderr, "DEBUG: Margins = [ %d %d ]\n", header->Margins[0], header->Margins[1]);
+  fprintf(stderr, "DEBUG: ManualFeed = %d\n", header->ManualFeed);
+  fprintf(stderr, "DEBUG: MediaPosition = %d\n", header->MediaPosition);
+  fprintf(stderr, "DEBUG: NumCopies = %d\n", header->NumCopies);
+  fprintf(stderr, "DEBUG: Orientation = %d\n", header->Orientation);
+  fprintf(stderr, "DEBUG: PageSize = [ %d %d ]\n", header->PageSize[0], header->PageSize[1]);
+  fprintf(stderr, "DEBUG: cupsWidth = %d\n", header->cupsWidth);
+  fprintf(stderr, "DEBUG: cupsHeight = %d\n", header->cupsHeight);
+  fprintf(stderr, "DEBUG: cupsMediaType = %d\n", header->cupsMediaType);
+  fprintf(stderr, "DEBUG: cupsBitsPerColor = %d\n", header->cupsBitsPerColor);
+  fprintf(stderr, "DEBUG: cupsBitsPerPixel = %d\n", header->cupsBitsPerPixel);
+  fprintf(stderr, "DEBUG: cupsBytesPerLine = %d\n", header->cupsBytesPerLine);
+  fprintf(stderr, "DEBUG: cupsColorOrder = %d\n", header->cupsColorOrder);
+  fprintf(stderr, "DEBUG: cupsColorSpace = %d\n", header->cupsColorSpace);
+  fprintf(stderr, "DEBUG: cupsCompression = %d\n", header->cupsCompression);
 
  /*
   * Send a reset sequence.
   */
 
-  if (ppd->nickname && strstr(ppd->nickname, "OKIDATA") != NULL)
+  if (ppd && ppd->nickname && strstr(ppd->nickname, "OKIDATA") != NULL)
     printf("\033{A");  /* Set EPSON emulation mode */
 
   printf("\033@");
@@ -162,9 +154,7 @@ StartPage(const ppd_file_t         *ppd,    /* I - PPD file */
   * See which type of printer we are using...
   */
 
-  EjectPage = header->Margins[0] || header->Margins[1];
-    
-  switch (ppd->model_number)
+  switch (Model)
   {
     case EPSON_9PIN :
     case EPSON_24PIN :
@@ -183,8 +173,8 @@ StartPage(const ppd_file_t         *ppd,    /* I - PPD file */
 
        printf("\033l%c\033Q%c", 0,     /* Side margins */
                       (int)(10.0 * header->PageSize[0] / 72.0 + 0.5));
-       printf("\033C%c%c", 0,          /* Page length */
-                      (int)(header->PageSize[1] / 72.0 + 0.5));
+       printf("\033\062\033C%c",       /* Page length in 1/6th inches */
+                     (int)(header->PageSize[1] / 12.0 + 0.5));
        printf("\033N%c", 0);           /* Bottom margin */
         printf("\033O");               /* No perforation skip */
 
@@ -196,7 +186,7 @@ StartPage(const ppd_file_t         *ppd,    /* I - PPD file */
        DotColumns = header->HWResolution[0] / 60;
         Shingling  = 0;
 
-        if (ppd->model_number == EPSON_9PIN)
+        if (Model == EPSON_9PIN)
          printf("\033\063\030");       /* Set line feed */
        else
          switch (header->HWResolution[0])
@@ -233,26 +223,28 @@ StartPage(const ppd_file_t         *ppd,  /* I - PPD file */
         if (Model < EPSON_ICOLOR)
        {
          pwrite("\033(U\001\000", 5);          /* Resolution/units */
-         putchar(3600 / header->HWResolution[1]);
+         putchar((int)(3600 / header->HWResolution[1]));
         }
        else
        {
          pwrite("\033(U\005\000", 5);
-         putchar(1440 / header->HWResolution[1]);
-         putchar(1440 / header->HWResolution[1]);
-         putchar(1440 / header->HWResolution[0]);
+         putchar((int)(1440 / header->HWResolution[1]));
+         putchar((int)(1440 / header->HWResolution[1]));
+         putchar((int)(1440 / header->HWResolution[0]));
          putchar(0xa0);        /* n/1440ths... */
          putchar(0x05);
        }
 
-       n = header->PageSize[1] * header->HWResolution[1] / 72.0;
+       n = (int)(header->PageSize[1] * header->HWResolution[1] / 72.0);
 
        pwrite("\033(C\002\000", 5);            /* Page length */
        putchar(n);
        putchar(n >> 8);
 
-       t = (ppd->sizes[1].length - ppd->sizes[1].top) *
-           header->HWResolution[1] / 72.0;
+        if (ppd)
+         t = (int)((ppd->sizes[1].length - ppd->sizes[1].top) * header->HWResolution[1] / 72.0);
+        else
+         t = 0;
 
        pwrite("\033(c\004\000", 5);            /* Top & bottom margins */
        putchar(t);
@@ -293,18 +285,34 @@ StartPage(const ppd_file_t         *ppd,  /* I - PPD file */
   * Allocate memory for a line/row of graphics...
   */
 
-  Planes[0] = malloc(header->cupsBytesPerLine);
+  if ((Planes[0] = malloc(header->cupsBytesPerLine + NumPlanes)) == NULL)
+  {
+    fputs("ERROR: Unable to allocate memory\n", stderr);
+    exit(1);
+  }
+
   for (plane = 1; plane < NumPlanes; plane ++)
     Planes[plane] = Planes[0] + plane * header->cupsBytesPerLine / NumPlanes;
 
   if (header->cupsCompression || DotBytes)
-    CompBuffer = calloc(2, header->cupsWidth);
+  {
+    if ((CompBuffer = calloc(2, header->cupsWidth + 1)) == NULL)
+    {
+      fputs("ERROR: Unable to allocate memory\n", stderr);
+      exit(1);
+    }
+  }
   else
     CompBuffer = NULL;
 
   if (DotBytes)
   {
-    LineBuffers[0] = calloc(DotBytes, header->cupsWidth * (Shingling + 1));
+    if ((LineBuffers[0] = calloc((size_t)DotBytes, header->cupsWidth * (size_t)(Shingling + 1))) == NULL)
+    {
+      fputs("ERROR: Unable to allocate memory\n", stderr);
+      exit(1);
+    }
+
     LineBuffers[1] = LineBuffers[0] + DotBytes * header->cupsWidth;
     DotBit         = 128;
     LineCount      = 0;
@@ -319,13 +327,9 @@ StartPage(const ppd_file_t         *ppd,   /* I - PPD file */
  */
 
 void
-EndPage(const cups_page_header_t *header)      /* I - Page header */
+EndPage(
+    const cups_page_header2_t *header) /* I - Page header */
 {
-#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
-  struct sigaction action;                     /* Actions for POSIX signals */
-#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
-
-
   if (DotBytes && header)
   {
    /*
@@ -353,26 +357,9 @@ EndPage(const cups_page_header_t *header)  /* I - Page header */
   * Eject the current page...
   */
 
-  if (EjectPage)
-    putchar(12);               /* Form feed */
+  putchar(12);                         /* Form feed */
   fflush(stdout);
 
- /*
-  * Unregister the signal handler...
-  */
-
-#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
-  sigset(SIGTERM, SIG_IGN);
-#elif defined(HAVE_SIGACTION)
-  memset(&action, 0, sizeof(action));
-
-  sigemptyset(&action.sa_mask);
-  action.sa_handler = SIG_IGN;
-  sigaction(SIGTERM, &action, NULL);
-#else
-  signal(SIGTERM, SIG_IGN);
-#endif /* HAVE_SIGSET */
-
  /*
   * Free memory...
   */
@@ -409,31 +396,9 @@ Shutdown(void)
 void
 CancelJob(int sig)                     /* I - Signal */
 {
-  int  i;                              /* Looping var */
-
-
   (void)sig;
 
- /*
-  * Send out lots of NUL bytes to clear out any pending raster data...
-  */
-
-  if (DotBytes)
-    i = DotBytes * 360 * 8;
-  else
-    i = 720;
-
-  for (; i > 0; i --)
-    putchar(0);
-
- /*
-  * End the current page and exit...
-  */
-
-  EndPage(NULL);
-  Shutdown();
-
-  exit(0);
+  Canceled = 1;
 }
 
 
@@ -443,11 +408,11 @@ CancelJob(int sig)                        /* I - Signal */
 
 void
 CompressData(const unsigned char *line,        /* I - Data to compress */
-             int                 length,/* I - Number of bytes */
-            int                 plane, /* I - Color plane */
-            int                 type,  /* I - Type of compression */
-            int                 xstep, /* I - X resolution */
-            int                 ystep) /* I - Y resolution */
+             unsigned            length,/* I - Number of bytes */
+            unsigned            plane, /* I - Color plane */
+            unsigned            type,  /* I - Type of compression */
+            unsigned            xstep, /* I - X resolution */
+            unsigned            ystep) /* I - Y resolution */
 {
   const unsigned char  *line_ptr,      /* Current byte pointer */
                        *line_end,      /* End-of-line byte pointer */
@@ -554,7 +519,7 @@ CompressData(const unsigned char *line,     /* I - Data to compress */
               count ++;
            }
 
-           *comp_ptr++ = 257 - count;
+           *comp_ptr++ = (unsigned char)(257 - count);
            *comp_ptr++ = *line_ptr++;
          }
          else
@@ -575,9 +540,9 @@ CompressData(const unsigned char *line,     /* I - Data to compress */
               count ++;
            }
 
-           *comp_ptr++ = count - 1;
+           *comp_ptr++ = (unsigned char)(count - 1);
 
-           memcpy(comp_ptr, start, count);
+           memcpy(comp_ptr, start, (size_t)count);
            comp_ptr += count;
          }
        }
@@ -617,12 +582,12 @@ CompressData(const unsigned char *line,   /* I - Data to compress */
 
     length *= 8;
     printf("\033.");                   /* Raster graphics */
-    putchar(type);
-    putchar(ystep);
-    putchar(xstep);
+    putchar((int)type);
+    putchar((int)ystep);
+    putchar((int)xstep);
     putchar(1);
-    putchar(length);
-    putchar(length >> 8);
+    putchar((int)length);
+    putchar((int)(length >> 8));
   }
   else
   {
@@ -632,15 +597,15 @@ CompressData(const unsigned char *line,   /* I - Data to compress */
 
     printf("\033i");
     putchar(ctable[plane]);
-    putchar(type);
+    putchar((int)type);
     putchar(1);
-    putchar(length & 255);
-    putchar(length >> 8);
+    putchar((int)length);
+    putchar((int)(length >> 8));
     putchar(1);
     putchar(0);
   }
 
-  pwrite(line_ptr, line_end - line_ptr);
+  pwrite(line_ptr, (size_t)(line_end - line_ptr));
   fflush(stdout);
 }
 
@@ -650,15 +615,16 @@ CompressData(const unsigned char *line,   /* I - Data to compress */
  */
 
 void
-OutputLine(const cups_page_header_t *header)   /* I - Page header */
+OutputLine(
+    const cups_page_header2_t *header) /* I - Page header */
 {
   if (header->cupsRowCount)
   {
-    int                        width;
+    unsigned           width;
     unsigned char      *tempptr,
                        *evenptr,
                        *oddptr;
-    register int       x;
+    unsigned int       x;
     unsigned char      bit;
     const unsigned char        *pixel;
     unsigned char      *temp;
@@ -711,13 +677,19 @@ OutputLine(const cups_page_header_t *header)      /* I - Page header */
         }
 
         for (width = header->cupsWidth, tempptr = CompBuffer;
-             width > 0;
+             width > 1;
              width -= 2, tempptr += 2, oddptr += DotBytes * 2,
                 evenptr += DotBytes * 2)
         {
           evenptr[0] = tempptr[0];
           oddptr[0]  = tempptr[1];
         }
+
+        if (width == 1)
+        {
+          evenptr[0] = tempptr[0];
+          oddptr[0]  = tempptr[1];
+        }
       }
       else
       {
@@ -768,10 +740,9 @@ OutputLine(const cups_page_header_t *header)       /* I - Page header */
   }
   else
   {
-    int        plane;          /* Current plane */
-    int        bytes;          /* Bytes per plane */
-    int        xstep, ystep;   /* X & Y resolutions */
-
+    unsigned   plane;          /* Current plane */
+    unsigned   bytes;          /* Bytes per plane */
+    unsigned   xstep, ystep;   /* X & Y resolutions */
 
    /*
     * Write a single line of bitmap data as needed...
@@ -788,7 +759,7 @@ OutputLine(const cups_page_header_t *header)        /* I - Page header */
       */
 
       if (!Planes[plane][0] &&
-          memcmp(Planes[plane], Planes[plane] + 1, bytes - 1) == 0)
+          memcmp(Planes[plane], Planes[plane] + 1, (size_t)bytes - 1) == 0)
        continue;
 
      /*
@@ -798,14 +769,13 @@ OutputLine(const cups_page_header_t *header)      /* I - Page header */
       if (Feed > 0)
       {
        pwrite("\033(v\002\000", 5);    /* Relative vertical position */
-       putchar(Feed);
-       putchar(Feed >> 8);
+       putchar((int)Feed);
+       putchar((int)(Feed >> 8));
 
        Feed = 0;
       }
 
-      CompressData(Planes[plane], bytes, plane, header->cupsCompression, xstep,
-                   ystep);
+      CompressData(Planes[plane], bytes, plane, header->cupsCompression, xstep, ystep);
     }
 
     Feed ++;
@@ -818,21 +788,21 @@ OutputLine(const cups_page_header_t *header)      /* I - Page header */
  */
 
 void
-OutputRows(const cups_page_header_t *header,   /* I - Page image header */
-           int                      row)       /* I - Row number (0 or 1) */
+OutputRows(
+    const cups_page_header2_t *header, /* I - Page image header */
+    int                      row)      /* I - Row number (0 or 1) */
 {
-  unsigned     i, n;                           /* Looping vars */
-  int          dot_count,                      /* Number of bytes to print */
-                dot_min;                       /* Minimum number of bytes */
-  unsigned char *dot_ptr,                      /* Pointer to print data */
-               *ptr;                           /* Current data */
+  unsigned     i, n,                   /* Looping vars */
+               dot_count,              /* Number of bytes to print */
+                dot_min;               /* Minimum number of bytes */
+  unsigned char *dot_ptr,              /* Pointer to print data */
+               *ptr;                   /* Current data */
 
 
   dot_min = DotBytes * DotColumns;
 
   if (LineBuffers[row][0] != 0 ||
-      memcmp(LineBuffers[row], LineBuffers[row] + 1,
-             header->cupsWidth * DotBytes - 1))
+      memcmp(LineBuffers[row], LineBuffers[row] + 1, header->cupsWidth * DotBytes - 1))
   {
    /*
     * Skip leading space...
@@ -869,8 +839,8 @@ OutputRows(const cups_page_header_t *header,        /* I - Page image header */
     {
       putchar(0x1b);
       putchar('$');
-      putchar(i & 255);
-      putchar(i >> 8);
+      putchar((int)(i & 255));
+      putchar((int)(i >> 8));
     }
 
    /*
@@ -910,9 +880,9 @@ OutputRows(const cups_page_header_t *header,        /* I - Page image header */
           break;
     }
 
-    n = (unsigned)dot_count / DotBytes;
-    putchar(n & 255);
-    putchar(n / 256);
+    n = dot_count / DotBytes;
+    putchar((int)(n & 255));
+    putchar((int)(n / 256));
 
    /*
     * Write the graphics data...
@@ -931,6 +901,9 @@ OutputRows(const cups_page_header_t *header,        /* I - Page image header */
        putchar(0);
       }
 
+      if (dot_count & 1)
+        putchar(*ptr);
+
      /*
       * Move the head back and print the odd bytes...
       */
@@ -941,8 +914,8 @@ OutputRows(const cups_page_header_t *header,        /* I - Page image header */
       {
        putchar(0x1b);
        putchar('$');
-       putchar(i & 255);
-       putchar(i >> 8);
+       putchar((int)(i & 255));
+       putchar((int)(i >> 8));
       }
 
       if (header->HWResolution[0] == 120)
@@ -951,14 +924,17 @@ OutputRows(const cups_page_header_t *header,      /* I - Page image header */
        printf("\033*\003");            /* Select bit image */
 
       n = (unsigned)dot_count / DotBytes;
-      putchar(n & 255);
-      putchar(n / 256);
+      putchar((int)(n & 255));
+      putchar((int)(n / 256));
 
       for (n = dot_count / 2, ptr = dot_ptr + 1; n > 0; n --, ptr += 2)
       {
        putchar(0);
         putchar(*ptr);
       }
+
+      if (dot_count & 1)
+        putchar(0);
     }
     else
       pwrite(dot_ptr, dot_count);
@@ -992,16 +968,19 @@ OutputRows(const cups_page_header_t *header,      /* I - Page image header */
  * 'main()' - Main entry and processing of driver.
  */
 
-int                            /* O - Exit status */
-main(int  argc,                        /* I - Number of command-line arguments */
-     char *argv[])             /* I - Command-line arguments */
+int                                    /* O - Exit status */
+main(int  argc,                                /* I - Number of command-line arguments */
+     char *argv[])                     /* I - Command-line arguments */
 {
-  int                  fd;     /* File descriptor */
-  cups_raster_t                *ras;   /* Raster stream for printing */
-  cups_page_header_t   header; /* Page header from file */
-  ppd_file_t           *ppd;   /* PPD file */
-  int                  page;   /* Current page */
-  int                  y;      /* Current line */
+  int                  fd;             /* File descriptor */
+  cups_raster_t                *ras;           /* Raster stream for printing */
+  cups_page_header2_t  header;         /* Page header from file */
+  ppd_file_t           *ppd;           /* PPD file */
+  int                  page;           /* Current page */
+  unsigned             y;              /* Current line */
+#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
+  struct sigaction action;             /* Actions for POSIX signals */
+#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
 
 
  /*
@@ -1021,8 +1000,9 @@ main(int  argc,                   /* I - Number of command-line arguments */
     * and return.
     */
 
-    fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
-            argv[0]);
+    _cupsLangPrintFilter(stderr, "ERROR",
+                         _("%s job-id user title copies options [file]"),
+                         "rastertoepson");
     return (1);
   }
 
@@ -1034,7 +1014,7 @@ main(int  argc,                   /* I - Number of command-line arguments */
   {
     if ((fd = open(argv[6], O_RDONLY)) == -1)
     {
-      perror("ERROR: Unable to open raster file - ");
+      _cupsLangPrintError("ERROR", _("Unable to open raster file"));
       sleep(1);
       return (1);
     }
@@ -1044,13 +1024,46 @@ main(int  argc,                 /* I - Number of command-line arguments */
 
   ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
 
+ /*
+  * Register a signal handler to eject the current page if the
+  * job is cancelled.
+  */
+
+  Canceled = 0;
+
+#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
+  sigset(SIGTERM, CancelJob);
+#elif defined(HAVE_SIGACTION)
+  memset(&action, 0, sizeof(action));
+
+  sigemptyset(&action.sa_mask);
+  action.sa_handler = CancelJob;
+  sigaction(SIGTERM, &action, NULL);
+#else
+  signal(SIGTERM, CancelJob);
+#endif /* HAVE_SIGSET */
+
  /*
   * Initialize the print device...
   */
 
   ppd = ppdOpenFile(getenv("PPD"));
-  if (ppd)
-    Model = ppd->model_number;
+  if (!ppd)
+  {
+    ppd_status_t       status;         /* PPD error */
+    int                        linenum;        /* Line number */
+
+    _cupsLangPrintFilter(stderr, "ERROR",
+                         _("The PPD file could not be opened."));
+
+    status = ppdLastError(&linenum);
+
+    fprintf(stderr, "DEBUG: %s on line %d.\n", ppdErrorString(status), linenum);
+
+    return (1);
+  }
+
+  Model = ppd->model_number;
 
   Setup();
 
@@ -1060,15 +1073,19 @@ main(int  argc,                 /* I - Number of command-line arguments */
 
   page = 0;
 
-  while (cupsRasterReadHeader(ras, &header))
+  while (cupsRasterReadHeader2(ras, &header))
   {
    /*
     * Write a status message with the page number and number of copies.
     */
 
+    if (Canceled)
+      break;
+
     page ++;
 
     fprintf(stderr, "PAGE: %d %d\n", page, header.NumCopies);
+    _cupsLangPrintFilter(stderr, "INFO", _("Starting page %d."), page);
 
    /*
     * Start the page...
@@ -1086,9 +1103,17 @@ main(int  argc,                  /* I - Number of command-line arguments */
       * Let the user know how far we have progressed...
       */
 
+      if (Canceled)
+       break;
+
       if ((y & 127) == 0)
-        fprintf(stderr, _("INFO: Printing page %d, %d%% complete...\n"), page,
-               100 * y / header.cupsHeight);
+      {
+        _cupsLangPrintFilter(stderr, "INFO",
+                            _("Printing page %d, %u%% complete."),
+                            page, 100 * y / header.cupsHeight);
+        fprintf(stderr, "ATTR: job-media-progress=%u\n",
+               100 * y / header.cupsHeight);
+      }
 
      /*
       * Read a line of graphics...
@@ -1108,7 +1133,12 @@ main(int  argc,                  /* I - Number of command-line arguments */
     * Eject the page...
     */
 
+    _cupsLangPrintFilter(stderr, "INFO", _("Finished page %d."), page);
+
     EndPage(&header);
+
+    if (Canceled)
+      break;
   }
 
  /*
@@ -1132,14 +1162,10 @@ main(int  argc,                 /* I - Number of command-line arguments */
   */
 
   if (page == 0)
-    fputs(_("ERROR: No pages found!\n"), stderr);
+  {
+    _cupsLangPrintFilter(stderr, "ERROR", _("No pages were found."));
+    return (1);
+  }
   else
-    fputs(_("INFO: Ready to print.\n"), stderr);
-
-  return (page == 0);
+    return (0);
 }
-
-
-/*
- * End of "$Id: rastertoepson.c 6649 2007-07-11 21:46:42Z mike $".
- */