]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/emit.c
Merge changes from CUPS 1.5svn-r9214.
[thirdparty/cups.git] / cups / emit.c
index 9b1aeb3aa54d7cdc14205a5c06fc736884945df6..60da1f9a1492c2805c20880338c7da83bfcc2fb8 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * "$Id: emit.c 7863 2008-08-26 03:39:59Z mike $"
  *
- *   PPD code emission routines for the Common UNIX Printing System (CUPS).
+ *   PPD code emission routines for CUPS.
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2010 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
  * Include necessary headers...
  */
 
-#include "ppd.h"
-#include <stdlib.h>
-#include "string.h"
-#include <errno.h>
-#include "debug.h"
-
+#include "cups-private.h"
 #if defined(WIN32) || defined(__EMX__)
 #  include <io.h>
 #else
@@ -109,7 +104,7 @@ ppdCollect2(ppd_file_t    *ppd,             /* I - PPD file data */
   float                *orders;                /* Collected order values */
 
 
-  DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
+  DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)",
                 ppd, section, min_order, choices));
 
   if (!ppd || !choices)
@@ -223,7 +218,7 @@ ppdCollect2(ppd_file_t    *ppd,             /* I - PPD file data */
 
   free(orders);
 
-  DEBUG_printf(("ppdCollect2: %d marked choices...\n", count));
+  DEBUG_printf(("2ppdCollect2: %d marked choices...", count));
 
  /*
   * Return the array and number of choices; if 0, free the array since
@@ -390,7 +385,8 @@ ppdEmitJCL(ppd_file_t *ppd,         /* I - PPD file record */
           const char *title)           /* I - Title */
 {
   char         *ptr;                   /* Pointer into JCL string */
-  char         temp[81];               /* Local title string */
+  char         temp[65],               /* Local title string */
+               displaymsg[33];         /* Local display string */
 
 
  /*
@@ -464,14 +460,39 @@ ppdEmitJCL(ppd_file_t *ppd,               /* I - PPD file record */
       }
 
    /*
-    * Eliminate any path info from the job title...
+    * Clean up the job title...
     */
 
     if ((ptr = strrchr(title, '/')) != NULL)
+    {
+     /*
+      * Only show basename of file path...
+      */
+
       title = ptr + 1;
+    }
+
+    if (!strncmp(title, "smbprn.", 7))
+    {
+     /*
+      * Skip leading smbprn.######## from Samba jobs...
+      */
+
+      for (title += 7; *title && isdigit(*title & 255); title ++);
+      for (; *title && isspace(*title & 255); title ++);
+
+      if ((ptr = strstr(title, " - ")) != NULL)
+      {
+       /*
+       * Skip application name in "Some Application - Title of job"...
+       */
+
+       title = ptr + 3;
+      }
+    }
 
    /*
-    * Replace double quotes with single quotes and 8-bit characters with
+    * Replace double quotes with single quotes and UTF-8 characters with
     * question marks so that the title does not cause a PJL syntax error.
     */
 
@@ -480,9 +501,18 @@ ppdEmitJCL(ppd_file_t *ppd,                /* I - PPD file record */
     for (ptr = temp; *ptr; ptr ++)
       if (*ptr == '\"')
         *ptr = '\'';
-      else if (charset && (*ptr & 128))
+      else if (!charset && (*ptr & 128))
         *ptr = '?';
 
+   /*
+    * CUPS STR #3125: Long PJL JOB NAME causes problems with some printers
+    *
+    * Generate the display message, truncating at 32 characters + nul to avoid
+    * issues with some printer's PJL implementations...
+    */
+
+    snprintf(displaymsg, sizeof(displaymsg), "%d %s %s", job_id, user, temp);
+
    /*
     * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
     */
@@ -492,11 +522,11 @@ ppdEmitJCL(ppd_file_t *ppd,               /* I - PPD file record */
       fprintf(fp, "@PJL JOB NAME = \"%s\"\n", temp);
 
       if (display && !strcmp(display->value, "rdymsg"))
-        fprintf(fp, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id, user, temp);
+        fprintf(fp, "@PJL RDYMSG DISPLAY = \"%s\"\n", displaymsg);
     }
     else
-      fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp,
-             job_id, user, temp);
+      fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%s\"\n", temp,
+             displaymsg);
   }
   else
     fputs(ppd->jcl_begin, fp);
@@ -591,7 +621,7 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
   struct lconv *loc;                   /* Locale data */
 
 
-  DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
+  DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)",
                 ppd, section, min_order));
 
  /*
@@ -648,7 +678,8 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
            case PPD_CUSTOM_PASSCODE :
            case PPD_CUSTOM_PASSWORD :
            case PPD_CUSTOM_STRING :
-               bufsize += strlen(cparam->current.custom_string);
+               if (cparam->current.custom_string)
+                 bufsize += strlen(cparam->current.custom_string);
                break;
           }
        }
@@ -662,7 +693,7 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
            !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
           !strcasecmp(choices[i]->choice, "Custom"))
       {
-        DEBUG_puts("ppdEmitString: Custom size set!");
+        DEBUG_puts("2ppdEmitString: Custom size set!");
 
         bufsize += 37;                 /* %%BeginFeature: *CustomPageSize True\n */
         bufsize += 50;                 /* Five 9-digit numbers + newline */
@@ -672,8 +703,8 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
                                              choices[i]->option->keyword))
                   != NULL)
       {
-        bufsize += 17 + strlen(choices[i]->option->keyword) + 6;
-                                       /* %%BeginFeature: *keyword True\n */
+        bufsize += 23 + strlen(choices[i]->option->keyword) + 6;
+                                       /* %%BeginFeature: *Customkeyword True\n */
 
         
         for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
@@ -693,7 +724,9 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
            case PPD_CUSTOM_PASSCODE :
            case PPD_CUSTOM_PASSWORD :
            case PPD_CUSTOM_STRING :
-               bufsize += 3 + 4 * strlen(cparam->current.custom_string);
+               bufsize += 3;
+               if (cparam->current.custom_string)
+                 bufsize += 4 * strlen(cparam->current.custom_string);
                break;
           }
        }
@@ -717,7 +750,7 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
   * Allocate memory...
   */
 
-  DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n",
+  DEBUG_printf(("2ppdEmitString: Allocating %d bytes for string...",
                 (int)bufsize));
 
   if ((buffer = calloc(1, bufsize)) == NULL)
@@ -737,6 +770,7 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
     if (section == PPD_ORDER_JCL)
     {
       if (!strcasecmp(choices[i]->choice, "Custom") &&
+         choices[i]->code &&
           (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
              != NULL)
       {
@@ -761,8 +795,8 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
              */
 
               pnum = *cptr++ - '0';
-             while (isalnum(*cptr & 255))
-               pnum = pnum * 10 + *cptr - '0';
+             while (isdigit(*cptr & 255))
+               pnum = pnum * 10 + *cptr++ - '0';
 
               for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
                   cparam;
@@ -792,9 +826,12 @@ ppdEmitString(ppd_file_t    *ppd,  /* I - PPD file record */
                  case PPD_CUSTOM_PASSCODE :
                  case PPD_CUSTOM_PASSWORD :
                  case PPD_CUSTOM_STRING :
-                     strlcpy(bufptr, cparam->current.custom_string,
-                             bufend - bufptr);
-                     bufptr += strlen(bufptr);
+                     if (cparam->current.custom_string)
+                     {
+                       strlcpy(bufptr, cparam->current.custom_string,
+                               bufend - bufptr);
+                       bufptr += strlen(bufptr);
+                     }
                      break;
                }
              }
@@ -830,8 +867,8 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
       * Send DSC comments with option...
       */
 
-      DEBUG_printf(("Adding code for %s=%s...\n", choices[i]->option->keyword,
-                    choices[i]->choice));
+      DEBUG_printf(("2ppdEmitString: Adding code for %s=%s...",
+                   choices[i]->option->keyword, choices[i]->choice));
 
       if ((!strcasecmp(choices[i]->option->keyword, "PageSize") ||
            !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
@@ -997,14 +1034,19 @@ ppdEmitString(ppd_file_t    *ppd,        /* I - PPD file record */
            case PPD_CUSTOM_STRING :
                *bufptr++ = '(';
 
-               for (s = cparam->current.custom_string; *s; s ++)
-                 if (*s < ' ' || *s == '(' || *s == ')' || *s >= 127)
+               if (cparam->current.custom_string)
+               {
+                 for (s = cparam->current.custom_string; *s; s ++)
                  {
-                   snprintf(bufptr, bufend - bufptr + 1, "\\%03o", *s & 255);
-                   bufptr += strlen(bufptr);
+                   if (*s < ' ' || *s == '(' || *s == ')' || *s >= 127)
+                   {
+                     snprintf(bufptr, bufend - bufptr + 1, "\\%03o", *s & 255);
+                     bufptr += strlen(bufptr);
+                   }
+                   else
+                     *bufptr++ = *s;
                  }
-                 else
-                   *bufptr++ = *s;
+               }
 
                *bufptr++ = ')';
                *bufptr++ = '\n';
@@ -1035,7 +1077,7 @@ ppdEmitString(ppd_file_t    *ppd, /* I - PPD file record */
                      "} stopped cleartomark\n", bufend - bufptr + 1);
       bufptr += strlen(bufptr);
 
-      DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
+      DEBUG_printf(("2ppdEmitString: Offset in string is %d...",
                     (int)(bufptr - buffer)));
     }
     else
@@ -1076,17 +1118,32 @@ static void
 ppd_handle_media(ppd_file_t *ppd)      /* I - PPD file */
 {
   ppd_choice_t *manual_feed,           /* ManualFeed choice, if any */
-               *input_slot,            /* InputSlot choice, if any */
-               *page;                  /* PageSize/PageRegion */
+               *input_slot;            /* InputSlot choice, if any */
   ppd_size_t   *size;                  /* Current media size */
   ppd_attr_t   *rpr;                   /* RequiresPageRegion value */
 
 
  /*
-  * This function determines if the user has selected a media source
-  * via the InputSlot or ManualFeed options; if so, it marks the
-  * PageRegion option corresponding to the current media size.
-  * Otherwise it marks the PageSize option.
+  * This function determines what page size code to use, if any, for the
+  * current media size, InputSlot, and ManualFeed selections.
+  *
+  * We use the PageSize code if:
+  *
+  * 1. A custom media size is selected.
+  * 2. ManualFeed and InputSlot are not selected (or do not exist).
+  * 3. ManualFeed is selected but is False and InputSlot is not selected or
+  *    the selection has no code - the latter check done to support "auto" or
+  *    "printer default" InputSlot options.
+  *
+  * We use the PageRegion code if:
+  *
+  * 4. RequiresPageRegion does not exist and the PPD contains cupsFilter
+  *    keywords, indicating this is a CUPS-based driver.
+  * 5. RequiresPageRegion exists for the selected InputSlot (or "All" for any
+  *    InputSlot or ManualFeed selection) and is True.
+  *
+  * If none of the 5 conditions are true, no page size code is used and we
+  * unmark any existing PageSize or PageRegion choices.
   */
 
   if ((size = ppdPageSize(ppd, NULL)) == NULL)
@@ -1103,46 +1160,52 @@ ppd_handle_media(ppd_file_t *ppd)       /* I - PPD file */
   if (!rpr)
     rpr = ppdFindAttr(ppd, "RequiresPageRegion", "All");
 
-  if (!strcasecmp(size->name, "Custom") || (!manual_feed && !input_slot) ||
-      !((manual_feed && !strcasecmp(manual_feed->choice, "True")) ||
-        (input_slot && input_slot->code && input_slot->code[0])))
+  if (!strcasecmp(size->name, "Custom") ||
+      (!manual_feed && !input_slot) ||
+      (manual_feed && !strcasecmp(manual_feed->choice, "False") &&
+       (!input_slot || (input_slot->code && !input_slot->code[0]))) ||
+      (!rpr && ppd->num_filters > 0))
   {
    /*
-    * Manual feed was not selected and/or the input slot selection does
-    * not contain any PostScript code.  Use the PageSize option...
+    * Use PageSize code...
     */
 
     ppdMarkOption(ppd, "PageSize", size->name);
   }
-  else
+  else if (rpr && rpr->value && !strcasecmp(rpr->value, "True"))
   {
    /*
-    * Manual feed was selected and/or the input slot selection contains
-    * PostScript code.  Use the PageRegion option...
+    * Use PageRegion code...
     */
 
     ppdMarkOption(ppd, "PageRegion", size->name);
-
+  }
+  else
+  {
    /*
-    * RequiresPageRegion does not apply to manual feed so we need to
-    * check that we are not doing manual feed before unmarking PageRegion.
+    * Do not use PageSize or PageRegion code...
     */
 
-    if (!(manual_feed && !strcasecmp(manual_feed->choice, "True")) &&
-        ((rpr && rpr->value && !strcmp(rpr->value, "False")) ||
-         (!rpr && !ppd->num_filters)))
+    ppd_choice_t       *page;          /* PageSize/Region choice, if any */
+
+    if ((page = ppdFindMarkedChoice(ppd, "PageSize")) != NULL)
     {
      /*
-      * Either the PPD file specifies no PageRegion code or the PPD file
-      * not for a CUPS raster driver and thus defaults to no PageRegion
-      * code...  Unmark the PageRegion choice so that we don't output the
-      * code...
+      * Unmark PageSize...
       */
 
-      page = ppdFindMarkedChoice(ppd, "PageRegion");
+      page->marked = 0;
+      cupsArrayRemove(ppd->marked, page);
+    }
+
+    if ((page = ppdFindMarkedChoice(ppd, "PageRegion")) != NULL)
+    {
+     /*
+      * Unmark PageRegion...
+      */
 
-      if (page)
-        page->marked = 0;
+      page->marked = 0;
+      cupsArrayRemove(ppd->marked, page);
     }
   }
 }