]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - filter/pstops.c
Merge changes from CUPS 1.3.1.
[thirdparty/cups.git] / filter / pstops.c
index 1a91a0402e6d2ef0334b24ec53bb43cabe7660bc..0b0844aeda26447f723e2c1ab7557cb04af8ea09 100644 (file)
@@ -1,25 +1,16 @@
 /*
- * "$Id: pstops.c 6578 2007-06-20 17:46:04Z mike $"
+ * "$Id: pstops.c 6759 2007-08-02 04:10:23Z mike $"
  *
  *   PostScript filter for the Common UNIX Printing System (CUPS).
  *
+ *   Copyright 2007 by Apple Inc.
  *   Copyright 1993-2007 by Easy Software Products.
  *
  *   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
- *   "LICENSE.txt" 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
+ *   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.
  *
@@ -27,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...
@@ -62,6 +54,7 @@
 #include <cups/file.h>
 #include <cups/array.h>
 #include <cups/i18n.h>
+#include <signal.h>
 
 
 /*
@@ -155,11 +148,19 @@ 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);
@@ -227,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 */
 
 
  /*
@@ -246,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...
@@ -430,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.
@@ -835,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))
@@ -894,7 +930,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)
   {
     int        copy;                           /* Current copy */
 
@@ -913,6 +949,9 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
 
     for (copy = !doc->slow_order; copy < doc->copies; copy ++)
     {
+      if (JobCanceled)
+       break;
+
      /*
       * Send end-of-job stuff followed by any start-of-job stuff required
       * for the JCL options...
@@ -966,6 +1005,9 @@ copy_dsc(cups_file_t  *fp,         /* I - File to read from */
 
       while (pageinfo)
       {
+        if (JobCanceled)
+         break;
+
         number ++;
 
        if (!ppd || !ppd->num_filters)
@@ -1004,7 +1046,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);
 }
 
 
@@ -1139,7 +1182,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...
@@ -1155,6 +1198,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);
 
@@ -3305,5 +3351,5 @@ write_labels(pstops_doc_t *doc,           /* I - Document information */
 
 
 /*
- * End of "$Id: pstops.c 6578 2007-06-20 17:46:04Z mike $".
+ * End of "$Id: pstops.c 6759 2007-08-02 04:10:23Z mike $".
  */