]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/emit.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / cups / emit.c
index ccbc0259639181617e96208d168c6e3ae18fa023..f3b32e666b0e3d9ddc60cd9d78701c7f9e1fc16b 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $"
+ * "$Id: emit.c 7863 2008-08-26 03:39:59Z mike $"
  *
  *   PPD code emission routines for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 2007 by Apple Inc.
+ *   Copyright 2007-2008 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
  *
  * Contents:
  *
- *   ppdCollect()        - Collect all marked options that reside in the
- *                         specified section.
- *   ppdCollect2()       - Collect all marked options that reside in the
- *                         specified section and minimum order.
- *   ppdEmit()           - Emit code for marked options to a file.
- *   ppdEmitAfterOrder() - Emit a subset of the code for marked options to a
- *                         file.
- *   ppdEmitFd()         - Emit code for marked options to a file.
- *   ppdEmitJCL()        - Emit code for JCL options to a file.
- *   ppdEmitJCLEnd()     - Emit JCLEnd code to a file.
- *   ppdEmitString()     - Get a string containing the code for marked options.
- *   ppd_handle_media()  - Handle media selection...
+ *   ppdCollect()          - Collect all marked options that reside in the
+ *                           specified section.
+ *   ppdCollect2()         - Collect all marked options that reside in the
+ *                           specified section and minimum order.
+ *   ppdEmit()             - Emit code for marked options to a file.
+ *   ppdEmitAfterOrder()   - Emit a subset of the code for marked options to a
+ *                           file.
+ *   ppdEmitFd()           - Emit code for marked options to a file.
+ *   ppdEmitJCL()          - Emit code for JCL options to a file.
+ *   ppdEmitJCLEnd()       - Emit JCLEnd code to a file.
+ *   ppdEmitString()       - Get a string containing the code for marked
+ *                           options.
+ *   ppd_compare_cparams() - Compare the order of two custom parameters.
+ *   ppd_handle_media()    - Handle media selection...
  */
 
 /*
@@ -53,6 +55,7 @@
  * Local functions...
  */
 
+static int     ppd_compare_cparams(ppd_cparam_t *a, ppd_cparam_t *b);
 static void    ppd_handle_media(ppd_file_t *ppd);
 
 
@@ -68,6 +71,9 @@ static const char ppd_custom_code[] =
 /*
  * 'ppdCollect()' - Collect all marked options that reside in the specified
  *                  section.
+ *
+ * The choices array should be freed using @code free@ when you are
+ * finished with it.
  */
 
 int                                    /* O - Number of options marked */
@@ -83,6 +89,9 @@ ppdCollect(ppd_file_t    *ppd,                /* I - PPD file data */
  * 'ppdCollect2()' - Collect all marked options that reside in the
  *                   specified section and minimum order.
  *
+ * The choices array should be freed using @code free@ when you are
+ * finished with it.
+ *
  * @since CUPS 1.2@
  */
 
@@ -103,16 +112,32 @@ ppdCollect2(ppd_file_t    *ppd,           /* I - PPD file data */
   DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
                 ppd, section, min_order, choices));
 
-  if (ppd == NULL)
+  if (!ppd || !choices)
+  {
+    if (choices)
+      *choices = NULL;
+
     return (0);
+  }
 
  /*
   * Allocate memory for up to N selected choices...
   */
 
-  count   = 0;
-  collect = calloc(sizeof(ppd_choice_t *), cupsArrayCount(ppd->marked));
-  orders  = calloc(sizeof(float), cupsArrayCount(ppd->marked));
+  count = 0;
+  if ((collect = calloc(sizeof(ppd_choice_t *),
+                        cupsArrayCount(ppd->marked))) == NULL)
+  {
+    *choices = NULL;
+    return (0);
+  }
+
+  if ((orders = calloc(sizeof(float), cupsArrayCount(ppd->marked))) == NULL)
+  {
+    *choices = NULL;
+    free(collect);
+    return (0);
+  }
 
  /*
   * Loop through all options and add choices as needed...
@@ -391,6 +416,7 @@ ppdEmitJCL(ppd_file_t *ppd,         /* I - PPD file record */
     */
 
     ppd_attr_t *charset;               /* PJL charset */
+    ppd_attr_t *display;               /* PJL display command */
 
 
     if ((charset = ppdFindAttr(ppd, "cupsPJLCharset", NULL)) != NULL)
@@ -399,6 +425,12 @@ ppdEmitJCL(ppd_file_t *ppd,                /* I - PPD file record */
         charset = NULL;
     }
 
+    if ((display = ppdFindAttr(ppd, "cupsPJLDisplay", NULL)) != NULL)
+    {
+      if (!display->value)
+        display = NULL;
+    }
+
     fputs("\033%-12345X@PJL\n", fp);
     for (ptr = ppd->jcl_begin + 9; *ptr;)
       if (!strncmp(ptr, "@PJL JOB", 8))
@@ -455,9 +487,16 @@ ppdEmitJCL(ppd_file_t *ppd,                /* I - PPD file record */
     * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
     */
 
-    fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp,
-            job_id, user, temp);
-    fprintf(fp, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id, user, temp);
+    if (display && strcmp(display->value, "job"))
+    {
+      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);
+    }
+    else
+      fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp,
+             job_id, user, temp);
   }
   else
     fputs(ppd->jcl_begin, fp);
@@ -510,7 +549,7 @@ ppdEmitJCLEnd(ppd_file_t *ppd,              /* I - PPD file record */
     */
 
     fputs("\033%-12345X@PJL\n", fp);
-    fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp);
+    fputs("@PJL RDYMSG DISPLAY = \"\"\n", fp);
     fputs(ppd->jcl_end + 9, fp);
   }
   else
@@ -529,12 +568,12 @@ ppdEmitJCLEnd(ppd_file_t *ppd,            /* I - PPD file record */
  * returned string.
  *
  * The return string is allocated on the heap and should be freed using
- * free() when you are done with it.
+ * @code free@ when you are done with it.
  *
  * @since CUPS 1.2@
  */
 
-char *                                 /* O - String containing option code */
+char *                                 /* O - String containing option code or @code NULL@ if there is no option code */
 ppdEmitString(ppd_file_t    *ppd,      /* I - PPD file record */
               ppd_section_t section,   /* I - Section to write */
              float         min_order)  /* I - Lowest OrderDependency */
@@ -582,7 +621,40 @@ ppdEmitString(ppd_file_t    *ppd,  /* I - PPD file record */
 
   for (i = 0, bufsize = 1; i < count; i ++)
   {
-    if (section != PPD_ORDER_EXIT && section != PPD_ORDER_JCL)
+    if (section == PPD_ORDER_JCL)
+    {
+      if (!strcasecmp(choices[i]->choice, "Custom") &&
+         (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
+             != NULL)
+      {
+       /*
+        * Add space to account for custom parameter substitution...
+       */
+
+        for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
+            cparam;
+            cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
+       {
+          switch (cparam->type)
+         {
+           case PPD_CUSTOM_CURVE :
+           case PPD_CUSTOM_INVCURVE :
+           case PPD_CUSTOM_POINTS :
+           case PPD_CUSTOM_REAL :
+           case PPD_CUSTOM_INT :
+               bufsize += 10;
+               break;
+
+           case PPD_CUSTOM_PASSCODE :
+           case PPD_CUSTOM_PASSWORD :
+           case PPD_CUSTOM_STRING :
+               bufsize += strlen(cparam->current.custom_string);
+               break;
+          }
+       }
+      }
+    }
+    else if (section != PPD_ORDER_EXIT)
     {
       bufsize += 3;                    /* [{\n */
 
@@ -645,7 +717,8 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
   * Allocate memory...
   */
 
-  DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n", bufsize));
+  DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n",
+                (int)bufsize));
 
   if ((buffer = calloc(1, bufsize)) == NULL)
   {
@@ -661,7 +734,89 @@ ppdEmitString(ppd_file_t    *ppd,  /* I - PPD file record */
   */
 
   for (i = 0, bufptr = buffer; i < count; i ++, bufptr += strlen(bufptr))
-    if (section != PPD_ORDER_EXIT && section != PPD_ORDER_JCL)
+    if (section == PPD_ORDER_JCL)
+    {
+      if (!strcasecmp(choices[i]->choice, "Custom") &&
+          (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
+             != NULL)
+      {
+       /*
+        * Handle substitutions in custom JCL options...
+       */
+
+       char    *cptr;                  /* Pointer into code */
+       int     pnum;                   /* Parameter number */
+
+
+        for (cptr = choices[i]->code; *cptr && bufptr < bufend;)
+       {
+         if (*cptr == '\\')
+         {
+           cptr ++;
+
+           if (isdigit(*cptr & 255))
+           {
+            /*
+             * Substitute parameter...
+             */
+
+              pnum = *cptr++ - '0';
+             while (isalnum(*cptr & 255))
+               pnum = pnum * 10 + *cptr - '0';
+
+              for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
+                  cparam;
+                  cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
+               if (cparam->order == pnum)
+                 break;
+
+              if (cparam)
+             {
+               switch (cparam->type)
+               {
+                 case PPD_CUSTOM_CURVE :
+                 case PPD_CUSTOM_INVCURVE :
+                 case PPD_CUSTOM_POINTS :
+                 case PPD_CUSTOM_REAL :
+                     bufptr = _cupsStrFormatd(bufptr, bufend,
+                                              cparam->current.custom_real,
+                                              loc);
+                     break;
+
+                 case PPD_CUSTOM_INT :
+                     snprintf(bufptr, bufend - bufptr, "%d",
+                              cparam->current.custom_int);
+                     bufptr += strlen(bufptr);
+                     break;
+
+                 case PPD_CUSTOM_PASSCODE :
+                 case PPD_CUSTOM_PASSWORD :
+                 case PPD_CUSTOM_STRING :
+                     strlcpy(bufptr, cparam->current.custom_string,
+                             bufend - bufptr);
+                     bufptr += strlen(bufptr);
+                     break;
+               }
+             }
+           }
+           else if (*cptr)
+             *bufptr++ = *cptr++;
+         }
+         else
+           *bufptr++ = *cptr++;
+       }
+      }
+      else
+      {
+       /*
+        * Otherwise just copy the option code directly...
+       */
+
+        strlcpy(bufptr, choices[i]->code, bufend - bufptr + 1);
+        bufptr += strlen(bufptr);
+      }
+    }
+    else if (section != PPD_ORDER_EXIT)
     {
      /*
       * Add wrapper commands to prevent printer errors for unsupported
@@ -794,8 +949,7 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
        }
       }
       else if (!strcasecmp(choices[i]->choice, "Custom") &&
-               (coption = ppdFindCustomOption(ppd,
-                                             choices[i]->option->keyword))
+               (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
                   != NULL)
       {
        /*
@@ -803,15 +957,23 @@ ppdEmitString(ppd_file_t    *ppd, /* I - PPD file record */
        */
 
         const char     *s;             /* Pointer into string value */
+        cups_array_t   *params;        /* Parameters in the correct output order */
 
 
+        params = cupsArrayNew((cups_array_func_t)ppd_compare_cparams, NULL);
+
+        for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
+            cparam;
+            cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
+          cupsArrayAdd(params, cparam);
+
         snprintf(bufptr, bufend - bufptr + 1,
                 "%%%%BeginFeature: *Custom%s True\n", coption->keyword);
         bufptr += strlen(bufptr);
 
-        for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
+        for (cparam = (ppd_cparam_t *)cupsArrayFirst(params);
             cparam;
-            cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
+            cparam = (ppd_cparam_t *)cupsArrayNext(params))
        {
           switch (cparam->type)
          {
@@ -849,6 +1011,8 @@ ppdEmitString(ppd_file_t    *ppd,  /* I - PPD file record */
                break;
           }
        }
+
+       cupsArrayDelete(params);
       }
       else
       {
@@ -872,7 +1036,7 @@ ppdEmitString(ppd_file_t    *ppd,  /* I - PPD file record */
       bufptr += strlen(bufptr);
 
       DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
-                    bufptr - buffer));
+                    (int)(bufptr - buffer)));
     }
     else
     {
@@ -892,12 +1056,24 @@ ppdEmitString(ppd_file_t    *ppd,        /* I - PPD file record */
 }
 
 
+/*
+ * 'ppd_compare_cparams()' - Compare the order of two custom parameters.
+ */
+
+static int                             /* O - Result of comparison */
+ppd_compare_cparams(ppd_cparam_t *a,   /* I - First parameter */
+                    ppd_cparam_t *b)   /* I - Second parameter */
+{
+  return (a->order - b->order);
+}
+
+
 /*
  * 'ppd_handle_media()' - Handle media selection...
  */
 
 static void
-ppd_handle_media(ppd_file_t *ppd)
+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 */
@@ -973,5 +1149,5 @@ ppd_handle_media(ppd_file_t *ppd)
 
 
 /*
- * End of "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $".
+ * End of "$Id: emit.c 7863 2008-08-26 03:39:59Z mike $".
  */