]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - filter/pstops.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / filter / pstops.c
index e887d381bef5edeaa879733bfe860b152c0732ed..f8ab6b00a6dc3d3fee8f26130b1f5d19aba31650 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: pstops.c 6649 2007-07-11 21:46:42Z mike $"
+ * "$Id: pstops.c 7006 2007-10-04 17:43:38Z mike $"
  *
  *   PostScript filter for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 2007 by Apple Inc.
+ *   Copyright 2007-2008 by Apple Inc.
  *   Copyright 1993-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
@@ -18,6 +18,7 @@
  *
  *   main()               - Main entry...
  *   add_page()           - Add a page to the pages array...
+ *   cancel_job()         - Flag the job as canceled.
  *   check_range()        - Check to see if the current page is selected for
  *   copy_bytes()         - Copy bytes from the input file to stdout...
  *   copy_comments()      - Copy all of the comments section...
@@ -53,6 +54,7 @@
 #include <cups/file.h>
 #include <cups/array.h>
 #include <cups/i18n.h>
+#include <signal.h>
 
 
 /*
@@ -146,35 +148,43 @@ typedef struct                            /**** Document information ****/
                                 ((p) % doc->number_up) != 0)
 
 
+/*
+ * Local globals...
+ */
+
+static int             JobCanceled = 0;/* Set to 1 on SIGTERM */
+
+
 /*
  * Local functions...
  */
 
 static pstops_page_t   *add_page(pstops_doc_t *doc, const char *label);
+static void            cancel_job(int sig);
 static int             check_range(pstops_doc_t *doc, int page);
 static void            copy_bytes(cups_file_t *fp, off_t offset,
                                   size_t length);
-static size_t          copy_comments(cups_file_t *fp, pstops_doc_t *doc,
+static ssize_t         copy_comments(cups_file_t *fp, pstops_doc_t *doc,
                                      ppd_file_t *ppd, char *line,
-                                     size_t linelen, size_t linesize);
+                                     ssize_t linelen, size_t linesize);
 static void            copy_dsc(cups_file_t *fp, pstops_doc_t *doc,
-                                ppd_file_t *ppd, char *line, size_t linelen,
+                                ppd_file_t *ppd, char *line, ssize_t linelen,
                                 size_t linesize);
 static void            copy_non_dsc(cups_file_t *fp, pstops_doc_t *doc,
                                     ppd_file_t *ppd, char *line,
-                                    size_t linelen, size_t linesize);
-static size_t          copy_page(cups_file_t *fp, pstops_doc_t *doc,
+                                    ssize_t linelen, size_t linesize);
+static ssize_t         copy_page(cups_file_t *fp, pstops_doc_t *doc,
                                  ppd_file_t *ppd, int number, char *line,
-                                 size_t linelen, size_t linesize);
-static size_t          copy_prolog(cups_file_t *fp, pstops_doc_t *doc,
+                                 ssize_t linelen, size_t linesize);
+static ssize_t         copy_prolog(cups_file_t *fp, pstops_doc_t *doc,
                                    ppd_file_t *ppd, char *line,
-                                   size_t linelen, size_t linesize);
-static size_t          copy_setup(cups_file_t *fp, pstops_doc_t *doc,
+                                   ssize_t linelen, size_t linesize);
+static ssize_t         copy_setup(cups_file_t *fp, pstops_doc_t *doc,
                                   ppd_file_t *ppd, char *line,
-                                  size_t linelen, size_t linesize);
-static size_t          copy_trailer(cups_file_t *fp, pstops_doc_t *doc,
+                                  ssize_t linelen, size_t linesize);
+static ssize_t         copy_trailer(cups_file_t *fp, pstops_doc_t *doc,
                                     ppd_file_t *ppd, int number, char *line,
-                                    size_t linelen, size_t linesize);
+                                    ssize_t linelen, size_t linesize);
 static void            do_prolog(pstops_doc_t *doc, ppd_file_t *ppd);
 static void            do_setup(pstops_doc_t *doc, ppd_file_t *ppd);
 static void            doc_printf(pstops_doc_t *doc, const char *format, ...)
@@ -193,7 +203,7 @@ static char         *parse_text(const char *start, char **end, char *buffer,
 static void            set_pstops_options(pstops_doc_t *doc, ppd_file_t *ppd,
                                           char *argv[], int num_options,
                                           cups_option_t *options);
-static size_t          skip_page(cups_file_t *fp, char *line, size_t linelen,
+static ssize_t         skip_page(cups_file_t *fp, char *line, ssize_t linelen,
                                  size_t linesize);
 static void            start_nup(pstops_doc_t *doc, int number,
                                  int show_border, const int *bounding_box);
@@ -218,6 +228,9 @@ main(int  argc,                             /* I - Number of command-line args */
   cups_option_t        *options;               /* Print options */
   char         line[8192];             /* Line buffer */
   size_t       len;                    /* Length of line buffer */
+#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
+  struct sigaction action;             /* Actions for POSIX signals */
+#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
 
 
  /*
@@ -237,6 +250,22 @@ main(int  argc,                            /* I - Number of command-line args */
     return (1);
   }
 
+ /*
+  * Register a signal handler to cleanly cancel a job.
+  */
+
+#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
+  sigset(SIGTERM, cancel_job);
+#elif defined(HAVE_SIGACTION)
+  memset(&action, 0, sizeof(action));
+
+  sigemptyset(&action.sa_mask);
+  action.sa_handler = cancel_job;
+  sigaction(SIGTERM, &action, NULL);
+#else
+  signal(SIGTERM, cancel_job);
+#endif /* HAVE_SIGSET */
+
  /*
   * If we have 7 arguments, print the file named on the command-line.
   * Otherwise, send stdin instead...
@@ -421,6 +450,19 @@ add_page(pstops_doc_t *doc,                /* I - Document information */
 }
 
 
+/*
+ * 'cancel_job()' - Flag the job as canceled.
+ */
+
+static void
+cancel_job(int sig)                    /* I - Signal number (unused) */
+{
+  (void)sig;
+
+  JobCanceled = 1;
+}
+
+
 /*
  * 'check_range()' - Check to see if the current page is selected for
  *                   printing.
@@ -539,12 +581,12 @@ copy_bytes(cups_file_t *fp,               /* I - File to read from */
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_comments(cups_file_t  *fp,                /* I - File to read from */
               pstops_doc_t *doc,       /* I - Document info */
              ppd_file_t   *ppd,        /* I - PPD file */
               char         *line,      /* I - Line buffer */
-             size_t       linelen,     /* I - Length of initial line */
+             ssize_t      linelen,     /* I - Length of initial line */
              size_t       linesize)    /* I - Size of line buffer */
 {
   int  saw_bounding_box,               /* Saw %%BoundingBox: comment? */
@@ -767,7 +809,7 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
          pstops_doc_t *doc,            /* I - Document info */
          ppd_file_t   *ppd,            /* I - PPD file */
         char         *line,            /* I - Line buffer */
-        size_t       linelen,          /* I - Length of initial line */
+        ssize_t      linelen,          /* I - Length of initial line */
         size_t       linesize)         /* I - Size of line buffer */
 {
   int          number;                 /* Page number */
@@ -826,6 +868,9 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
   fprintf(stderr, "DEBUG: Before page loop - %s", line);
   while (!strncmp(line, "%%Page:", 7))
   {
+    if (JobCanceled)
+      break;
+
     number ++;
 
     if (check_range(doc, (number - 1) / doc->number_up + 1))
@@ -844,7 +889,8 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
   * Finish up the last page(s)...
   */
 
-  if (number && is_not_last_page(number) && cupsArrayLast(doc->pages))
+  if (number && is_not_last_page(number) && cupsArrayLast(doc->pages) &&
+      check_range(doc, (number - 1) / doc->number_up + 1))
   {
     pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages);
 
@@ -885,7 +931,7 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
 
   number = doc->slow_order ? 0 : doc->page;
 
-  if (doc->temp)
+  if (doc->temp && !JobCanceled && cupsArrayCount(doc->pages) > 0)
   {
     int        copy;                           /* Current copy */
 
@@ -902,8 +948,16 @@ copy_dsc(cups_file_t  *fp,         /* I - File to read from */
     * Make the copies...
     */
 
-    for (copy = !doc->slow_order; copy < doc->copies; copy ++)
+    if (doc->slow_collate)
+      copy = !doc->slow_order;
+    else
+      copy = doc->copies - 1;
+
+    for (; copy < doc->copies; copy ++)
     {
+      if (JobCanceled)
+       break;
+
      /*
       * Send end-of-job stuff followed by any start-of-job stuff required
       * for the JCL options...
@@ -957,10 +1011,14 @@ copy_dsc(cups_file_t  *fp,               /* I - File to read from */
 
       while (pageinfo)
       {
+        if (JobCanceled)
+         break;
+
         number ++;
 
        if (!ppd || !ppd->num_filters)
-         fprintf(stderr, "PAGE: %d 1\n", number);
+         fprintf(stderr, "PAGE: %d %d\n", number,
+                 doc->slow_collate ? 1 : doc->copies);
 
        if (doc->number_up > 1)
        {
@@ -995,7 +1053,8 @@ copy_dsc(cups_file_t  *fp,         /* I - File to read from */
   * Write/copy the trailer...
   */
 
-  linelen = copy_trailer(fp, doc, ppd, number, line, linelen, linesize);
+  if (!JobCanceled)
+    linelen = copy_trailer(fp, doc, ppd, number, line, linelen, linesize);
 }
 
 
@@ -1010,7 +1069,7 @@ copy_non_dsc(cups_file_t  *fp,            /* I - File to read from */
              pstops_doc_t *doc,                /* I - Document info */
              ppd_file_t   *ppd,                /* I - PPD file */
             char         *line,        /* I - Line buffer */
-            size_t       linelen,      /* I - Length of initial line */
+            ssize_t      linelen,      /* I - Length of initial line */
             size_t       linesize)     /* I - Size of line buffer */
 {
   int  copy;                           /* Current copy */
@@ -1130,7 +1189,7 @@ copy_non_dsc(cups_file_t  *fp,            /* I - File to read from */
     puts("ESPshowpage");
   }
 
-  if (doc->temp)
+  if (doc->temp && !JobCanceled)
   {
    /*
     * Reopen the temporary file for reading...
@@ -1146,6 +1205,9 @@ copy_non_dsc(cups_file_t  *fp,            /* I - File to read from */
 
     for (copy = 1; copy < doc->copies; copy ++)
     {
+      if (JobCanceled)
+       break;
+
       if (!ppd || !ppd->num_filters)
        fputs("PAGE: 1 1\n", stderr);
 
@@ -1183,13 +1245,13 @@ copy_non_dsc(cups_file_t  *fp,          /* I - File to read from */
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_page(cups_file_t  *fp,            /* I - File to read from */
           pstops_doc_t *doc,           /* I - Document info */
           ppd_file_t   *ppd,           /* I - PPD file */
          int          number,          /* I - Current page number */
          char         *line,           /* I - Line buffer */
-         size_t       linelen,         /* I - Length of initial line */
+         ssize_t      linelen,         /* I - Length of initial line */
          size_t       linesize)        /* I - Size of line buffer */
 {
   char         label[256],             /* Page label string */
@@ -1652,12 +1714,12 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_prolog(cups_file_t  *fp,          /* I - File to read from */
             pstops_doc_t *doc,         /* I - Document info */
             ppd_file_t   *ppd,         /* I - PPD file */
            char         *line,         /* I - Line buffer */
-           size_t       linelen,       /* I - Length of initial line */
+           ssize_t      linelen,       /* I - Length of initial line */
            size_t       linesize)      /* I - Size of line buffer */
 {
   while (strncmp(line, "%%BeginProlog", 13))
@@ -1706,12 +1768,12 @@ copy_prolog(cups_file_t  *fp,           /* I - File to read from */
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_setup(cups_file_t  *fp,           /* I - File to read from */
            pstops_doc_t *doc,          /* I - Document info */
            ppd_file_t   *ppd,          /* I - PPD file */
           char         *line,          /* I - Line buffer */
-          size_t       linelen,        /* I - Length of initial line */
+          ssize_t      linelen,        /* I - Length of initial line */
           size_t       linesize)       /* I - Size of line buffer */
 {
   while (strncmp(line, "%%BeginSetup", 12))
@@ -1771,13 +1833,13 @@ copy_setup(cups_file_t  *fp,            /* I - File to read from */
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_trailer(cups_file_t  *fp,         /* I - File to read from */
              pstops_doc_t *doc,                /* I - Document info */
              ppd_file_t   *ppd,                /* I - PPD file */
             int          number,       /* I - Number of pages */
             char         *line,        /* I - Line buffer */
-            size_t       linelen,      /* I - Length of initial line */
+            ssize_t      linelen,      /* I - Length of initial line */
             size_t       linesize)     /* I - Size of line buffer */
 {
  /*
@@ -2650,10 +2712,10 @@ set_pstops_options(
  * 'skip_page()' - Skip past a page that won't be printed...
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 skip_page(cups_file_t *fp,             /* I - File to read from */
           char        *line,           /* I - Line buffer */
-         size_t      linelen,          /* I - Length of initial line */
+         ssize_t     linelen,          /* I - Length of initial line */
           size_t      linesize)                /* I - Size of line buffer */
 {
   int  level;                          /* Embedded document level */
@@ -2724,8 +2786,11 @@ start_nup(pstops_doc_t *doc,             /* I - Document information */
                tx, ty;                 /* Translation values for subpage */
   float                pagew,                  /* Printable width of page */
                pagel;                  /* Printable height of page */
-  int          bboxw,                  /* BoundingBox width */
+  int          bboxx,                  /* BoundingBox X origin */
+               bboxy,                  /* BoundingBox Y origin */
+               bboxw,                  /* BoundingBox width */
                bboxl;                  /* BoundingBox height */
+  float                margin = 0;             /* Current margin for border */
 
 
   if (doc->number_up > 1)
@@ -2740,17 +2805,22 @@ start_nup(pstops_doc_t *doc,            /* I - Document information */
 
   if (doc->fitplot)
   {
+    bboxx = bounding_box[0];
+    bboxy = bounding_box[1];
     bboxw = bounding_box[2] - bounding_box[0];
     bboxl = bounding_box[3] - bounding_box[1];
   }
   else
   {
+    bboxx = 0;
+    bboxy = 0;
     bboxw = PageWidth;
     bboxl = PageLength;
   }
 
   fprintf(stderr, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew, pagel);
-  fprintf(stderr, "DEBUG: bboxw = %d, bboxl = %d\n", bboxw, bboxl);
+  fprintf(stderr, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
+          bboxx, bboxy, bboxw, bboxl);
   fprintf(stderr, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
           PageLeft, PageRight);
   fprintf(stderr, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
@@ -3068,8 +3138,7 @@ start_nup(pstops_doc_t *doc,              /* I - Document information */
   if (doc->page_border && show_border)
   {
     int                rects;                  /* Number of border rectangles */
-    float      fscale,                 /* Scaling value for points */
-               margin;                 /* Current margin for borders */
+    float      fscale;                 /* Scaling value for points */
 
 
     rects  = (doc->page_border & PSTOPS_BORDERDOUBLE) ? 2 : 1;
@@ -3092,10 +3161,10 @@ start_nup(pstops_doc_t *doc,            /* I - Document information */
     for (; rects > 0; rects --, margin += 2 * fscale)
       if (doc->number_up > 1)
        doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
-                  margin - 2.25 * fscale,
-                  margin - 2.25 * fscale,
-                  bboxw + 4.5 * fscale - 2 * margin,
-                  bboxl + 4.5 * fscale - 2 * margin);
+                  margin,
+                  margin,
+                  bboxw - 2 * margin,
+                  bboxl - 2 * margin);
       else
        doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
                   PageLeft + margin,
@@ -3113,21 +3182,22 @@ start_nup(pstops_doc_t *doc,            /* I - Document information */
   if (doc->fitplot)
   {
    /*
-    * Clip the page that follows to the bounding box of the page...
+    * Offset the page by its bounding box...
     */
 
     doc_printf(doc, "%d %d translate\n", -bounding_box[0],
                -bounding_box[1]);
-    doc_printf(doc, "%d %d %d %d ESPrc\n", bounding_box[0], bounding_box[1],
-               bboxw, bboxl);
   }
-  else if (doc->number_up > 1)
+
+  if (doc->fitplot || doc->number_up > 1)
   {
    /*
-    * Clip the page that follows to the default page size...
+    * Clip the page to the page's bounding box...
     */
 
-    doc_printf(doc, "0 0 %d %d ESPrc\n", bboxw, bboxl);
+    doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrc\n",
+               bboxx + margin, bboxy + margin,
+               bboxw - 2 * margin, bboxl - 2 * margin);
   }
 }
 
@@ -3296,5 +3366,5 @@ write_labels(pstops_doc_t *doc,           /* I - Document information */
 
 
 /*
- * End of "$Id: pstops.c 6649 2007-07-11 21:46:42Z mike $".
+ * End of "$Id: pstops.c 7006 2007-10-04 17:43:38Z mike $".
  */