]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/ppd.c
Merge changes from CUPS 1.4svn-r7844.
[thirdparty/cups.git] / cups / ppd.c
index 50ad5787f08325cf435330279d86a564bd56d826..110f230ef00884eed4546af89dcc307b609b3988 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: ppd.c 6937 2007-09-10 21:13:31Z mike $"
+ * "$Id: ppd.c 7552 2008-05-12 17:42:15Z mike $"
  *
  *   PPD file 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
@@ -40,7 +40,6 @@
  *   ppd_add_size()         - Add a page size.
  *   ppd_compare_attrs()    - Compare two attributes.
  *   ppd_compare_choices()  - Compare two choices...
- *   ppd_compare_consts()   - Compare two constraints.
  *   ppd_compare_coptions() - Compare two custom options.
  *   ppd_compare_cparams()  - Compare two custom parameters.
  *   ppd_compare_options()  - Compare two options.
@@ -60,6 +59,7 @@
  * Include necessary headers.
  */
 
+#include "ppd-private.h"
 #include "globals.h"
 #include "debug.h"
 #include <stdlib.h>
@@ -109,7 +109,6 @@ static ppd_choice_t *ppd_add_choice(ppd_option_t *option, const char *name);
 static ppd_size_t      *ppd_add_size(ppd_file_t *ppd, const char *name);
 static int             ppd_compare_attrs(ppd_attr_t *a, ppd_attr_t *b);
 static int             ppd_compare_choices(ppd_choice_t *a, ppd_choice_t *b);
-static int             ppd_compare_consts(ppd_const_t *a, ppd_const_t *b);
 static int             ppd_compare_coptions(ppd_coption_t *a,
                                             ppd_coption_t *b);
 static int             ppd_compare_cparams(ppd_cparam_t *a, ppd_cparam_t *b);
@@ -293,6 +292,26 @@ ppdClose(ppd_file_t *ppd)          /* I - PPD file record */
 
   cupsArrayDelete(ppd->coptions);
 
+ /*
+  * Free constraints...
+  */
+
+  if (ppd->cups_uiconstraints)
+  {
+    _ppd_cups_uiconsts_t *consts;      /* Current constraints */
+
+
+    for (consts = (_ppd_cups_uiconsts_t *)cupsArrayFirst(ppd->cups_uiconstraints);
+         consts;
+        consts = (_ppd_cups_uiconsts_t *)cupsArrayNext(ppd->cups_uiconstraints))
+    {
+      free(consts->constraints);
+      free(consts);
+    }
+
+    cupsArrayDelete(ppd->cups_uiconstraints);
+  }
+
  /*
   * Free the whole record...
   */
@@ -428,7 +447,7 @@ ppdOpen(FILE *fp)                   /* I - File to read from */
  * @since CUPS 1.2@
  */
 
-ppd_file_t *                           /* O - PPD file record */
+ppd_file_t *                           /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
 ppdOpen2(cups_file_t *fp)              /* I - File to read from */
 {
   int                  i, j, k;        /* Looping vars */
@@ -569,6 +588,7 @@ ppdOpen2(cups_file_t *fp)           /* I - File to read from */
       cg->ppd_status = PPD_MISSING_PPDADOBE4;
 
     _cupsStrFree(string);
+    ppd_free(line.buffer);
 
     return (NULL);
   }
@@ -585,12 +605,15 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
   {
     cg->ppd_status = PPD_ALLOC_ERROR;
 
+    _cupsStrFree(string);
+    ppd_free(line.buffer);
+
     return (NULL);
   }
 
   ppd->language_level = 2;
   ppd->color_device   = 0;
-  ppd->colorspace     = PPD_CS_GRAY;
+  ppd->colorspace     = PPD_CS_N;
   ppd->landscape      = -90;
   ppd->coptions       = cupsArrayNew((cups_array_func_t)ppd_compare_coptions,
                                      NULL);
@@ -615,31 +638,11 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
 
   while ((mask = ppd_read(fp, &line, keyword, name, text, &string, 1, cg)) != 0)
   {
-#ifdef DEBUG
-    printf("mask = %x, keyword = \"%s\"", mask, keyword);
+    DEBUG_printf(("mask=%x, keyword=\"%s\", name=\"%s\", text=\"%s\", "
+                  "string=%d chars...", mask, keyword, name, text,
+                 string ? (int)strlen(string) : 0));
 
-    if (name[0] != '\0')
-      printf(", name = \"%s\"", name);
-
-    if (text[0] != '\0')
-      printf(", text = \"%s\"", text);
-
-    if (string != NULL)
-    {
-      if (strlen(string) > 40)
-        printf(", string = %p", string);
-      else
-        printf(", string = \"%s\"", string);
-    }
-
-    puts("");
-#endif /* DEBUG */
-
-    if (strcmp(keyword, "CloseUI") && strcmp(keyword, "CloseGroup") &&
-       strcmp(keyword, "CloseSubGroup") && strncmp(keyword, "Default", 7) &&
-        strcmp(keyword, "JCLCloseUI") && strcmp(keyword, "JCLOpenUI") &&
-       strcmp(keyword, "OpenUI") && strcmp(keyword, "OpenGroup") &&
-       strcmp(keyword, "OpenSubGroup") && string == NULL)
+    if (strncmp(keyword, "Default", 7) && !string)
     {
      /*
       * Need a string value!
@@ -826,6 +829,13 @@ ppdOpen2(cups_file_t *fp)          /* I - File to read from */
         profile = realloc(ppd->profiles, sizeof(ppd_profile_t) *
                                         (ppd->num_profiles + 1));
 
+      if (!profile)
+      {
+        cg->ppd_status = PPD_ALLOC_ERROR;
+
+       goto error;
+      }
+
       ppd->profiles     = profile;
       profile           += ppd->num_profiles;
       ppd->num_profiles ++;
@@ -1044,19 +1054,23 @@ ppdOpen2(cups_file_t *fp)               /* I - File to read from */
        * Add the "custom" option...
        */
 
-       if ((choice = ppd_add_choice(custom_option, "Custom")) == NULL)
-       {
-         DEBUG_puts("Unable to add Custom choice!");
+        if ((choice = ppdFindChoice(custom_option, "Custom")) == NULL)
+         if ((choice = ppd_add_choice(custom_option, "Custom")) == NULL)
+         {
+           DEBUG_puts("Unable to add Custom choice!");
 
-         cg->ppd_status = PPD_ALLOC_ERROR;
+           cg->ppd_status = PPD_ALLOC_ERROR;
 
-         goto error;
-       }
+           goto error;
+         }
 
        strlcpy(choice->text, text[0] ? text : _("Custom"),
                sizeof(choice->text));
 
        choice->code = _cupsStrAlloc(string);
+
+       if (custom_option->section == PPD_ORDER_JCL)
+         ppd_decode(choice->code);
       }
 
      /*
@@ -1080,14 +1094,15 @@ ppdOpen2(cups_file_t *fp)               /* I - File to read from */
 
         if (custom_option)
        {
-         if ((choice = ppd_add_choice(custom_option, "Custom")) == NULL)
-         {
-           DEBUG_puts("Unable to add Custom choice!");
+         if ((choice = ppdFindChoice(custom_option, "Custom")) == NULL)
+           if ((choice = ppd_add_choice(custom_option, "Custom")) == NULL)
+           {
+             DEBUG_puts("Unable to add Custom choice!");
 
-           cg->ppd_status = PPD_ALLOC_ERROR;
+             cg->ppd_status = PPD_ALLOC_ERROR;
 
-           goto error;
-         }
+             goto error;
+           }
 
          strlcpy(choice->text, text[0] ? text : _("Custom"),
                  sizeof(choice->text));
@@ -1101,7 +1116,7 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
       else if (!strcmp(string, "Plus90"))
         ppd->landscape = 90;
     }
-    else if (!strcmp(keyword, "Emulators"))
+    else if (!strcmp(keyword, "Emulators") && string)
     {
       for (count = 1, sptr = string; sptr != NULL;)
         if ((sptr = strchr(sptr, ' ')) != NULL)
@@ -1112,7 +1127,12 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
        }
 
       ppd->num_emulations = count;
-      ppd->emulations     = calloc(count, sizeof(ppd_emul_t));
+      if ((ppd->emulations = calloc(count, sizeof(ppd_emul_t))) == NULL)
+      {
+        cg->ppd_status = PPD_ALLOC_ERROR;
+
+       goto error;
+      }
 
       for (i = 0, sptr = string; i < count; i ++)
       {
@@ -1187,7 +1207,7 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
       * Add an option record to the current sub-group, group, or file...
       */
 
-      DEBUG_printf(("name=\"%s\" (%d)\n", name, strlen(name)));
+      DEBUG_printf(("name=\"%s\" (%d)\n", name, (int)strlen(name)));
 
       if (name[0] == '*')
         _cups_strcpy(name, name + 1); /* Eliminate leading asterisk */
@@ -1287,14 +1307,15 @@ ppdOpen2(cups_file_t *fp)               /* I - File to read from */
 
       if ((custom_attr = ppdFindAttr(ppd, custom_name, "True")) != NULL)
       {
-       if ((choice = ppd_add_choice(option, "Custom")) == NULL)
-       {
-         DEBUG_puts("Unable to add Custom choice!");
+        if ((choice = ppdFindChoice(option, "Custom")) == NULL)
+         if ((choice = ppd_add_choice(option, "Custom")) == NULL)
+         {
+           DEBUG_puts("Unable to add Custom choice!");
 
-         cg->ppd_status = PPD_ALLOC_ERROR;
+           cg->ppd_status = PPD_ALLOC_ERROR;
 
-         goto error;
-       }
+           goto error;
+         }
 
        strlcpy(choice->text,
                custom_attr->text[0] ? custom_attr->text : _("Custom"),
@@ -1755,52 +1776,6 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
            break;
       }
 
-     /*
-      * For CustomPageSize and InputSlot/ManualFeed, create a duplicate
-      * constraint for PageRegion...
-      */
-
-      if (!strcasecmp(constraint->option1, "CustomPageSize") &&
-          (!strcasecmp(constraint->option2, "InputSlot") ||
-          !strcasecmp(constraint->option2, "ManualFeed")))
-      {
-        ppd->num_consts ++;
-
-        strcpy(constraint[1].option1, "PageRegion");
-       strcpy(constraint[1].choice1, "Custom");
-       strcpy(constraint[1].option2, constraint->option2);
-       strcpy(constraint[1].choice2, constraint->choice2);
-      }
-      else if (!strcasecmp(constraint->option2, "CustomPageSize") &&
-               (!strcasecmp(constraint->option1, "InputSlot") ||
-               !strcasecmp(constraint->option1, "ManualFeed")))
-      {
-        ppd->num_consts ++;
-
-       strcpy(constraint[1].option1, constraint->option1);
-       strcpy(constraint[1].choice1, constraint->choice1);
-        strcpy(constraint[1].option2, "PageRegion");
-       strcpy(constraint[1].choice2, "Custom");
-      }
-
-     /*
-      * Handle CustomFoo option constraints...
-      */
-
-      if (!strncasecmp(constraint->option1, "Custom", 6) &&
-          !strcasecmp(constraint->choice1, "True"))
-      {
-        _cups_strcpy(constraint->option1, constraint->option1 + 6);
-       strcpy(constraint->choice1, "Custom");
-      }
-
-      if (!strncasecmp(constraint->option2, "Custom", 6) &&
-          !strcasecmp(constraint->choice2, "True"))
-      {
-        _cups_strcpy(constraint->option2, constraint->option2 + 6);
-       strcpy(constraint->choice2, "Custom");
-      }
-
      /*
       * Don't add this one as an attribute...
       */
@@ -1875,7 +1850,12 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
       * Add the option choice...
       */
 
-      choice = ppd_add_choice(option, name);
+      if ((choice = ppd_add_choice(option, name)) == NULL)
+      {
+        cg->ppd_status = PPD_ALLOC_ERROR;
+
+       goto error;
+      }
 
       if (text[0])
         cupsCharsetToUTF8((cups_utf8_t *)choice->text, text,
@@ -1905,8 +1885,7 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
       _cupsStrFree(string);
   }
 
-  if (line.buffer)
-    free(line.buffer);
+  ppd_free(line.buffer);
 
  /*
   * Reset language preferences...
@@ -1916,7 +1895,8 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
 
 #ifdef DEBUG
   if (!cupsFileEOF(fp))
-    printf("Premature EOF at %lu...\n", (unsigned long)cupsFileTell(fp));
+    DEBUG_printf(("Premature EOF at %lu...\n",
+                  (unsigned long)cupsFileTell(fp)));
 #endif /* DEBUG */
 
   if (cg->ppd_status != PPD_OK)
@@ -1960,14 +1940,6 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
     }
   }
 
- /*
-  * Sort the constraints...
-  */
-
-  if (ppd->num_consts > 1)
-    qsort(ppd->consts, ppd->num_consts, sizeof(ppd_const_t),
-          (int (*)(const void *, const void *))ppd_compare_consts);
-
  /*
   * Create an array to track the marked choices...
   */
@@ -1986,10 +1958,8 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
 
   error:
 
-  if (line.buffer)
-    free(line.buffer);
-
   _cupsStrFree(string);
+  ppd_free(line.buffer);
 
   ppdClose(ppd);
 
@@ -2003,7 +1973,7 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
  * 'ppdOpenFd()' - Read a PPD file into memory.
  */
 
-ppd_file_t *                           /* O - PPD file record */
+ppd_file_t *                           /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
 ppdOpenFd(int fd)                      /* I - File to read from */
 {
   cups_file_t          *fp;            /* CUPS file pointer */
@@ -2053,7 +2023,7 @@ ppdOpenFd(int fd)                 /* I - File to read from */
  * 'ppdOpenFile()' - Read a PPD file into memory.
  */
 
-ppd_file_t *                           /* O - PPD file record */
+ppd_file_t *                           /* O - PPD file record or @code NULL@ if the PPD file could not be opened. */
 ppdOpenFile(const char *filename)      /* I - File to read from */
 {
   cups_file_t          *fp;            /* File pointer */
@@ -2282,28 +2252,6 @@ ppd_compare_choices(ppd_choice_t *a,     /* I - First choice */
 }
 
 
-/*
- * 'ppd_compare_consts()' - Compare two constraints.
- */
-
-static int                             /* O - Result of comparison */
-ppd_compare_consts(ppd_const_t *a,     /* I - First constraint */
-                   ppd_const_t *b)     /* I - Second constraint */
-{
-  int  ret;                            /* Result of comparison */
-
-
-  if ((ret = strcmp(a->option1, b->option1)) != 0)
-    return (ret);
-  else if ((ret = strcmp(a->choice1, b->choice1)) != 0)
-    return (ret);
-  else if ((ret = strcmp(a->option2, b->option2)) != 0)
-    return (ret);
-  else
-    return (strcmp(a->choice2, b->choice2));
-}
-
-
 /*
  * 'ppd_compare_coptions()' - Compare two custom options.
  */
@@ -2852,8 +2800,6 @@ ppd_read(cups_file_t    *fp,              /* I - File to read from */
            if (ch == 0x0a)
              cupsFileGetChar(fp);
          }
-
-         ch = '\n';
        }
        else if (ch < ' ' && ch != '\t' && cg->ppd_conform == PPD_CONFORM_STRICT)
        {
@@ -2948,7 +2894,7 @@ ppd_read(cups_file_t    *fp,              /* I - File to read from */
 
     *lineptr = '\0';
 
-    DEBUG_printf(("LINE = \"%s\"\n", line));
+    DEBUG_printf(("LINE=\"%s\"\n", line->buffer));
 
    /*
     * The dynamically created PPDs for older style Mac OS X
@@ -3171,5 +3117,5 @@ ppd_read(cups_file_t    *fp,              /* I - File to read from */
 
 
 /*
- * End of "$Id: ppd.c 6937 2007-09-10 21:13:31Z mike $".
+ * End of "$Id: ppd.c 7552 2008-05-12 17:42:15Z mike $".
  */