]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/ppd.c
Load cups into easysw/current.
[thirdparty/cups.git] / cups / ppd.c
index c131493202f49cb5eb38f2bbcdc8f9666240ce9c..d68a1209cd1cb664d21b97f85c3c272c3b9a986d 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: ppd.c 5200 2006-02-28 00:10:32Z mike $"
+ * "$Id: ppd.c 6479 2007-04-27 11:44:10Z mike $"
  *
  *   PPD file routines for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
  *   property of Easy Software Products and are protected by Federal
@@ -36,6 +36,8 @@
  *
  *   ppdClose()             - Free all memory used by the PPD file.
  *   ppdErrorString()       - Returns the text assocated with a status.
+ *   _ppdGetEncoding()      - Get the CUPS encoding value for the given
+ *                            LanguageEncoding.
  *   ppdLastError()         - Return the status from the last ppdOpen*().
  *   ppdOpen()              - Read a PPD file into memory.
  *   ppdOpen2()             - Read a PPD file into memory.
@@ -46,6 +48,8 @@
  *   ppd_add_choice()       - Add a choice to an option.
  *   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.
@@ -56,6 +60,7 @@
  *   ppd_get_cparam()       - Get a custom parameter record.
  *   ppd_get_group()        - Find or create the named group as needed.
  *   ppd_get_option()       - Find or create the named option as needed.
+ *   ppd_hash_option()      - Generate a hash of the option name...
  *   ppd_read()             - Read a line from a PPD file, skipping comment
  *                            lines as necessary.
  */
@@ -88,6 +93,8 @@
 #define PPD_TEXT       4               /* Line contained human-readable text */
 #define PPD_STRING     8               /* Line contained a string or code */
 
+#define PPD_HASHSIZE   512             /* Size of hash */
+
 
 /*
  * Local functions...
@@ -99,6 +106,8 @@ static ppd_attr_t    *ppd_add_attr(ppd_file_t *ppd, const char *name,
 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);
@@ -111,8 +120,10 @@ static ppd_cparam_t        *ppd_get_cparam(ppd_coption_t *opt,
                                        const char *param,
                                        const char *text);
 static ppd_group_t     *ppd_get_group(ppd_file_t *ppd, const char *name,
-                                      const char *text, _cups_globals_t *cg);
+                                      const char *text, _cups_globals_t *cg,
+                                      cups_encoding_t encoding);
 static ppd_option_t    *ppd_get_option(ppd_group_t *group, const char *name);
+static int             ppd_hash_option(ppd_option_t *option);
 static int             ppd_read(cups_file_t *fp, char *keyword, char *option,
                                 char *text, char **string, int ignoreblank,
                                 _cups_globals_t *cg);
@@ -146,6 +157,8 @@ ppdClose(ppd_file_t *ppd)           /* I - PPD file record */
   * Free all strings at the top level...
   */
 
+  ppd_free(ppd->lang_encoding);
+  ppd_free(ppd->nickname);
   ppd_free(ppd->patches);
   ppd_free(ppd->jcl_begin);
   ppd_free(ppd->jcl_end);
@@ -179,6 +192,7 @@ ppdClose(ppd_file_t *ppd)           /* I - PPD file record */
   }
 
   cupsArrayDelete(ppd->options);
+  cupsArrayDelete(ppd->marked);
 
  /*
   * Free any page sizes...
@@ -330,6 +344,31 @@ ppdErrorString(ppd_status_t status)        /* I - PPD status */
 }
 
 
+/*
+ * '_ppdGetEncoding()' - Get the CUPS encoding value for the given
+ *                       LanguageEncoding.
+ */
+
+cups_encoding_t                                /* O - CUPS encoding value */
+_ppdGetEncoding(const char *name)      /* I - LanguageEncoding string */
+{
+  if (!strcasecmp(name, "ISOLatin1"))
+    return (CUPS_ISO8859_1);
+  else if (!strcasecmp(name, "ISOLatin2"))
+    return (CUPS_ISO8859_2);
+  else if (!strcasecmp(name, "ISOLatin5"))
+    return (CUPS_ISO8859_5);
+  else if (!strcasecmp(name, "JIS83-RKSJ"))
+    return (CUPS_WINDOWS_932);
+  else if (!strcasecmp(name, "MacStandard"))
+    return (CUPS_MAC_ROMAN);
+  else if (!strcasecmp(name, "WindowsANSI"))
+    return (CUPS_WINDOWS_1252);
+  else
+    return (CUPS_UTF8);
+}
+
+
 /*
  * 'ppdLastError()' - Return the status from the last ppdOpen*().
  *
@@ -393,7 +432,6 @@ ppdOpen(FILE *fp)                   /* I - File to read from */
 ppd_file_t *                           /* O - PPD file record */
 ppdOpen2(cups_file_t *fp)              /* I - File to read from */
 {
-  char                 *oldlocale;     /* Old locale settings */
   int                  i, j, k;        /* Looping vars */
   int                  count;          /* Temporary count */
   ppd_file_t           *ppd;           /* PPD file record */
@@ -420,7 +458,9 @@ ppdOpen2(cups_file_t *fp)           /* I - File to read from */
   ppd_profile_t                *profile;       /* Pointer to color profile */
   char                 **filter;       /* Pointer to filter */
   cups_lang_t          *language;      /* Default language */
+  struct lconv         *loc;           /* Locale data */
   int                  ui_keyword;     /* Is this line a UI keyword? */
+  cups_encoding_t      encoding;       /* Encoding of PPD file */
   _cups_globals_t      *cg = _cupsGlobals();
                                        /* Global data */
   static const char * const ui_keywords[] =
@@ -554,12 +594,7 @@ ppdOpen2(cups_file_t *fp)          /* I - File to read from */
   */
 
   language = cupsLangDefault();
-
-#ifdef LC_NUMERIC
-  oldlocale = _cupsSaveLocale(LC_NUMERIC, "C");
-#else
-  oldlocale = _cupsSaveLocale(LC_ALL, "C");
-#endif /* LC_NUMERIC */
+  loc      = localeconv();
 
  /*
   * Read lines from the PPD file and add them to the file record...
@@ -570,6 +605,7 @@ ppdOpen2(cups_file_t *fp)           /* I - File to read from */
   option     = NULL;
   choice     = NULL;
   ui_keyword = 0;
+  encoding   = CUPS_ISO8859_1;
 
   while ((mask = ppd_read(fp, keyword, name, text, &string, 1, cg)) != 0)
   {
@@ -648,7 +684,8 @@ ppdOpen2(cups_file_t *fp)           /* I - File to read from */
 
         if (!group)
        {
-          if ((group = ppd_get_group(ppd, "General", _("General"), cg)) == NULL)
+          if ((group = ppd_get_group(ppd, "General", _("General"), cg,
+                                    encoding)) == NULL)
            goto error;
 
           DEBUG_printf(("Adding to group %s...\n", group->text));
@@ -711,7 +748,14 @@ ppdOpen2(cups_file_t *fp)          /* I - File to read from */
     if (!strcmp(keyword, "LanguageLevel"))
       ppd->language_level = atoi(string);
     else if (!strcmp(keyword, "LanguageEncoding"))
-      ppd->lang_encoding = string;
+    {
+     /*
+      * Say all PPD files are UTF-8, since we convert to UTF-8...
+      */
+
+      ppd->lang_encoding = strdup("UTF-8");
+      encoding           = _ppdGetEncoding(string);
+    }
     else if (!strcmp(keyword, "LanguageVersion"))
       ppd->lang_version = string;
     else if (!strcmp(keyword, "Manufacturer"))
@@ -723,7 +767,18 @@ ppdOpen2(cups_file_t *fp)          /* I - File to read from */
     else if (!strcmp(keyword, "PCFileName"))
       ppd->pcfilename = string;
     else if (!strcmp(keyword, "NickName"))
-      ppd->nickname = string;
+    {
+      if (encoding != CUPS_UTF8)
+      {
+        cups_utf8_t    utf8[256];      /* UTF-8 version of NickName */
+
+
+        cupsCharsetToUTF8(utf8, string, sizeof(utf8), encoding);
+       ppd->nickname = strdup((char *)utf8);
+      }
+      else
+        ppd->nickname = strdup(string);
+    }
     else if (!strcmp(keyword, "Product"))
       ppd->product = string;
     else if (!strcmp(keyword, "ShortNickName"))
@@ -772,13 +827,18 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
       memset(profile, 0, sizeof(ppd_profile_t));
       strlcpy(profile->resolution, name, sizeof(profile->resolution));
       strlcpy(profile->media_type, text, sizeof(profile->media_type));
-      sscanf(string, "%f%f%f%f%f%f%f%f%f%f%f", &(profile->density),
-            &(profile->gamma),
-            profile->matrix[0] + 0, profile->matrix[0] + 1,
-            profile->matrix[0] + 2, profile->matrix[1] + 0,
-            profile->matrix[1] + 1, profile->matrix[1] + 2,
-            profile->matrix[2] + 0, profile->matrix[2] + 1,
-            profile->matrix[2] + 2);
+
+      profile->density      = (float)_cupsStrScand(string, &sptr, loc);
+      profile->gamma        = (float)_cupsStrScand(sptr, &sptr, loc);
+      profile->matrix[0][0] = (float)_cupsStrScand(sptr, &sptr, loc);
+      profile->matrix[0][1] = (float)_cupsStrScand(sptr, &sptr, loc);
+      profile->matrix[0][2] = (float)_cupsStrScand(sptr, &sptr, loc);
+      profile->matrix[1][0] = (float)_cupsStrScand(sptr, &sptr, loc);
+      profile->matrix[1][1] = (float)_cupsStrScand(sptr, &sptr, loc);
+      profile->matrix[1][2] = (float)_cupsStrScand(sptr, &sptr, loc);
+      profile->matrix[2][0] = (float)_cupsStrScand(sptr, &sptr, loc);
+      profile->matrix[2][1] = (float)_cupsStrScand(sptr, &sptr, loc);
+      profile->matrix[2][2] = (float)_cupsStrScand(sptr, &sptr, loc);
     }
     else if (!strcmp(keyword, "cupsFilter"))
     {
@@ -877,8 +937,8 @@ ppdOpen2(cups_file_t *fp)           /* I - File to read from */
       if (!strcmp(ctype, "curve"))
       {
         cparam->type = PPD_CUSTOM_CURVE;
-       cparam->minimum.custom_curve = atof(cminimum);
-       cparam->maximum.custom_curve = atof(cmaximum);
+       cparam->minimum.custom_curve = (float)_cupsStrScand(cminimum, NULL, loc);
+       cparam->maximum.custom_curve = (float)_cupsStrScand(cmaximum, NULL, loc);
       }
       else if (!strcmp(ctype, "int"))
       {
@@ -889,8 +949,8 @@ ppdOpen2(cups_file_t *fp)           /* I - File to read from */
       else if (!strcmp(ctype, "invcurve"))
       {
         cparam->type = PPD_CUSTOM_INVCURVE;
-       cparam->minimum.custom_invcurve = atof(cminimum);
-       cparam->maximum.custom_invcurve = atof(cmaximum);
+       cparam->minimum.custom_invcurve = (float)_cupsStrScand(cminimum, NULL, loc);
+       cparam->maximum.custom_invcurve = (float)_cupsStrScand(cmaximum, NULL, loc);
       }
       else if (!strcmp(ctype, "passcode"))
       {
@@ -907,14 +967,14 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
       else if (!strcmp(ctype, "points"))
       {
         cparam->type = PPD_CUSTOM_POINTS;
-       cparam->minimum.custom_points = atof(cminimum);
-       cparam->maximum.custom_points = atof(cmaximum);
+       cparam->minimum.custom_points = (float)_cupsStrScand(cminimum, NULL, loc);
+       cparam->maximum.custom_points = (float)_cupsStrScand(cmaximum, NULL, loc);
       }
       else if (!strcmp(ctype, "real"))
       {
         cparam->type = PPD_CUSTOM_REAL;
-       cparam->minimum.custom_real = atof(cminimum);
-       cparam->maximum.custom_real = atof(cmaximum);
+       cparam->minimum.custom_real = (float)_cupsStrScand(cminimum, NULL, loc);
+       cparam->maximum.custom_real = (float)_cupsStrScand(cmaximum, NULL, loc);
       }
       else if (!strcmp(ctype, "string"))
       {
@@ -948,14 +1008,12 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
       }
     }
     else if (!strcmp(keyword, "HWMargins"))
-      sscanf(string, "%f%f%f%f", ppd->custom_margins + 0,
-             ppd->custom_margins + 1, ppd->custom_margins + 2,
-             ppd->custom_margins + 3);
-    else if (!strncmp(keyword, "Custom", 6) && !strcmp(name, "True"))
     {
-      ppd_coption_t    *coption;       /* Custom option */
-
-
+      for (i = 0, sptr = string; i < 4; i ++)
+        ppd->custom_margins[i] = (float)_cupsStrScand(sptr, &sptr, loc);
+    }
+    else if (!strncmp(keyword, "Custom", 6) && !strcmp(name, "True") && !option)
+    {
       DEBUG_puts("Processing Custom option...");
 
      /*
@@ -969,7 +1027,8 @@ ppdOpen2(cups_file_t *fp)          /* I - File to read from */
 
         DEBUG_printf(("%s option not found for %s...\n", keyword + 6, keyword));
 
-       if ((gtemp = ppd_get_group(ppd, "General", _("General"), cg)) == NULL)
+       if ((gtemp = ppd_get_group(ppd, "General", _("General"), cg,
+                                  encoding)) == NULL)
        {
          DEBUG_puts("Unable to get general group!");
 
@@ -986,7 +1045,7 @@ ppdOpen2(cups_file_t *fp)          /* I - File to read from */
        }
       }
 
-      if ((coption = ppd_get_coption(ppd, keyword + 6)) == NULL)
+      if (!ppd_get_coption(ppd, keyword + 6))
       {
         cg->ppd_status = PPD_ALLOC_ERROR;
 
@@ -1026,6 +1085,34 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
        */
 
        ppd_add_size(ppd, "Custom");
+
+       if ((option = ppdFindOption(ppd, "PageRegion")) == NULL)
+       {
+         ppd_group_t   *gtemp;         /* Temporary group */
+
+         if ((gtemp = ppd_get_group(ppd, "General", _("General"), cg,
+                                    encoding)) == NULL)
+         {
+           DEBUG_puts("Unable to get general group!");
+
+           goto error;
+         }
+
+         option = ppd_get_option(gtemp, "PageRegion");
+        }
+
+       if ((choice = ppd_add_choice(option, "Custom")) == NULL)
+       {
+         DEBUG_puts("Unable to add Custom choice!");
+
+         cg->ppd_status = PPD_ALLOC_ERROR;
+
+         goto error;
+       }
+
+       strlcpy(choice->text, text[0] ? text : _("Custom"),
+               sizeof(choice->text));
+        option = NULL;
       }
     }
     else if (!strcmp(keyword, "LandscapeOrientation"))
@@ -1134,7 +1221,8 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
         option = ppd_get_option(subgroup, name);
       else if (group == NULL)
       {
-       if ((group = ppd_get_group(ppd, "General", _("General"), cg)) == NULL)
+       if ((group = ppd_get_group(ppd, "General", _("General"), cg,
+                                  encoding)) == NULL)
          goto error;
 
         DEBUG_printf(("Adding to group %s...\n", group->text));
@@ -1183,7 +1271,8 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
        }
 
       if (text[0])
-        strlcpy(option->text, text, sizeof(option->text));
+        cupsCharsetToUTF8((cups_utf8_t *)option->text, text,
+                          sizeof(option->text), encoding);
       else
       {
         if (!strcmp(name, "PageSize"))
@@ -1222,7 +1311,7 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
       * Find the JCL group, and add if needed...
       */
 
-      group = ppd_get_group(ppd, "JCL", _("JCL"), cg);
+      group = ppd_get_group(ppd, "JCL", _("JCL"), cg, encoding);
 
       if (group == NULL)
        goto error;
@@ -1272,7 +1361,11 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
          break;
        }
 
-      strlcpy(option->text, text, sizeof(option->text));
+      if (text[0])
+        cupsCharsetToUTF8((cups_utf8_t *)option->text, text,
+                          sizeof(option->text), encoding);
+      else
+        strlcpy(option->text, name, sizeof(option->text));
 
       option->section = PPD_ORDER_JCL;
       group = NULL;
@@ -1326,7 +1419,7 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
       * Find/add the group...
       */
 
-      group = ppd_get_group(ppd, string, sptr, cg);
+      group = ppd_get_group(ppd, string, sptr, cg, encoding);
 
       if (group == NULL)
        goto error;
@@ -1344,7 +1437,9 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
     else if (!strcmp(keyword, "OrderDependency") ||
              !strcmp(keyword, "NonUIOrderDependency"))
     {
-      if (sscanf(string, "%f%40s%40s", &order, name, keyword) != 3)
+      order = (float)_cupsStrScand(string, &sptr, loc);
+
+      if (!sptr || sscanf(sptr, "%40s%40s", name, keyword) != 2)
       {
         cg->ppd_status = PPD_BAD_ORDER_DEPENDENCY;
 
@@ -1465,10 +1560,10 @@ ppdOpen2(cups_file_t *fp)               /* I - File to read from */
              !strcmp(keyword, "NonUIConstraints"))
     {
       if (ppd->num_consts == 0)
-       constraint = calloc(1, sizeof(ppd_const_t));
+       constraint = calloc(2, sizeof(ppd_const_t));
       else
        constraint = realloc(ppd->consts,
-                            (ppd->num_consts + 1) * sizeof(ppd_const_t));
+                            (ppd->num_consts + 2) * sizeof(ppd_const_t));
 
       if (constraint == NULL)
       {
@@ -1491,6 +1586,18 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
            goto error;
 
        case 2 : /* Two options... */
+          /*
+           * Check for broken constraints like "* Option"...
+           */
+
+           if (cg->ppd_conform == PPD_CONFORM_STRICT &&
+               (!strcmp(constraint->option1, "*") ||
+                !strcmp(constraint->choice1, "*")))
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
+
           /*
            * The following strcpy's are safe, as optionN and
            * choiceN are all the same size (size defined by PPD spec...)
@@ -1498,17 +1605,38 @@ ppdOpen2(cups_file_t *fp)               /* I - File to read from */
 
            if (constraint->option1[0] == '*')
              _cups_strcpy(constraint->option1, constraint->option1 + 1);
+           else if (cg->ppd_conform == PPD_CONFORM_STRICT)
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
 
            if (constraint->choice1[0] == '*')
              _cups_strcpy(constraint->option2, constraint->choice1 + 1);
-           else
-             _cups_strcpy(constraint->option2, constraint->choice1);
+           else if (cg->ppd_conform == PPD_CONFORM_STRICT)
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
 
             constraint->choice1[0] = '\0';
             constraint->choice2[0] = '\0';
            break;
            
        case 3 : /* Two options, one choice... */
+          /*
+           * Check for broken constraints like "* Option"...
+           */
+
+           if (cg->ppd_conform == PPD_CONFORM_STRICT &&
+               (!strcmp(constraint->option1, "*") ||
+                !strcmp(constraint->choice1, "*") ||
+                !strcmp(constraint->option2, "*")))
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
+
           /*
            * The following _cups_strcpy's are safe, as optionN and
            * choiceN are all the same size (size defined by PPD spec...)
@@ -1516,9 +1644,21 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
 
            if (constraint->option1[0] == '*')
              _cups_strcpy(constraint->option1, constraint->option1 + 1);
+           else if (cg->ppd_conform == PPD_CONFORM_STRICT)
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
 
            if (constraint->choice1[0] == '*')
            {
+             if (cg->ppd_conform == PPD_CONFORM_STRICT &&
+                 constraint->option2[0] == '*')
+             {
+               cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+               goto error;
+             }
+
              _cups_strcpy(constraint->choice2, constraint->option2);
              _cups_strcpy(constraint->option2, constraint->choice1 + 1);
               constraint->choice1[0] = '\0';
@@ -1527,20 +1667,113 @@ ppdOpen2(cups_file_t *fp)              /* I - File to read from */
            {
              if (constraint->option2[0] == '*')
                _cups_strcpy(constraint->option2, constraint->option2 + 1);
+             else if (cg->ppd_conform == PPD_CONFORM_STRICT)
+             {
+               cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+               goto error;
+             }
 
               constraint->choice2[0] = '\0';
            }
            break;
            
        case 4 : /* Two options, two choices... */
+          /*
+           * Check for broken constraints like "* Option"...
+           */
+
+           if (cg->ppd_conform == PPD_CONFORM_STRICT &&
+               (!strcmp(constraint->option1, "*") ||
+                !strcmp(constraint->choice1, "*") ||
+                !strcmp(constraint->option2, "*") ||
+                !strcmp(constraint->choice2, "*")))
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
+
            if (constraint->option1[0] == '*')
              _cups_strcpy(constraint->option1, constraint->option1 + 1);
+           else if (cg->ppd_conform == PPD_CONFORM_STRICT)
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
+
+            if (cg->ppd_conform == PPD_CONFORM_STRICT &&
+               constraint->choice1[0] == '*')
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
 
            if (constraint->option2[0] == '*')
              _cups_strcpy(constraint->option2, constraint->option2 + 1);
+           else if (cg->ppd_conform == PPD_CONFORM_STRICT)
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
+
+            if (cg->ppd_conform == PPD_CONFORM_STRICT &&
+               constraint->choice2[0] == '*')
+           {
+             cg->ppd_status = PPD_BAD_UI_CONSTRAINTS;
+             goto error;
+           }
            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...
+      */
+
       ppd_free(string);
       string = NULL;
     }
@@ -1560,7 +1793,8 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
        goto error;
       }
 
-      sscanf(string, "%f%f", &(size->width), &(size->length));
+      size->width  = (float)_cupsStrScand(string, &sptr, loc);
+      size->length = (float)_cupsStrScand(sptr, NULL, loc);
 
       ppd_free(string);
       string = NULL;
@@ -1581,8 +1815,10 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
        goto error;
       }
 
-      sscanf(string, "%f%f%f%f", &(size->left), &(size->bottom),
-            &(size->right), &(size->top));
+      size->left   = (float)_cupsStrScand(string, &sptr, loc);
+      size->bottom = (float)_cupsStrScand(sptr, &sptr, loc);
+      size->right  = (float)_cupsStrScand(sptr, &sptr, loc);
+      size->top    = (float)_cupsStrScand(sptr, NULL, loc);
 
       ppd_free(string);
       string = NULL;
@@ -1610,8 +1846,9 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
 
       choice = ppd_add_choice(option, name);
 
-      if (mask & PPD_TEXT)
-        strlcpy(choice->text, text, sizeof(choice->text));
+      if (text[0])
+        cupsCharsetToUTF8((cups_utf8_t *)choice->text, text,
+                          sizeof(choice->text), encoding);
       else if (!strcmp(name, "True"))
         strcpy(choice->text, _("Yes"));
       else if (!strcmp(name, "False"))
@@ -1643,12 +1880,6 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
 
   cupsLangFree(language);
 
-#ifdef LC_NUMERIC
-  _cupsRestoreLocale(LC_NUMERIC, oldlocale);
-#else
-  _cupsRestoreLocale(LC_ALL, oldlocale);
-#endif /* LC_NUMERIC */
-
 #ifdef DEBUG
   if (!feof(fp))
     printf("Premature EOF at %lu...\n", (unsigned long)ftell(fp));
@@ -1670,7 +1901,9 @@ ppdOpen2(cups_file_t *fp)         /* I - File to read from */
   * each choice and custom option...
   */
 
-  ppd->options = cupsArrayNew((cups_array_func_t)ppd_compare_options, NULL);
+  ppd->options = cupsArrayNew2((cups_array_func_t)ppd_compare_options, NULL,
+                               (cups_ahash_func_t)ppd_hash_option,
+                              PPD_HASHSIZE);
 
   for (i = ppd->num_groups, group = ppd->groups;
        i > 0;
@@ -1693,6 +1926,20 @@ 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...
+  */
+
+  ppd->marked = cupsArrayNew((cups_array_func_t)ppd_compare_choices, NULL);
+
  /*
   * Return the PPD file structure...
   */
@@ -1711,12 +1958,6 @@ ppdOpen2(cups_file_t *fp)                /* I - File to read from */
 
   cupsLangFree(language);
 
-#ifdef LC_NUMERIC
-  _cupsRestoreLocale(LC_NUMERIC, oldlocale);
-#else
-  _cupsRestoreLocale(LC_ALL, oldlocale);
-#endif /* LC_NUMERIC */
-
   return (NULL);
 }
 
@@ -1987,10 +2228,42 @@ ppd_compare_attrs(ppd_attr_t *a,        /* I - First attribute */
 
   if ((ret = strcasecmp(a->name, b->name)) != 0)
     return (ret);
-  else if (a->spec[0] && b->spec[0])
+  else
     return (strcasecmp(a->spec, b->spec));
+}
+
+
+/*
+ * 'ppd_compare_choices()' - Compare two choices...
+ */
+
+static int                             /* O - Result of comparison */
+ppd_compare_choices(ppd_choice_t *a,   /* I - First choice */
+                    ppd_choice_t *b)   /* I - Second choice */
+{
+  return (a->option - b->option);
+}
+
+
+/*
+ * '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 (0);
+    return (strcmp(a->choice2, b->choice2));
 }
 
 
@@ -2212,7 +2485,7 @@ ppd_get_cparam(ppd_coption_t *opt,        /* I - PPD file */
     return (NULL);
 
   strlcpy(cparam->name, param, sizeof(cparam->name));
-  strlcpy(cparam->text, text, sizeof(cparam->text));
+  strlcpy(cparam->text, text[0] ? text : param, sizeof(cparam->text));
 
  /*
   * Add this record to the array...
@@ -2233,10 +2506,11 @@ ppd_get_cparam(ppd_coption_t *opt,      /* I - PPD file */
  */
 
 static ppd_group_t *                   /* O - Named group */
-ppd_get_group(ppd_file_t     *ppd,     /* I - PPD file */
-              const char     *name,    /* I - Name of group */
-             const char     *text,     /* I - Text for group */
-              _cups_globals_t *cg)     /* I - Global data */
+ppd_get_group(ppd_file_t      *ppd,    /* I - PPD file */
+              const char      *name,   /* I - Name of group */
+             const char      *text,    /* I - Text for group */
+              _cups_globals_t *cg,     /* I - Global data */
+             cups_encoding_t encoding) /* I - Encoding of text */
 {
   int          i;                      /* Looping var */
   ppd_group_t  *group;                 /* Group */
@@ -2279,7 +2553,9 @@ ppd_get_group(ppd_file_t     *ppd,        /* I - PPD file */
 
     memset(group, 0, sizeof(ppd_group_t));
     strlcpy(group->name, name, sizeof(group->name));
-    strlcpy(group->text, text, sizeof(group->text));
+
+    cupsCharsetToUTF8((cups_utf8_t *)group->text, text,
+                      sizeof(group->text), encoding);
   }
 
   return (group);
@@ -2328,6 +2604,24 @@ ppd_get_option(ppd_group_t *group,       /* I - Group */
 }
 
 
+/*
+ * 'ppd_hash_option()' - Generate a hash of the option name...
+ */
+
+static int                             /* O - Hash index */
+ppd_hash_option(ppd_option_t *option)  /* I - Option */
+{
+  int          hash = 0;               /* Hash index */
+  const char   *k;                     /* Pointer into keyword */
+
+
+  for (hash = option->keyword[0], k = option->keyword + 1; *k;)
+    hash = 33 * hash + *k++;
+
+  return (hash & 511);
+}
+
+
 /*
  * 'ppd_read()' - Read a line from a PPD file, skipping comment lines as
  *                necessary.
@@ -2885,5 +3179,5 @@ ppd_read(cups_file_t    *fp,              /* I - File to read from */
 
 
 /*
- * End of "$Id: ppd.c 5200 2006-02-28 00:10:32Z mike $".
+ * End of "$Id: ppd.c 6479 2007-04-27 11:44:10Z mike $".
  */