]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/cups-driverd.cxx
Merge changes from CUPS 1.6svn-r9939.
[thirdparty/cups.git] / scheduler / cups-driverd.cxx
index f902d309f6f9f2ca326ed5cd06431ce77077defd..c8865dbde8aed2f62b57a54c9a393514fb1005b1 100644 (file)
@@ -1,13 +1,13 @@
 /*
  * "$Id$"
  *
- *   PPD/driver support for the Common UNIX Printing System (CUPS).
+ *   PPD/driver support for CUPS.
  *
- *   This program handles listing and installing both static PPD files
- *   in CUPS_DATADIR/model and dynamically generated PPD files using
- *   the driver helper programs in CUPS_SERVERBIN/driver.
+ *   This program handles listing and installing static PPD files, PPD files
+ *   created from driver information files, and dynamically generated PPD files
+ *   using driver helper programs.
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2011 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
  *   cat_drv()         - Generate a PPD from a driver info file.
  *   cat_ppd()         - Copy a PPD file to stdout.
  *   copy_static()     - Copy a static PPD file to stdout.
+ *   compare_inodes()  - Compare two inodes.
  *   compare_matches() - Compare PPD match scores for sorting.
  *   compare_names()   - Compare PPD filenames for sorting.
  *   compare_ppds()    - Compare PPD file make and model names for sorting.
+ *   dump_ppds_dat()   - Dump the contents of the ppds.dat file.
  *   free_array()      - Free an array of strings.
  *   list_ppds()       - List PPD files.
- *   load_ppds()       - Load PPD files recursively.
  *   load_drv()        - Load the PPDs from a driver information file.
  *   load_drivers()    - Load driver-generated PPD files.
+ *   load_ppds()       - Load PPD files recursively.
+ *   load_ppds_dat()   - Load the ppds.dat file.
  *   regex_device_id() - Compile a regular expression based on the 1284 device
  *                       ID.
  *   regex_string()    - Construct a regular expression to compare a simple
  * Constants...
  */
 
-#define PPD_SYNC       0x50504436      /* Sync word for ppds.dat (PPD6) */
+#define PPD_SYNC       0x50504437      /* Sync word for ppds.dat (PPD7) */
 #define PPD_MAX_LANG   32              /* Maximum languages */
-#define PPD_MAX_PROD                 /* Maximum products */
-#define PPD_MAX_VERS                 /* Maximum versions */
+#define PPD_MAX_PROD   32              /* Maximum products */
+#define PPD_MAX_VERS   32              /* Maximum versions */
 
 #define PPD_TYPE_POSTSCRIPT    0       /* PostScript PPD */
 #define PPD_TYPE_PDF           1       /* PDF PPD */
@@ -71,7 +74,8 @@ static const char * const ppd_types[] =       /* ppd-type values */
   "pdf",
   "raster",
   "fax",
-  "unknown"
+  "unknown",
+  "drv"
 };
 
 
@@ -97,7 +101,7 @@ typedef struct                               /**** PPD record ****/
                make_and_model[128],    /* NickName/ModelName */
                device_id[256],         /* IEEE 1284 Device ID */
                scheme[128];            /* PPD scheme */
-} ppd_rec_t; 
+} ppd_rec_t;
 
 typedef struct                         /**** In-memory record ****/
 {
@@ -111,7 +115,8 @@ typedef struct                              /**** In-memory record ****/
  * Globals...
  */
 
-cups_array_t   *PPDsByName = NULL,     /* PPD files sorted by filename and name */
+cups_array_t   *Inodes = NULL,         /* Inodes of directories we've visited */
+               *PPDsByName = NULL,     /* PPD files sorted by filename and name */
                *PPDsByMakeModel = NULL;/* PPD files sorted by make and model */
 int            ChangedPPD;             /* Did we change the PPD database? */
 
@@ -130,12 +135,14 @@ static ppd_info_t *add_ppd(const char *filename, const char *name,
 static int             cat_drv(const char *name, int request_id);
 static int             cat_ppd(const char *name, int request_id);
 static int             cat_static(const char *name, int request_id);
+static int             compare_inodes(struct stat *a, struct stat *b);
 static int             compare_matches(const ppd_info_t *p0,
                                        const ppd_info_t *p1);
 static int             compare_names(const ppd_info_t *p0,
                                      const ppd_info_t *p1);
 static int             compare_ppds(const ppd_info_t *p0,
                                     const ppd_info_t *p1);
+static int             dump_ppds_dat(void);
 static void            free_array(cups_array_t *a);
 static int             list_ppds(int request_id, int limit, const char *opt);
 static int             load_drivers(cups_array_t *include,
@@ -143,6 +150,8 @@ static int          load_drivers(cups_array_t *include,
 static int             load_drv(const char *filename, const char *name,
                                 cups_file_t *fp, time_t mtime, off_t size);
 static int             load_ppds(const char *d, const char *p, int descend);
+static void            load_ppds_dat(char *filename, size_t filesize,
+                                     int verbose);
 static regex_t         *regex_device_id(const char *device_id);
 static regex_t         *regex_string(const char *s);
 
@@ -165,6 +174,8 @@ main(int  argc,                             /* I - Number of command-line args */
 
   if (argc == 3 && !strcmp(argv[1], "cat"))
     return (cat_ppd(argv[2], 0));
+  else if (argc == 2 && !strcmp(argv[1], "dump"))
+    return (dump_ppds_dat());
   else if (argc == 4 && !strcmp(argv[1], "get"))
     return (cat_ppd(argv[3], atoi(argv[2])));
   else if (argc == 5 && !strcmp(argv[1], "list"))
@@ -172,6 +183,7 @@ main(int  argc,                             /* I - Number of command-line args */
   else
   {
     fputs("Usage: cups-driverd cat ppd-name\n", stderr);
+    fputs("Usage: cups-driverd dump\n", stderr);
     fputs("Usage: cups-driverd get request_id ppd-name\n", stderr);
     fputs("Usage: cups-driverd list request_id limit options\n", stderr);
     return (1);
@@ -272,7 +284,7 @@ cat_drv(const char *name,           /* I - PPD name */
   const char   *datadir;               // CUPS_DATADIR env var
   ppdcSource   *src;                   // PPD source file data
   ppdcDriver   *d;                     // Current driver
-  cups_file_t  *out;                   // Stdout via CUPS file API 
+  cups_file_t  *out;                   // Stdout via CUPS file API
   char         message[2048],          // status-message
                filename[1024],         // Full path to .drv file(s)
                scheme[32],             // URI scheme ("drv")
@@ -287,7 +299,7 @@ cat_drv(const char *name,           /* I - PPD name */
   if ((datadir = getenv("CUPS_DATADIR")) == NULL)
     datadir = CUPS_DATADIR;
 
-  // Pull out the 
+  // Pull out the path to the .drv file...
   if (httpSeparateURI(HTTP_URI_CODING_ALL, name, scheme, sizeof(scheme),
                       userpass, sizeof(userpass), host, sizeof(host), &port,
                      resource, sizeof(resource)) < HTTP_URI_OK ||
@@ -316,7 +328,8 @@ cat_drv(const char *name,           /* I - PPD name */
   *pc_file_name++ = '\0';
 
 #ifdef __APPLE__
-  if (!strncmp(resource, "/Library/Printers/PPDs.drv/", 27))
+  if (!strncmp(resource, "/Library/Printers/PPDs/Contents/Resources/", 42) ||
+      !strncmp(resource, "/System/Library/Printers/PPDs/Contents/Resources/", 49))
     strlcpy(filename, resource, sizeof(filename));
   else
 #endif // __APPLE__
@@ -331,8 +344,8 @@ cat_drv(const char *name,           /* I - PPD name */
   for (d = (ppdcDriver *)src->drivers->first();
        d;
        d = (ppdcDriver *)src->drivers->next())
-    if (!strcasecmp(pc_file_name, d->pc_file_name->value) ||
-        (d->file_name && !strcasecmp(pc_file_name, d->file_name->value)))
+    if (!strcmp(pc_file_name, d->pc_file_name->value) ||
+        (d->file_name && !strcmp(pc_file_name, d->file_name->value)))
       break;
 
   if (d)
@@ -341,7 +354,7 @@ cat_drv(const char *name,           /* I - PPD name */
     ppdcCatalog        *catalog;               // Message catalog in .drv file
 
 
-    fprintf(stderr, "DEBUG: [cups-driverd] %d locales defined in \"%s\"...\n",
+    fprintf(stderr, "DEBUG2: [cups-driverd] %d locales defined in \"%s\"...\n",
             src->po_files->count, filename);
 
     locales = new ppdcArray();
@@ -349,8 +362,9 @@ cat_drv(const char *name,           /* I - PPD name */
          catalog;
         catalog = (ppdcCatalog *)src->po_files->next())
     {
-      fprintf(stderr, "DEBUG: [cups-driverd] Adding locale \"%s\"...\n",
+      fprintf(stderr, "DEBUG2: [cups-driverd] Adding locale \"%s\"...\n",
               catalog->locale->value);
+      catalog->locale->retain();
       locales->add(catalog->locale);
     }
 
@@ -529,6 +543,10 @@ cat_static(const char *name,               /* I - PPD name */
   const char   *datadir;               /* CUPS_DATADIR env var */
   char         line[1024],             /* Line/filename */
                message[2048];          /* status-message */
+#ifdef __APPLE__
+  const char   *printerDriver,         /* Pointer to .printerDriver extension */
+               *slash;                 /* Pointer to next slash */
+#endif /* __APPLE__ */
 
 
   if (name[0] == '/' || strstr(name, "../") || strstr(name, "/.."))
@@ -561,7 +579,19 @@ cat_static(const char *name,               /* I - PPD name */
 
 #ifdef __APPLE__
   if (!strncmp(name, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
-      !strncmp(name, "Library/Printers/PPDs/Contents/Resources/", 41))
+      !strncmp(name, "Library/Printers/PPDs/Contents/Resources/", 41) ||
+      (!strncmp(name, "System/Library/Printers/", 24) &&
+       (printerDriver =
+           strstr(name + 24,
+                  ".printerDriver/Contents/Resources/PPDs")) != NULL &&
+       (slash = strchr(name + 24, '/')) != NULL &&
+       slash > printerDriver) ||
+      (!strncmp(name, "Library/Printers/", 17) &&
+       (printerDriver =
+           strstr(name + 17,
+                  ".printerDriver/Contents/Resources/PPDs")) != NULL &&
+       (slash = strchr(name + 17, '/')) != NULL &&
+       slash > printerDriver))
   {
    /*
     * Map ppd-name to Mac OS X standard locations...
@@ -651,6 +681,21 @@ cat_static(const char *name,               /* I - PPD name */
 }
 
 
+/*
+ * 'compare_inodes()' - Compare two inodes.
+ */
+
+static int                             /* O - Result of comparison */
+compare_inodes(struct stat *a,         /* I - First inode */
+               struct stat *b)         /* I - Second inode */
+{
+  if (a->st_dev != b->st_dev)
+    return (a->st_dev - b->st_dev);
+  else
+    return (a->st_ino - b->st_ino);
+}
+
+
 /*
  * 'compare_matches()' - Compare PPD match scores for sorting.
  */
@@ -678,10 +723,10 @@ compare_names(const ppd_info_t *p0,       /* I - First PPD file */
   int  diff;                           /* Difference between strings */
 
 
-  if ((diff = strcasecmp(p0->record.filename, p1->record.filename)) != 0)
+  if ((diff = strcmp(p0->record.filename, p1->record.filename)) != 0)
     return (diff);
   else
-    return (strcasecmp(p0->record.name, p1->record.name));
+    return (strcmp(p0->record.name, p1->record.name));
 }
 
 
@@ -700,14 +745,51 @@ compare_ppds(const ppd_info_t *p0,        /* I - First PPD file */
   * First compare manufacturers...
   */
 
-  if ((diff = strcasecmp(p0->record.make, p1->record.make)) != 0)
+  if ((diff = _cups_strcasecmp(p0->record.make, p1->record.make)) != 0)
     return (diff);
   else if ((diff = cupsdCompareNames(p0->record.make_and_model,
                                      p1->record.make_and_model)) != 0)
     return (diff);
+  else if ((diff = strcmp(p0->record.languages[0],
+                          p1->record.languages[0])) != 0)
+    return (diff);
   else
-    return (strcasecmp(p0->record.languages[0],
-                       p1->record.languages[0]));
+    return (compare_names(p0, p1));
+}
+
+
+/*
+ * 'dump_ppds_dat()' - Dump the contents of the ppds.dat file.
+ */
+
+static int                             /* O - Exit status */
+dump_ppds_dat(void)
+{
+  char         filename[1024];         /* ppds.dat filename */
+  ppd_info_t   *ppd;                   /* Current PPD */
+
+
+ /*
+  * See if we a PPD database file...
+  */
+
+  load_ppds_dat(filename, sizeof(filename), 0);
+
+  puts("mtime,size,model_number,type,filename,name,languages0,products0,"
+       "psversions0,make,make_and_model,device_id,scheme");
+  for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
+       ppd;
+       ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
+    printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
+           "\"%s\",\"%s\"\n",
+           (int)ppd->record.mtime, (long)ppd->record.size,
+          ppd->record.model_number, ppd->record.type, ppd->record.filename,
+          ppd->record.name, ppd->record.languages[0], ppd->record.products[0],
+          ppd->record.psversions[0], ppd->record.make,
+          ppd->record.make_and_model, ppd->record.device_id,
+          ppd->record.scheme);
+
+  return (0);
 }
 
 
@@ -743,10 +825,8 @@ list_ppds(int        request_id,   /* I - Request ID */
   int          count;                  /* Number of PPDs to send */
   ppd_info_t   *ppd;                   /* Current PPD file */
   cups_file_t  *fp;                    /* ppds.dat file */
-  struct stat  fileinfo;               /* ppds.dat information */
   char         filename[1024],         /* ppds.dat filename */
                model[1024];            /* Model directory */
-  const char   *cups_cachedir;         /* CUPS_CACHEDIR environment variable */
   const char   *cups_datadir;          /* CUPS_DATADIR environment variable */
   int          num_options;            /* Number of options */
   cups_option_t        *options;               /* Options */
@@ -789,64 +869,8 @@ list_ppds(int        request_id,   /* I - Request ID */
   * See if we a PPD database file...
   */
 
-  PPDsByName      = cupsArrayNew((cups_array_func_t)compare_names, NULL);
-  PPDsByMakeModel = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
-  ChangedPPD      = 0;
-
-  if ((cups_cachedir = getenv("CUPS_CACHEDIR")) == NULL)
-    cups_cachedir = CUPS_CACHEDIR;
-
-  snprintf(filename, sizeof(filename), "%s/ppds.dat", cups_cachedir);
-  if ((fp = cupsFileOpen(filename, "r")) != NULL)
-  {
-   /*
-    * See if we have the right sync word...
-    */
-
-    unsigned ppdsync;                  /* Sync word */
-    int      num_ppds;                 /* Number of PPDs */
-
-
-    if (cupsFileRead(fp, (char *)&ppdsync, sizeof(ppdsync))
-            == sizeof(ppdsync) &&
-        ppdsync == PPD_SYNC &&
-        !stat(filename, &fileinfo) &&
-       ((fileinfo.st_size - sizeof(ppdsync)) % sizeof(ppd_rec_t)) == 0 &&
-       (num_ppds = (fileinfo.st_size - sizeof(ppdsync)) /
-                   sizeof(ppd_rec_t)) > 0)
-    {
-     /*
-      * We have a ppds.dat file, so read it!
-      */
-
-      for (; num_ppds > 0; num_ppds --)
-      {
-       if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
-       {
-         fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
-               stderr);
-         exit(1);
-       }
-
-       if (cupsFileRead(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)) > 0)
-       {
-         cupsArrayAdd(PPDsByName, ppd);
-         cupsArrayAdd(PPDsByMakeModel, ppd);
-       }
-       else
-       {
-         free(ppd);
-         break;
-       }
-      }
-
-      fprintf(stderr, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
-             filename, cupsArrayCount(PPDsByName));
-    }
+  load_ppds_dat(filename, sizeof(filename), 1);
 
-    cupsFileClose(fp);
-  }
-  
  /*
   * Load all PPDs in the specified directory and below...
   */
@@ -854,6 +878,8 @@ list_ppds(int        request_id,    /* I - Request ID */
   if ((cups_datadir = getenv("CUPS_DATADIR")) == NULL)
     cups_datadir = CUPS_DATADIR;
 
+  Inodes = cupsArrayNew((cups_array_func_t)compare_inodes, NULL);
+
   snprintf(model, sizeof(model), "%s/model", cups_datadir);
   load_ppds(model, "", 1);
 
@@ -865,10 +891,14 @@ list_ppds(int        request_id,  /* I - Request ID */
   * Load PPDs from standard Mac OS X locations...
   */
 
+  load_ppds("/Library/Printers",
+            "Library/Printers", 0);
   load_ppds("/Library/Printers/PPDs/Contents/Resources",
             "Library/Printers/PPDs/Contents/Resources", 0);
   load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
             "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
+  load_ppds("/System/Library/Printers",
+            "System/Library/Printers", 0);
   load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
             "System/Library/Printers/PPDs/Contents/Resources", 0);
   load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
@@ -903,19 +933,26 @@ list_ppds(int        request_id,  /* I - Request ID */
       cupsArrayRemove(PPDsByName, ppd);
       cupsArrayRemove(PPDsByMakeModel, ppd);
       free(ppd);
+
+      ChangedPPD = 1;
     }
 
  /*
   * Write the new ppds.dat file...
   */
 
+  fprintf(stderr, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD);
+
   if (ChangedPPD)
   {
-    if ((fp = cupsFileOpen(filename, "w")) != NULL)
+    char       newname[1024];          /* New filename */
+
+    snprintf(newname, sizeof(newname), "%s.%d", filename, (int)getpid());
+
+    if ((fp = cupsFileOpen(newname, "w")) != NULL)
     {
       unsigned ppdsync = PPD_SYNC;     /* Sync word */
 
-
       cupsFileWrite(fp, (char *)&ppdsync, sizeof(ppdsync));
 
       for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
@@ -925,8 +962,12 @@ list_ppds(int        request_id,   /* I - Request ID */
 
       cupsFileClose(fp);
 
-      fprintf(stderr, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
-              filename, cupsArrayCount(PPDsByName));
+      if (rename(newname, filename))
+       fprintf(stderr, "ERROR: [cups-driverd] Unable to rename \"%s\" - %s\n",
+               newname, strerror(errno));
+      else
+       fprintf(stderr, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
+               filename, cupsArrayCount(PPDsByName));
     }
     else
       fprintf(stderr, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
@@ -942,7 +983,7 @@ list_ppds(int        request_id,    /* I - Request ID */
   num_options = cupsParseOptions(opt, 0, &options);
   exclude     = cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
                                                       num_options, options));
-  include     = cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
+  include     = cupsdCreateStringsArray(cupsGetOption("include-schemes",
                                                      num_options, options));
 
   load_drivers(include, exclude);
@@ -1004,7 +1045,7 @@ list_ppds(int        request_id,  /* I - Request ID */
     type = 0;
 
   for (i = 0; i < num_options; i ++)
-    fprintf(stderr, "DEBUG: [cups-driverd] %s=\"%s\"\n", options[i].name,
+    fprintf(stderr, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options[i].name,
             options[i].value);
 
   if (!requested || cupsArrayFind(requested, (void *)"all") != NULL)
@@ -1041,7 +1082,14 @@ list_ppds(int        request_id, /* I - Request ID */
                                           (void *)"ppd-type") != NULL;
   }
 
-  puts("Content-Type: application/ipp\n");
+ /*
+  * Send the content type header to the scheduler; request_id can only be
+  * 0 when run manually since the scheduler enforces the IPP requirement for
+  * a request ID from 1 to 2^31-1...
+  */
+
+  if (request_id > 0)
+    puts("Content-Type: application/ipp\n");
 
   sent_header = 0;
 
@@ -1051,7 +1099,7 @@ list_ppds(int        request_id,  /* I - Request ID */
     count = limit;
 
   if (device_id || language || make || make_and_model || model_number_str ||
-      product || include || exclude)
+      product)
   {
     matches = cupsArrayNew((cups_array_func_t)compare_matches, NULL);
 
@@ -1106,15 +1154,16 @@ list_ppds(int        request_id,        /* I - Request ID */
       if (language)
       {
        for (i = 0; i < PPD_MAX_LANG; i ++)
-         if (!ppd->record.languages[i][0] ||
-             !strcasecmp(ppd->record.languages[i], language))
+         if (!ppd->record.languages[i][0])
+           break;
+         else if (!strcmp(ppd->record.languages[i], language))
          {
            ppd->matches ++;
            break;
          }
       }
 
-      if (make && !strcasecmp(ppd->record.make, make))
+      if (make && !_cups_strcasecmp(ppd->record.make, make))
         ppd->matches ++;
 
       if (make_and_model_re &&
@@ -1140,8 +1189,9 @@ list_ppds(int        request_id,  /* I - Request ID */
       if (product)
       {
        for (i = 0; i < PPD_MAX_PROD; i ++)
-         if (!ppd->record.products[i][0] ||
-             !strcasecmp(ppd->record.products[i], product))
+         if (!ppd->record.products[i][0])
+           break;
+         else if (!_cups_strcasecmp(ppd->record.products[i], product))
          {
            ppd->matches += 3;
            break;
@@ -1151,8 +1201,9 @@ list_ppds(int        request_id,  /* I - Request ID */
       if (psversion)
       {
        for (i = 0; i < PPD_MAX_VERS; i ++)
-         if (!ppd->record.psversions[i][0] ||
-             !strcasecmp(ppd->record.psversions[i], psversion))
+         if (!ppd->record.psversions[i][0])
+           break;
+         else if (!_cups_strcasecmp(ppd->record.psversions[i], psversion))
          {
            ppd->matches ++;
            break;
@@ -1164,12 +1215,35 @@ list_ppds(int        request_id,        /* I - Request ID */
 
       if (ppd->matches)
       {
-        fprintf(stderr, "DEBUG: [cups-driverd] %s matches with score %d!\n",
+        fprintf(stderr, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
                ppd->record.name, ppd->matches);
         cupsArrayAdd(matches, ppd);
       }
     }
   }
+  else if (include || exclude)
+  {
+    matches = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
+
+    for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
+        ppd;
+        ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
+    {
+     /*
+      * Filter PPDs based on the include/exclude lists.
+      */
+
+      if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
+         ppd->record.type >= PPD_TYPE_DRV)
+       continue;
+
+      if (cupsArrayFind(exclude, ppd->record.scheme) ||
+          (include && !cupsArrayFind(include, ppd->record.scheme)))
+        continue;
+
+      cupsArrayAdd(matches, ppd);
+    }
+  }
   else
     matches = PPDsByMakeModel;
 
@@ -1200,7 +1274,7 @@ list_ppds(int        request_id,  /* I - Request ID */
                          "en-US");
     }
 
-    fprintf(stderr, "DEBUG: [cups-driverd] Sending %s (%s)...\n",
+    fprintf(stderr, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
            ppd->record.name, ppd->record.make_and_model);
 
     count --;
@@ -1271,7 +1345,7 @@ list_ppds(int        request_id,  /* I - Request ID */
                ppd = (ppd_info_t *)cupsArrayNext(matches);
           ppd;
           ppd = (ppd_info_t *)cupsArrayNext(matches))
-       if (strcasecmp(this_make, ppd->record.make))
+       if (_cups_strcasecmp(this_make, ppd->record.make))
          break;
 
       cupsArrayPrev(matches);
@@ -1293,104 +1367,513 @@ list_ppds(int        request_id,      /* I - Request ID */
 
 
 /*
- * 'load_ppds()' - Load PPD files recursively.
+ * 'load_drv()' - Load the PPDs from a driver information file.
  */
 
 static int                             /* O - 1 on success, 0 on failure */
-load_ppds(const char *d,               /* I - Actual directory */
-          const char *p,               /* I - Virtual path in name */
-         int        descend)           /* I - Descend into directories? */
+load_drv(const char  *filename,                /* I - Actual filename */
+         const char  *name,            /* I - Name to the rest of the world */
+         cups_file_t *fp,              /* I - File to read from */
+        time_t      mtime,             /* I - Mod time of driver info file */
+        off_t       size)              /* I - Size of driver info file */
 {
-  int          i;                      /* Looping var */
-  cups_file_t  *fp;                    /* Pointer to file */
-  cups_dir_t   *dir;                   /* Directory pointer */
-  cups_dentry_t        *dent;                  /* Directory entry */
-  char         filename[1024],         /* Name of PPD or directory */
-               line[256],              /* Line from backend */
-               *ptr,                   /* Pointer into name */
-               name[128],              /* Name of PPD file */
-               lang_version[64],       /* PPD LanguageVersion */
-               lang_encoding[64],      /* PPD LanguageEncoding */
-               country[64],            /* Country code */
-               manufacturer[256],      /* Manufacturer */
-               make_model[256],        /* Make and Model */
-               model_name[256],        /* ModelName */
-               nick_name[256],         /* NickName */
-               device_id[256],         /* 1284DeviceID */
-               product[256],           /* Product */
-               psversion[256],         /* PSVersion */
-               temp[512];              /* Temporary make and model */
-  int          model_number,           /* cupsModelNumber */
-               type;                   /* ppd-type */
-  cups_array_t *products,              /* Product array */
-               *psversions,            /* PSVersion array */
-               *cups_languages;        /* cupsLanguages array */
-  ppd_info_t   *ppd,                   /* New PPD file */
-               key;                    /* Search key */
-  int          new_ppd;                /* Is this a new PPD? */
-  struct                               /* LanguageVersion translation table */
-  {
-    const char *version,               /* LanguageVersion string */
-               *language;              /* Language code */
-  }            languages[] =
-  {
-    { "chinese",               "zh" },
-    { "czech",                 "cs" },
-    { "danish",                        "da" },
-    { "dutch",                 "nl" },
-    { "english",               "en" },
-    { "finnish",               "fi" },
-    { "french",                        "fr" },
-    { "german",                        "de" },
-    { "greek",                 "el" },
-    { "hungarian",             "hu" },
-    { "italian",               "it" },
-    { "japanese",              "ja" },
-    { "korean",                        "ko" },
-    { "norwegian",             "no" },
-    { "polish",                        "pl" },
-    { "portuguese",            "pt" },
-    { "russian",               "ru" },
-    { "simplified chinese",    "zh_CN" },
-    { "slovak",                        "sk" },
-    { "spanish",               "es" },
-    { "swedish",               "sv" },
-    { "traditional chinese",   "zh_TW" },
-    { "turkish",               "tr" }
-  };
+  ppdcSource   *src;                   // Driver information file
+  ppdcDriver   *d;                     // Current driver
+  ppdcAttr     *device_id,             // 1284DeviceID attribute
+               *product,               // Current product value
+               *ps_version,            // PSVersion attribute
+               *cups_fax,              // cupsFax attribute
+               *nick_name;             // NickName attribute
+  ppdcFilter   *filter;                // Current filter
+  ppd_info_t   *ppd;                   // Current PPD
+  int          products_found;         // Number of products found
+  char         uri[1024],              // Driver URI
+               make_model[1024];       // Make and model
+  int          type;                   // Driver type
 
 
-  fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d);
+ /*
+  * Load the driver info file...
+  */
 
-  if ((dir = cupsDirOpen(d)) == NULL)
-  {
-    if (errno != ENOENT)
-      fprintf(stderr,
-             "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
-             d, strerror(errno));
+  src = new ppdcSource(filename, fp);
 
+  if (src->drivers->count == 0)
+  {
+    fprintf(stderr,
+            "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
+           filename);
+    src->release();
     return (0);
   }
 
-  while ((dent = cupsDirRead(dir)) != NULL)
-  {
-   /*
-    * Skip files/directories starting with "."...
-    */
+ /*
+  * Add a dummy entry for the file...
+  */
 
-    if (dent->filename[0] == '.')
-      continue;
+  add_ppd(name, name, "", "", "", "", "", "", mtime, size, 0,
+          PPD_TYPE_DRV, "drv");
+  ChangedPPD = 1;
 
  /*
-    * See if this is a file...
-    */
+ /*
+  * Then the drivers in the file...
+  */
 
-    snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
+  for (d = (ppdcDriver *)src->drivers->first();
+       d;
+       d = (ppdcDriver *)src->drivers->next())
+  {
+    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0,
+                     "/%s/%s", name,
+                    d->file_name ? d->file_name->value :
+                                   d->pc_file_name->value);
 
-    if (p[0])
-      snprintf(name, sizeof(name), "%s/%s", p, dent->filename);
+    device_id  = d->find_attr("1284DeviceID", NULL);
+    ps_version = d->find_attr("PSVersion", NULL);
+    nick_name  = d->find_attr("NickName", NULL);
+
+    if (nick_name)
+      strlcpy(make_model, nick_name->value->value, sizeof(make_model));
+    else if (_cups_strncasecmp(d->model_name->value, d->manufacturer->value,
+                         strlen(d->manufacturer->value)))
+      snprintf(make_model, sizeof(make_model), "%s %s, %s",
+               d->manufacturer->value, d->model_name->value,
+              d->version->value);
     else
-      strlcpy(name, dent->filename, sizeof(name));
+      snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value,
+               d->version->value);
+
+    if ((cups_fax = d->find_attr("cupsFax", NULL)) != NULL &&
+        !_cups_strcasecmp(cups_fax->value->value, "true"))
+      type = PPD_TYPE_FAX;
+    else if (d->type == PPDC_DRIVER_PS)
+      type = PPD_TYPE_POSTSCRIPT;
+    else if (d->type != PPDC_DRIVER_CUSTOM)
+      type = PPD_TYPE_RASTER;
+    else
+    {
+      for (filter = (ppdcFilter *)d->filters->first(),
+               type = PPD_TYPE_POSTSCRIPT;
+          filter;
+          filter = (ppdcFilter *)d->filters->next())
+        if (_cups_strcasecmp(filter->mime_type->value, "application/vnd.cups-raster"))
+         type = PPD_TYPE_RASTER;
+        else if (_cups_strcasecmp(filter->mime_type->value,
+                           "application/vnd.cups-pdf"))
+         type = PPD_TYPE_PDF;
+    }
+
+    for (product = (ppdcAttr *)d->attrs->first(), products_found = 0,
+             ppd = NULL;
+         product;
+        product = (ppdcAttr *)d->attrs->next())
+      if (!strcmp(product->name->value, "Product"))
+      {
+        if (!products_found)
+         ppd = add_ppd(name, uri, "en", d->manufacturer->value, make_model,
+                       device_id ? device_id->value->value : "",
+                       product->value->value,
+                       ps_version ? ps_version->value->value : "(3010) 0",
+                       mtime, size, d->model_number, type, "drv");
+       else if (products_found < PPD_MAX_PROD)
+         strlcpy(ppd->record.products[products_found], product->value->value,
+                 sizeof(ppd->record.products[0]));
+       else
+         break;
+
+       products_found ++;
+      }
+
+    if (!products_found)
+      add_ppd(name, uri, "en", d->manufacturer->value, make_model,
+             device_id ? device_id->value->value : "",
+             d->model_name->value,
+             ps_version ? ps_version->value->value : "(3010) 0",
+             mtime, size, d->model_number, type, "drv");
+  }
+
+  src->release();
+
+  return (1);
+}
+
+
+/*
+ * 'load_drivers()' - Load driver-generated PPD files.
+ */
+
+static int                             /* O - 1 on success, 0 on failure */
+load_drivers(cups_array_t *include,    /* I - Drivers to include */
+             cups_array_t *exclude)    /* I - Drivers to exclude */
+{
+  int          i;                      /* Looping var */
+  char         *start,                 /* Start of value */
+               *ptr;                   /* Pointer into string */
+  const char   *server_bin,            /* CUPS_SERVERBIN env variable */
+               *scheme,                /* Scheme for this driver */
+               *scheme_end;            /* Pointer to end of scheme */
+  char         drivers[1024];          /* Location of driver programs */
+  int          pid;                    /* Process ID for driver program */
+  cups_file_t  *fp;                    /* Pipe to driver program */
+  cups_dir_t   *dir;                   /* Directory pointer */
+  cups_dentry_t *dent;                 /* Directory entry */
+  char         *argv[3],               /* Arguments for command */
+               filename[1024],         /* Name of driver */
+               line[2048],             /* Line from driver */
+               name[512],              /* ppd-name */
+               make[128],              /* ppd-make */
+               make_and_model[128],    /* ppd-make-and-model */
+               device_id[256],         /* ppd-device-id */
+               languages[128],         /* ppd-natural-language */
+               product[128],           /* ppd-product */
+               psversion[128],         /* ppd-psversion */
+               type_str[128];          /* ppd-type */
+  int          type;                   /* PPD type */
+  ppd_info_t   *ppd;                   /* Newly added PPD */
+
+
+ /*
+  * Try opening the driver directory...
+  */
+
+  if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
+    server_bin = CUPS_SERVERBIN;
+
+  snprintf(drivers, sizeof(drivers), "%s/driver", server_bin);
+
+  if ((dir = cupsDirOpen(drivers)) == NULL)
+  {
+    fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory "
+                   "\"%s\": %s\n",
+           drivers, strerror(errno));
+    return (0);
+  }
+
+ /*
+  * Loop through all of the device drivers...
+  */
+
+  argv[1] = (char *)"list";
+  argv[2] = NULL;
+
+  while ((dent = cupsDirRead(dir)) != NULL)
+  {
+   /*
+    * Only look at executable files...
+    */
+
+    if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode))
+      continue;
+
+   /*
+    * Include/exclude specific drivers...
+    */
+
+    if (exclude)
+    {
+     /*
+      * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
+      */
+
+      for (scheme = (char *)cupsArrayFirst(exclude);
+          scheme;
+          scheme = (char *)cupsArrayNext(exclude))
+      {
+        fprintf(stderr, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
+               dent->filename, scheme);
+       scheme_end = scheme + strlen(scheme) - 1;
+
+       if ((scheme_end > scheme && *scheme_end == '*' &&
+            !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
+           !strcmp(scheme, dent->filename))
+       {
+         fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr);
+         break;
+       }
+      }
+
+      if (scheme)
+        continue;
+    }
+
+    if (include)
+    {
+     /*
+      * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
+      */
+
+      for (scheme = (char *)cupsArrayFirst(include);
+          scheme;
+          scheme = (char *)cupsArrayNext(include))
+      {
+        fprintf(stderr, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
+               dent->filename, scheme);
+       scheme_end = scheme + strlen(scheme) - 1;
+
+       if ((scheme_end > scheme && *scheme_end == '*' &&
+            !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
+           !strcmp(scheme, dent->filename))
+       {
+         fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr);
+         break;
+       }
+      }
+
+      if (!scheme)
+        continue;
+    }
+    else
+      scheme = dent->filename;
+
+   /*
+    * Run the driver with no arguments and collect the output...
+    */
+
+    snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename);
+
+    if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_PROGRAM, !geteuid(),
+                       _cupsFileCheckFilter, NULL))
+      continue;
+
+    argv[0] = dent->filename;
+
+    if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL)
+    {
+      while (cupsFileGets(fp, line, sizeof(line)))
+      {
+       /*
+        * Each line is of the form:
+       *
+       *   "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
+       *       "ppd-device-id" "ppd-product" "ppd-psversion"
+       */
+
+        device_id[0] = '\0';
+       product[0]   = '\0';
+       psversion[0] = '\0';
+       strcpy(type_str, "postscript");
+
+        if (sscanf(line, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
+                        "%*[ \t]\"%127[^\"]\"%*[ \t]\"%255[^\"]\""
+                        "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
+                        "%*[ \t]\"%127[^\"]\"",
+                  name, languages, make, make_and_model,
+                  device_id, product, psversion, type_str) < 4)
+        {
+        /*
+         * Bad format; strip trailing newline and write an error message.
+         */
+
+          if (line[strlen(line) - 1] == '\n')
+           line[strlen(line) - 1] = '\0';
+
+         fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
+                 dent->filename, line);
+         break;
+        }
+       else
+       {
+        /*
+         * Add the device to the array of available devices...
+         */
+
+          if ((start = strchr(languages, ',')) != NULL)
+           *start++ = '\0';
+
+         for (type = 0;
+               type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
+              type ++)
+           if (!strcmp(type_str, ppd_types[type]))
+              break;
+
+         if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
+         {
+           fprintf(stderr,
+                   "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
+                   type_str);
+           type = PPD_TYPE_UNKNOWN;
+         }
+
+          ppd = add_ppd(filename, name, languages, make, make_and_model,
+                        device_id, product, psversion, 0, 0, 0, type, scheme);
+
+          if (!ppd)
+         {
+            cupsDirClose(dir);
+           cupsFileClose(fp);
+           return (0);
+         }
+
+          if (start && *start)
+         {
+           for (i = 1; i < PPD_MAX_LANG && *start; i ++)
+           {
+             if ((ptr = strchr(start, ',')) != NULL)
+               *ptr++ = '\0';
+             else
+               ptr = start + strlen(start);
+
+              strlcpy(ppd->record.languages[i], start,
+                     sizeof(ppd->record.languages[0]));
+
+             start = ptr;
+           }
+          }
+
+          fprintf(stderr, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
+                 name);
+       }
+      }
+
+      cupsFileClose(fp);
+    }
+    else
+      fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
+              filename, strerror(errno));
+  }
+
+  cupsDirClose(dir);
+
+  return (1);
+}
+
+
+/*
+ * 'load_ppds()' - Load PPD files recursively.
+ */
+
+static int                             /* O - 1 on success, 0 on failure */
+load_ppds(const char *d,               /* I - Actual directory */
+          const char *p,               /* I - Virtual path in name */
+         int        descend)           /* I - Descend into directories? */
+{
+  struct stat  dinfo,                  /* Directory information */
+               *dinfoptr;              /* Pointer to match */
+  int          i;                      /* Looping var */
+  cups_file_t  *fp;                    /* Pointer to file */
+  cups_dir_t   *dir;                   /* Directory pointer */
+  cups_dentry_t        *dent;                  /* Directory entry */
+  char         filename[1024],         /* Name of PPD or directory */
+               line[256],              /* Line from backend */
+               *ptr,                   /* Pointer into name */
+               name[128],              /* Name of PPD file */
+               lang_version[64],       /* PPD LanguageVersion */
+               lang_encoding[64],      /* PPD LanguageEncoding */
+               country[64],            /* Country code */
+               manufacturer[256],      /* Manufacturer */
+               make_model[256],        /* Make and Model */
+               model_name[256],        /* ModelName */
+               nick_name[256],         /* NickName */
+               device_id[256],         /* 1284DeviceID */
+               product[256],           /* Product */
+               psversion[256],         /* PSVersion */
+               temp[512];              /* Temporary make and model */
+  int          model_number,           /* cupsModelNumber */
+               type;                   /* ppd-type */
+  cups_array_t *products,              /* Product array */
+               *psversions,            /* PSVersion array */
+               *cups_languages;        /* cupsLanguages array */
+  ppd_info_t   *ppd,                   /* New PPD file */
+               key;                    /* Search key */
+  int          new_ppd;                /* Is this a new PPD? */
+  struct                               /* LanguageVersion translation table */
+  {
+    const char *version,               /* LanguageVersion string */
+               *language;              /* Language code */
+  }            languages[] =
+  {
+    { "chinese",               "zh" },
+    { "czech",                 "cs" },
+    { "danish",                        "da" },
+    { "dutch",                 "nl" },
+    { "english",               "en" },
+    { "finnish",               "fi" },
+    { "french",                        "fr" },
+    { "german",                        "de" },
+    { "greek",                 "el" },
+    { "hungarian",             "hu" },
+    { "italian",               "it" },
+    { "japanese",              "ja" },
+    { "korean",                        "ko" },
+    { "norwegian",             "no" },
+    { "polish",                        "pl" },
+    { "portuguese",            "pt" },
+    { "russian",               "ru" },
+    { "simplified chinese",    "zh_CN" },
+    { "slovak",                        "sk" },
+    { "spanish",               "es" },
+    { "swedish",               "sv" },
+    { "traditional chinese",   "zh_TW" },
+    { "turkish",               "tr" }
+  };
+
+
+ /*
+  * See if we've loaded this directory before...
+  */
+
+  if (stat(d, &dinfo))
+  {
+    if (errno != ENOENT)
+      fprintf(stderr, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d,
+             strerror(errno));
+
+    return (0);
+  }
+  else if (cupsArrayFind(Inodes, &dinfo))
+  {
+    fprintf(stderr, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
+            d);
+    return (0);
+  }
+
+ /*
+  * Nope, add it to the Inodes array and continue...
+  */
+
+  dinfoptr = (struct stat *)malloc(sizeof(struct stat));
+  memcpy(dinfoptr, &dinfo, sizeof(struct stat));
+  cupsArrayAdd(Inodes, dinfoptr);
+
+ /*
+  * Check permissions...
+  */
+
+  if (_cupsFileCheck(d, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
+                    _cupsFileCheckFilter, NULL))
+    return (0);
+
+  if ((dir = cupsDirOpen(d)) == NULL)
+  {
+    if (errno != ENOENT)
+      fprintf(stderr,
+             "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
+             d, strerror(errno));
+
+    return (0);
+  }
+
+  fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d);
+
+  while ((dent = cupsDirRead(dir)) != NULL)
+  {
+   /*
+    * Skip files/directories starting with "."...
+    */
+
+    if (dent->filename[0] == '.')
+      continue;
+
+   /*
+    * See if this is a file...
+    */
+
+    snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
+
+    if (p[0])
+      snprintf(name, sizeof(name), "%s/%s", p, dent->filename);
+    else
+      strlcpy(name, dent->filename, sizeof(name));
 
     if (S_ISDIR(dent->fileinfo.st_mode))
     {
@@ -1399,14 +1882,44 @@ load_ppds(const char *d,                /* I - Actual directory */
       */
 
       if (descend)
+      {
        if (!load_ppds(filename, name, 1))
        {
          cupsDirClose(dir);
          return (1);
        }
+      }
+      else if ((ptr = filename + strlen(filename) - 14) > filename &&
+              !strcmp(ptr, ".printerDriver"))
+      {
+       /*
+        * Load PPDs in a printer driver bundle.
+       */
+
+       if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
+                          _cupsFileCheckFilter, NULL))
+         continue;
+
+       strlcat(filename, "/Contents/Resources/PPDs", sizeof(filename));
+       strlcat(name, "/Contents/Resources/PPDs", sizeof(name));
+
+       load_ppds(filename, name, 0);
+      }
+
+      continue;
+    }
+    else if ((ptr = filename + strlen(filename) - 6) > filename &&
+             !strcmp(ptr, ".plist"))
+    {
+     /*
+      * Skip plist files in the PPDs directory...
+      */
 
       continue;
     }
+    else if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_FILE_ONLY, !geteuid(),
+                           _cupsFileCheckFilter, NULL))
+      continue;
 
    /*
     * See if this file has been scanned before...
@@ -1421,12 +1934,20 @@ load_ppds(const char *d,                /* I - Actual directory */
        ppd->record.size == dent->fileinfo.st_size &&
        ppd->record.mtime == dent->fileinfo.st_mtime)
     {
-      do
-      {
-        ppd->found = 1;
-      }
+     /*
+      * Rewind to the first entry for this file...
+      */
+
+      while ((ppd = (ppd_info_t *)cupsArrayPrev(PPDsByName)) != NULL &&
+            !strcmp(ppd->record.filename, name));
+
+     /*
+      * Then mark all of the matches for this file as found...
+      */
+
       while ((ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) != NULL &&
-            !strcasecmp(ppd->record.filename, name));
+            !strcmp(ppd->record.filename, name))
+        ppd->found = 1;
 
       continue;
     }
@@ -1485,7 +2006,7 @@ load_ppds(const char *d,          /* I - Actual directory */
        sscanf(line, "%*[^:]:%63s", lang_version);
       else if (!strncmp(line, "*NickName:", 10))
        sscanf(line, "%*[^\"]\"%255[^\"]", nick_name);
-      else if (!strncasecmp(line, "*1284DeviceID:", 14))
+      else if (!_cups_strncasecmp(line, "*1284DeviceID:", 14))
       {
        sscanf(line, "%*[^\"]\"%255[^\"]", device_id);
 
@@ -1495,8 +2016,26 @@ load_ppds(const char *d,         /* I - Actual directory */
       }
       else if (!strncmp(line, "*Product:", 9))
       {
-       if (sscanf(line, "%*[^\"]\"(%255[^)]", product) == 1)
-         cupsArrayAdd(products, strdup(product));
+       if (sscanf(line, "%*[^\"]\"(%255[^\"]", product) == 1)
+       {
+        /*
+         * Make sure the value ends with a right parenthesis - can't stop at
+         * the first right paren since the product name may contain escaped
+         * parenthesis...
+         */
+
+         ptr = product + strlen(product) - 1;
+         if (ptr > product && *ptr == ')')
+         {
+          /*
+           * Yes, ends with a parenthesis, so remove it from the end and
+           * add the product to the list...
+           */
+
+           *ptr = '\0';
+           cupsArrayAdd(products, strdup(product));
+         }
+       }
       }
       else if (!strncmp(line, "*PSVersion:", 11))
       {
@@ -1535,7 +2074,7 @@ load_ppds(const char *d,          /* I - Actual directory */
       {
         for (ptr = line + 9; isspace(*ptr & 255); ptr ++);
 
-       if (!strncasecmp(ptr, "true", 4))
+       if (!_cups_strncasecmp(ptr, "true", 4))
           type = PPD_TYPE_FAX;
       }
       else if (!strncmp(line, "*cupsFilter:", 12) && type == PPD_TYPE_POSTSCRIPT)
@@ -1613,7 +2152,7 @@ load_ppds(const char *d,          /* I - Actual directory */
     while (isspace(manufacturer[0] & 255))
       _cups_strcpy(manufacturer, manufacturer + 1);
 
-    if (!strncasecmp(make_model, manufacturer, strlen(manufacturer)))
+    if (!_cups_strncasecmp(make_model, manufacturer, strlen(manufacturer)))
       strlcpy(temp, make_model, sizeof(temp));
     else
       snprintf(temp, sizeof(temp), "%s %s", manufacturer, make_model);
@@ -1646,10 +2185,10 @@ load_ppds(const char *d,                /* I - Actual directory */
       else
        strcpy(manufacturer, "Other");
     }
-    else if (!strncasecmp(manufacturer, "LHAG", 4) ||
-             !strncasecmp(manufacturer, "linotype", 8))
+    else if (!_cups_strncasecmp(manufacturer, "LHAG", 4) ||
+             !_cups_strncasecmp(manufacturer, "linotype", 8))
       strcpy(manufacturer, "LHAG");
-    else if (!strncasecmp(manufacturer, "Hewlett", 7))
+    else if (!_cups_strncasecmp(manufacturer, "Hewlett", 7))
       strcpy(manufacturer, "HP");
 
    /*
@@ -1680,7 +2219,7 @@ load_ppds(const char *d,          /* I - Actual directory */
     }
 
     for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++)
-      if (!strcasecmp(languages[i].version, lang_version))
+      if (!_cups_strcasecmp(languages[i].version, lang_version))
         break;
 
     if (i < (int)(sizeof(languages) / sizeof(languages[0])))
@@ -1705,397 +2244,169 @@ load_ppds(const char *d,              /* I - Actual directory */
     * Record the PPD file...
     */
 
-    new_ppd = !ppd;
-
-    if (new_ppd)
-    {
-     /*
-      * Add new PPD file...
-      */
-
-      fprintf(stderr, "DEBUG: [cups-driverd] Adding ppd \"%s\"...\n", name);
-
-      ppd = add_ppd(name, name, lang_version, manufacturer, make_model,
-                    device_id, (char *)cupsArrayFirst(products),
-                    (char *)cupsArrayFirst(psversions),
-                    dent->fileinfo.st_mtime, dent->fileinfo.st_size,
-                   model_number, type, "file");
-
-      if (!ppd)
-      {
-        cupsDirClose(dir);
-       return (0);
-      }
-    }
-    else
-    {
-     /*
-      * Update existing record...
-      */
-
-      fprintf(stderr, "DEBUG: [cups-driverd] Updating ppd \"%s\"...\n", name);
-
-      memset(ppd, 0, sizeof(ppd_info_t));
-
-      ppd->found               = 1;
-      ppd->record.mtime        = dent->fileinfo.st_mtime;
-      ppd->record.size         = dent->fileinfo.st_size;
-      ppd->record.model_number = model_number;
-      ppd->record.type         = type;
-
-      strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
-      strlcpy(ppd->record.make, manufacturer, sizeof(ppd->record.make));
-      strlcpy(ppd->record.make_and_model, make_model,
-              sizeof(ppd->record.make_and_model));
-      strlcpy(ppd->record.languages[0], lang_version,
-              sizeof(ppd->record.languages[0]));
-      strlcpy(ppd->record.products[0], (char *)cupsArrayFirst(products),
-              sizeof(ppd->record.products[0]));
-      strlcpy(ppd->record.psversions[0], (char *)cupsArrayFirst(psversions),
-              sizeof(ppd->record.psversions[0]));
-      strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
-    }
-
-   /*
-    * Add remaining products, versions, and languages...
-    */
-
-    for (i = 1;
-         i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL;
-        i ++)
-      strlcpy(ppd->record.products[i], ptr,
-              sizeof(ppd->record.products[0]));
-
-    for (i = 1;
-         i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL;
-        i ++)
-      strlcpy(ppd->record.psversions[i], ptr,
-              sizeof(ppd->record.psversions[0]));
-
-    for (i = 1, ptr = (char *)cupsArrayFirst(cups_languages);
-         i < PPD_MAX_LANG && ptr;
-        i ++, ptr = (char *)cupsArrayNext(cups_languages))
-      strlcpy(ppd->record.languages[i], ptr,
-              sizeof(ppd->record.languages[0]));
-
-   /*
-    * Free products, versions, and languages...
-    */
-
-    free_array(cups_languages);
-    free_array(products);
-    free_array(psversions);
-
-    ChangedPPD = 1;
-  }
-
-  cupsDirClose(dir);
-
-  return (1);
-}
-
-
-/*
- * 'load_drv()' - Load the PPDs from a driver information file.
- */
-
-static int                             /* O - 1 on success, 0 on failure */
-load_drv(const char  *filename,                /* I - Actual filename */
-         const char  *name,            /* I - Name to the rest of the world */
-         cups_file_t *fp,              /* I - File to read from */
-        time_t      mtime,             /* I - Mod time of driver info file */
-        off_t       size)              /* I - Size of driver info file */
-{
-  ppdcSource   *src;                   // Driver information file
-  ppdcDriver   *d;                     // Current driver
-  ppdcAttr     *device_id,             // 1284DeviceID attribute
-               *product,               // Current product value
-               *ps_version,            // PSVersion attribute
-               *cups_fax,              // cupsFax attribute
-               *nick_name;             // NickName attribute
-  ppdcFilter   *filter;                // Current filter
-  bool         product_found;          // Found product?
-  char         uri[1024],              // Driver URI
-               make_model[1024];       // Make and model
-  int          type;                   // Driver type
-
-
- /*
-  * Load the driver info file...
-  */
-
-  src = new ppdcSource(filename, fp);
-
-  if (src->drivers->count == 0)
-  {
-    fprintf(stderr,
-            "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
-           filename);
-    src->release();
-    return (0);
-  }
-
- /*
-  * Add a dummy entry for the file...
-  */
-
-  add_ppd(filename, filename, "", "", "", "", "", "", mtime, size, 0,
-          PPD_TYPE_DRV, "drv");
-
- /*
-  * Then the drivers in the file...
-  */
-
-  for (d = (ppdcDriver *)src->drivers->first();
-       d;
-       d = (ppdcDriver *)src->drivers->next())
-  {
-    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0,
-                     "/%s/%s", name,
-                    d->file_name ? d->file_name->value :
-                                   d->pc_file_name->value);
-
-    device_id  = d->find_attr("1284DeviceID", NULL);
-    ps_version = d->find_attr("PSVersion", NULL);
-    nick_name  = d->find_attr("NickName", NULL);
-
-    if (nick_name)
-      strlcpy(make_model, nick_name->value->value, sizeof(make_model));
-    else if (strncasecmp(d->model_name->value, d->manufacturer->value,
-                         strlen(d->manufacturer->value)))
-      snprintf(make_model, sizeof(make_model), "%s %s, %s",
-               d->manufacturer->value, d->model_name->value,
-              d->version->value);
-    else
-      snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value,
-               d->version->value);
-
-    if ((cups_fax = d->find_attr("cupsFax", NULL)) != NULL &&
-        !strcasecmp(cups_fax->value->value, "true"))
-      type = PPD_TYPE_FAX;
-    else if (d->type == PPDC_DRIVER_PS)
-      type = PPD_TYPE_POSTSCRIPT;
-    else if (d->type != PPDC_DRIVER_CUSTOM)
-      type = PPD_TYPE_RASTER;
-    else
-    {
-      for (filter = (ppdcFilter *)d->filters->first(),
-               type = PPD_TYPE_POSTSCRIPT;
-          filter;
-          filter = (ppdcFilter *)d->filters->next())
-        if (strcasecmp(filter->mime_type->value, "application/vnd.cups-raster"))
-         type = PPD_TYPE_RASTER;
-        else if (strcasecmp(filter->mime_type->value,
-                           "application/vnd.cups-pdf"))
-         type = PPD_TYPE_PDF;
-    }
-
-    for (product = (ppdcAttr *)d->attrs->first(), product_found = false;
-         product;
-        product = (ppdcAttr *)d->attrs->next())
-      if (!strcmp(product->name->value, "Product"))
-      {
-        product_found = true;
-
-       add_ppd(filename, uri, "en", d->manufacturer->value, make_model,
-               device_id ? device_id->value->value : "",
-               product->value->value,
-               ps_version ? ps_version->value->value : "(3010) 0",
-               mtime, size, d->model_number, type, "drv");
-      }
-
-    if (!product_found)
-      add_ppd(filename, uri, "en", d->manufacturer->value, make_model,
-             device_id ? device_id->value->value : "",
-             d->model_name->value,
-             ps_version ? ps_version->value->value : "(3010) 0",
-             mtime, size, d->model_number, type, "drv");
-  }
-
-  src->release();
-
-  return (1);
-}
-
-
-/*
- * 'load_drivers()' - Load driver-generated PPD files.
- */
-
-static int                             /* O - 1 on success, 0 on failure */
-load_drivers(cups_array_t *include,    /* I - Drivers to include */
-             cups_array_t *exclude)    /* I - Drivers to exclude */
-{
-  int          i;                      /* Looping var */
-  char         *start,                 /* Start of value */
-               *ptr;                   /* Pointer into string */
-  const char   *server_bin;            /* CUPS_SERVERBIN env variable */
-  char         drivers[1024];          /* Location of driver programs */
-  int          pid;                    /* Process ID for driver program */
-  cups_file_t  *fp;                    /* Pipe to driver program */
-  cups_dir_t   *dir;                   /* Directory pointer */
-  cups_dentry_t *dent;                 /* Directory entry */
-  char         *argv[3],               /* Arguments for command */
-               filename[1024],         /* Name of driver */
-               line[2048],             /* Line from driver */
-               name[512],              /* ppd-name */
-               make[128],              /* ppd-make */
-               make_and_model[128],    /* ppd-make-and-model */
-               device_id[128],         /* ppd-device-id */
-               languages[128],         /* ppd-natural-language */
-               product[128],           /* ppd-product */
-               psversion[128],         /* ppd-psversion */
-               type_str[128];          /* ppd-type */
-  int          type;                   /* PPD type */
-  ppd_info_t   *ppd;                   /* Newly added PPD */
+    new_ppd = !ppd;
 
+    if (new_ppd)
+    {
+     /*
+      * Add new PPD file...
+      */
 
- /*
-  * Try opening the driver directory...
-  */
+      fprintf(stderr, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name);
 
-  if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
-    server_bin = CUPS_SERVERBIN;
+      ppd = add_ppd(name, name, lang_version, manufacturer, make_model,
+                    device_id, (char *)cupsArrayFirst(products),
+                    (char *)cupsArrayFirst(psversions),
+                    dent->fileinfo.st_mtime, dent->fileinfo.st_size,
+                   model_number, type, "file");
 
-  snprintf(drivers, sizeof(drivers), "%s/driver", server_bin);
+      if (!ppd)
+      {
+        cupsDirClose(dir);
+       return (0);
+      }
+    }
+    else
+    {
+     /*
+      * Update existing record...
+      */
 
-  if ((dir = cupsDirOpen(drivers)) == NULL)
-  {
-    fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory "
-                   "\"%s\": %s\n",
-           drivers, strerror(errno));
-    return (0);
-  }
+      fprintf(stderr, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name);
 
- /*
-  * Loop through all of the device drivers...
-  */
+      memset(ppd, 0, sizeof(ppd_info_t));
 
-  argv[1] = (char *)"list";
-  argv[2] = NULL;
+      ppd->found               = 1;
+      ppd->record.mtime        = dent->fileinfo.st_mtime;
+      ppd->record.size         = dent->fileinfo.st_size;
+      ppd->record.model_number = model_number;
+      ppd->record.type         = type;
+
+      strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
+      strlcpy(ppd->record.make, manufacturer, sizeof(ppd->record.make));
+      strlcpy(ppd->record.make_and_model, make_model,
+              sizeof(ppd->record.make_and_model));
+      strlcpy(ppd->record.languages[0], lang_version,
+              sizeof(ppd->record.languages[0]));
+      strlcpy(ppd->record.products[0], (char *)cupsArrayFirst(products),
+              sizeof(ppd->record.products[0]));
+      strlcpy(ppd->record.psversions[0], (char *)cupsArrayFirst(psversions),
+              sizeof(ppd->record.psversions[0]));
+      strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
+    }
 
-  while ((dent = cupsDirRead(dir)) != NULL)
-  {
    /*
-    * Only look at executable files...
+    * Add remaining products, versions, and languages...
     */
 
-    if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode))
-      continue;
+    for (i = 1;
+         i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL;
+        i ++)
+      strlcpy(ppd->record.products[i], ptr,
+              sizeof(ppd->record.products[0]));
 
-   /*
-    * Include/exclude specific drivers...
-    */
+    for (i = 1;
+         i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL;
+        i ++)
+      strlcpy(ppd->record.psversions[i], ptr,
+              sizeof(ppd->record.psversions[0]));
 
-    if (cupsArrayFind(exclude, dent->filename) ||
-       (include && !cupsArrayFind(include, dent->filename)))
-      continue;
+    for (i = 1, ptr = (char *)cupsArrayFirst(cups_languages);
+         i < PPD_MAX_LANG && ptr;
+        i ++, ptr = (char *)cupsArrayNext(cups_languages))
+      strlcpy(ppd->record.languages[i], ptr,
+              sizeof(ppd->record.languages[0]));
 
    /*
-    * Run the driver with no arguments and collect the output...
+    * Free products, versions, and languages...
     */
 
-    argv[0] = dent->filename;
-    snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename);
+    free_array(cups_languages);
+    free_array(products);
+    free_array(psversions);
 
-    if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL)
-    {
-      while (cupsFileGets(fp, line, sizeof(line)))
-      {
-       /*
-        * Each line is of the form:
-       *
-       *   "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
-       *       "ppd-device-id" "ppd-product" "ppd-psversion"
-       */
+    ChangedPPD = 1;
+  }
 
-        device_id[0] = '\0';
-       product[0]   = '\0';
-       psversion[0] = '\0';
-       strcpy(type_str, "postscript");
+  cupsDirClose(dir);
 
-        if (sscanf(line, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
-                        "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
-                        "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
-                        "%*[ \t]\"%127[^\"]\"",
-                  name, languages, make, make_and_model,
-                  device_id, product, psversion, type_str) < 4)
-        {
-        /*
-         * Bad format; strip trailing newline and write an error message.
-         */
+  return (1);
+}
 
-          if (line[strlen(line) - 1] == '\n')
-           line[strlen(line) - 1] = '\0';
 
-         fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
-                 dent->filename, line);
-         break;
-        }
-       else
-       {
-        /*
-         * Add the device to the array of available devices...
-         */
+/*
+ * 'load_ppds_dat()' - Load the ppds.dat file.
+ */
 
-          if ((start = strchr(languages, ',')) != NULL)
-           *start++ = '\0';
+static void
+load_ppds_dat(char   *filename,                /* I - Filename buffer */
+              size_t filesize,         /* I - Size of filename buffer */
+              int    verbose)          /* I - Be verbose? */
+{
+  ppd_info_t   *ppd;                   /* Current PPD file */
+  cups_file_t  *fp;                    /* ppds.dat file */
+  struct stat  fileinfo;               /* ppds.dat information */
+  const char   *cups_cachedir;         /* CUPS_CACHEDIR environment variable */
 
-         for (type = 0;
-               type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
-              type ++)
-           if (!strcmp(type_str, ppd_types[type]))
-              break;
 
-         if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
-         {
-           fprintf(stderr,
-                   "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
-                   type_str);
-           type = PPD_TYPE_UNKNOWN;
-         }
+  PPDsByName      = cupsArrayNew((cups_array_func_t)compare_names, NULL);
+  PPDsByMakeModel = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
+  ChangedPPD      = 0;
 
-          ppd = add_ppd("", name, languages, make, make_and_model, device_id,
-                       product, psversion, 0, 0, 0, type, dent->filename);
+  if ((cups_cachedir = getenv("CUPS_CACHEDIR")) == NULL)
+    cups_cachedir = CUPS_CACHEDIR;
 
-          if (!ppd)
-         {
-            cupsDirClose(dir);
-           cupsFileClose(fp);
-           return (0);
-         }
+  snprintf(filename, filesize, "%s/ppds.dat", cups_cachedir);
+  if ((fp = cupsFileOpen(filename, "r")) != NULL)
+  {
+   /*
+    * See if we have the right sync word...
+    */
 
-          if (start && *start)
-         {
-           for (i = 1; i < PPD_MAX_LANG && *start; i ++)
-           {
-             if ((ptr = strchr(start, ',')) != NULL)
-               *ptr++ = '\0';
-             else
-               ptr = start + strlen(start);
+    unsigned ppdsync;                  /* Sync word */
+    int      num_ppds;                 /* Number of PPDs */
 
-              strlcpy(ppd->record.languages[i], start,
-                     sizeof(ppd->record.languages[0]));
+    if (cupsFileRead(fp, (char *)&ppdsync, sizeof(ppdsync))
+            == sizeof(ppdsync) &&
+        ppdsync == PPD_SYNC &&
+        !stat(filename, &fileinfo) &&
+       ((fileinfo.st_size - sizeof(ppdsync)) % sizeof(ppd_rec_t)) == 0 &&
+       (num_ppds = (fileinfo.st_size - sizeof(ppdsync)) /
+                   sizeof(ppd_rec_t)) > 0)
+    {
+     /*
+      * We have a ppds.dat file, so read it!
+      */
 
-             start = ptr;
-           }
-          }
+      for (; num_ppds > 0; num_ppds --)
+      {
+       if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
+       {
+         if (verbose)
+           fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
+                 stderr);
+         exit(1);
+       }
 
-          fprintf(stderr, "DEBUG: [cups-driverd] Added dynamic PPD \"%s\"...\n",
-                 name);
+       if (cupsFileRead(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)) > 0)
+       {
+         cupsArrayAdd(PPDsByName, ppd);
+         cupsArrayAdd(PPDsByMakeModel, ppd);
+       }
+       else
+       {
+         free(ppd);
+         break;
        }
       }
 
-      cupsFileClose(fp);
+      if (verbose)
+       fprintf(stderr, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
+               filename, cupsArrayCount(PPDsByName));
     }
-    else
-      fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
-              filename, strerror(errno));
-  }
-
-  cupsDirClose(dir);
 
-  return (1);
+    cupsFileClose(fp);
+  }
 }
 
 
@@ -2126,14 +2437,14 @@ regex_device_id(const char *device_id)  /* I - IEEE-1284 device ID */
 
   while (*device_id && ptr < (res + sizeof(res) - 6))
   {
-    cmd = !strncasecmp(device_id, "COMMAND SET:", 12) ||
-          !strncasecmp(device_id, "CMD:", 4);
-
-    if (cmd || !strncasecmp(device_id, "MANUFACTURER:", 13) ||
-        !strncasecmp(device_id, "MFG:", 4) ||
-        !strncasecmp(device_id, "MFR:", 4) ||
-        !strncasecmp(device_id, "MODEL:", 6) ||
-        !strncasecmp(device_id, "MDL:", 4))
+    cmd = !_cups_strncasecmp(device_id, "COMMAND SET:", 12) ||
+          !_cups_strncasecmp(device_id, "CMD:", 4);
+
+    if (cmd || !_cups_strncasecmp(device_id, "MANUFACTURER:", 13) ||
+        !_cups_strncasecmp(device_id, "MFG:", 4) ||
+        !_cups_strncasecmp(device_id, "MFR:", 4) ||
+        !_cups_strncasecmp(device_id, "MODEL:", 6) ||
+        !_cups_strncasecmp(device_id, "MDL:", 4))
     {
       if (ptr > res)
       {
@@ -2143,15 +2454,34 @@ regex_device_id(const char *device_id)  /* I - IEEE-1284 device ID */
 
       *ptr++ = '(';
 
-      while (*device_id && *device_id != ';' && ptr < (res + sizeof(res) - 4))
+      while (*device_id && *device_id != ';' && ptr < (res + sizeof(res) - 8))
       {
         if (strchr("[]{}().*\\|", *device_id))
          *ptr++ = '\\';
-       *ptr++ = *device_id++;
+        if (*device_id == ':')
+       {
+        /*
+         * KEY:.*value
+         */
+
+         *ptr++ = *device_id++;
+         *ptr++ = '.';
+         *ptr++ = '*';
+       }
+       else
+         *ptr++ = *device_id++;
       }
 
       if (*device_id == ';' || !*device_id)
+      {
+       /*
+        * KEY:.*value.*;
+       */
+
+       *ptr++ = '.';
+       *ptr++ = '*';
         *ptr++ = ';';
+      }
       *ptr++ = ')';
       if (cmd)
         *ptr++ = '?';