]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/cups-driverd.cxx
Merge changes from CUPS 1.6svn-r10127.
[thirdparty/cups.git] / scheduler / cups-driverd.cxx
CommitLineData
ef416fc2 1/*
4509bb49 2 * "$Id$"
ef416fc2 3 *
f14324a7 4 * PPD/driver support for CUPS.
ef416fc2 5 *
94da7e34
MS
6 * This program handles listing and installing static PPD files, PPD files
7 * created from driver information files, and dynamically generated PPD files
8 * using driver helper programs.
ef416fc2 9 *
f14324a7 10 * Copyright 2007-2011 by Apple Inc.
f899b121 11 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 12 *
13 * These coded instructions, statements, and computer programs are the
bc44d920 14 * property of Apple Inc. and are protected by Federal copyright
15 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
16 * which should have been included with this file. If this file is
17 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 18 *
19 * Contents:
20 *
58dc1933
MS
21 * main() - Scan for drivers and return an IPP response.
22 * add_ppd() - Add a PPD file.
23 * cat_drv() - Generate a PPD from a driver info file.
24 * cat_ppd() - Copy a PPD file to stdout.
25 * copy_static() - Copy a static PPD file to stdout.
178cb736 26 * compare_inodes() - Compare two inodes.
58dc1933
MS
27 * compare_matches() - Compare PPD match scores for sorting.
28 * compare_names() - Compare PPD filenames for sorting.
29 * compare_ppds() - Compare PPD file make and model names for sorting.
f0ab5bff 30 * dump_ppds_dat() - Dump the contents of the ppds.dat file.
58dc1933
MS
31 * free_array() - Free an array of strings.
32 * list_ppds() - List PPD files.
58dc1933
MS
33 * load_drv() - Load the PPDs from a driver information file.
34 * load_drivers() - Load driver-generated PPD files.
07ed0e9a 35 * load_ppds() - Load PPD files recursively.
f0ab5bff 36 * load_ppds_dat() - Load the ppds.dat file.
58dc1933
MS
37 * regex_device_id() - Compile a regular expression based on the 1284 device
38 * ID.
39 * regex_string() - Construct a regular expression to compare a simple
40 * string.
ef416fc2 41 */
42
43/*
44 * Include necessary headers...
45 */
46
47#include "util.h"
48#include <cups/dir.h>
80ca4592 49#include <cups/transcode.h>
5eb9da71 50#include <cups/ppd-private.h>
4509bb49 51#include <ppdc/ppdc.h>
58dc1933 52#include <regex.h>
ef416fc2 53
54
b94498cf 55/*
56 * Constants...
57 */
58
b0f6947b 59#define PPD_SYNC 0x50504437 /* Sync word for ppds.dat (PPD7) */
b94498cf 60#define PPD_MAX_LANG 32 /* Maximum languages */
b0f6947b
MS
61#define PPD_MAX_PROD 32 /* Maximum products */
62#define PPD_MAX_VERS 32 /* Maximum versions */
b94498cf 63
3d8365b8 64#define PPD_TYPE_POSTSCRIPT 0 /* PostScript PPD */
65#define PPD_TYPE_PDF 1 /* PDF PPD */
66#define PPD_TYPE_RASTER 2 /* CUPS raster PPD */
67#define PPD_TYPE_FAX 3 /* Facsimile/MFD PPD */
68#define PPD_TYPE_UNKNOWN 4 /* Other/hybrid PPD */
66ab9486 69#define PPD_TYPE_DRV 5 /* Driver info file */
3d8365b8 70
ac884b6a 71static const char * const ppd_types[] = /* ppd-type values */
3d8365b8 72{
73 "postscript",
74 "pdf",
75 "raster",
76 "fax",
94da7e34
MS
77 "unknown",
78 "drv"
3d8365b8 79};
80
b94498cf 81
ef416fc2 82/*
83 * PPD information structures...
84 */
85
86typedef struct /**** PPD record ****/
87{
88 time_t mtime; /* Modification time */
749b1e90 89 off_t size; /* Size in bytes */
3d8365b8 90 int model_number; /* cupsModelNumber */
91 int type; /* ppd-type */
66ab9486
MS
92 char filename[512], /* Filename */
93 name[512], /* PPD name */
b94498cf 94 languages[PPD_MAX_LANG][6],
95 /* LanguageVersion/cupsLanguages */
96 products[PPD_MAX_PROD][128],
97 /* Product strings */
98 psversions[PPD_MAX_VERS][32],
99 /* PSVersion strings */
ef416fc2 100 make[128], /* Manufacturer */
f899b121 101 make_and_model[128], /* NickName/ModelName */
ed6e7faf
MS
102 device_id[256], /* IEEE 1284 Device ID */
103 scheme[128]; /* PPD scheme */
88f9aafc 104} ppd_rec_t;
ef416fc2 105
106typedef struct /**** In-memory record ****/
107{
108 int found; /* 1 if PPD is found */
58dc1933 109 int matches; /* Match count */
ef416fc2 110 ppd_rec_t record; /* PPDs.dat record */
111} ppd_info_t;
112
113
114/*
115 * Globals...
116 */
117
178cb736
MS
118cups_array_t *Inodes = NULL, /* Inodes of directories we've visited */
119 *PPDsByName = NULL, /* PPD files sorted by filename and name */
66ab9486 120 *PPDsByMakeModel = NULL;/* PPD files sorted by make and model */
ef416fc2 121int ChangedPPD; /* Did we change the PPD database? */
122
123
124/*
125 * Local functions...
126 */
127
66ab9486
MS
128static ppd_info_t *add_ppd(const char *filename, const char *name,
129 const char *language, const char *make,
130 const char *make_and_model,
f899b121 131 const char *device_id, const char *product,
b94498cf 132 const char *psversion, time_t mtime,
ed6e7faf
MS
133 size_t size, int model_number, int type,
134 const char *scheme);
4509bb49 135static int cat_drv(const char *name, int request_id);
b94498cf 136static int cat_ppd(const char *name, int request_id);
4509bb49 137static int cat_static(const char *name, int request_id);
178cb736 138static int compare_inodes(struct stat *a, struct stat *b);
58dc1933
MS
139static int compare_matches(const ppd_info_t *p0,
140 const ppd_info_t *p1);
bd7854cb 141static int compare_names(const ppd_info_t *p0,
142 const ppd_info_t *p1);
143static int compare_ppds(const ppd_info_t *p0,
144 const ppd_info_t *p1);
f0ab5bff 145static int dump_ppds_dat(void);
b94498cf 146static void free_array(cups_array_t *a);
bd7854cb 147static int list_ppds(int request_id, int limit, const char *opt);
ed6e7faf
MS
148static int load_drivers(cups_array_t *include,
149 cups_array_t *exclude);
4509bb49
MS
150static int load_drv(const char *filename, const char *name,
151 cups_file_t *fp, time_t mtime, off_t size);
b94498cf 152static int load_ppds(const char *d, const char *p, int descend);
f0ab5bff
MS
153static void load_ppds_dat(char *filename, size_t filesize,
154 int verbose);
58dc1933
MS
155static regex_t *regex_device_id(const char *device_id);
156static regex_t *regex_string(const char *s);
ef416fc2 157
158
159/*
160 * 'main()' - Scan for drivers and return an IPP response.
161 *
162 * Usage:
163 *
164 * cups-driverd request_id limit options
165 */
166
167int /* O - Exit code */
168main(int argc, /* I - Number of command-line args */
169 char *argv[]) /* I - Command-line arguments */
170{
171 /*
172 * Install or list PPDs...
173 */
174
175 if (argc == 3 && !strcmp(argv[1], "cat"))
b94498cf 176 return (cat_ppd(argv[2], 0));
f0ab5bff
MS
177 else if (argc == 2 && !strcmp(argv[1], "dump"))
178 return (dump_ppds_dat());
b94498cf 179 else if (argc == 4 && !strcmp(argv[1], "get"))
180 return (cat_ppd(argv[3], atoi(argv[2])));
ef416fc2 181 else if (argc == 5 && !strcmp(argv[1], "list"))
182 return (list_ppds(atoi(argv[2]), atoi(argv[3]), argv[4]));
183 else
184 {
185 fputs("Usage: cups-driverd cat ppd-name\n", stderr);
f0ab5bff 186 fputs("Usage: cups-driverd dump\n", stderr);
b94498cf 187 fputs("Usage: cups-driverd get request_id ppd-name\n", stderr);
ef416fc2 188 fputs("Usage: cups-driverd list request_id limit options\n", stderr);
189 return (1);
190 }
191}
192
193
194/*
195 * 'add_ppd()' - Add a PPD file.
196 */
197
bd7854cb 198static ppd_info_t * /* O - PPD */
66ab9486
MS
199add_ppd(const char *filename, /* I - PPD filename */
200 const char *name, /* I - PPD name */
b94498cf 201 const char *language, /* I - LanguageVersion */
ef416fc2 202 const char *make, /* I - Manufacturer */
f899b121 203 const char *make_and_model, /* I - NickName/ModelName */
89d46774 204 const char *device_id, /* I - 1284DeviceID */
f899b121 205 const char *product, /* I - Product */
b94498cf 206 const char *psversion, /* I - PSVersion */
ef416fc2 207 time_t mtime, /* I - Modification time */
3d8365b8 208 size_t size, /* I - File size */
209 int model_number, /* I - Model number */
ed6e7faf
MS
210 int type, /* I - Driver type */
211 const char *scheme) /* I - PPD scheme */
ef416fc2 212{
213 ppd_info_t *ppd; /* PPD */
b86bc4cf 214 char *recommended; /* Foomatic driver string */
ef416fc2 215
216
217 /*
218 * Add a new PPD file...
219 */
220
66ab9486 221 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
ef416fc2 222 {
66ab9486
MS
223 fprintf(stderr,
224 "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n",
225 cupsArrayCount(PPDsByName));
226 return (NULL);
ef416fc2 227 }
228
ef416fc2 229 /*
230 * Zero-out the PPD data and copy the values over...
231 */
232
3d8365b8 233 ppd->found = 1;
234 ppd->record.mtime = mtime;
235 ppd->record.size = size;
236 ppd->record.model_number = model_number;
237 ppd->record.type = type;
ef416fc2 238
66ab9486 239 strlcpy(ppd->record.filename, filename, sizeof(ppd->record.filename));
ef416fc2 240 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
b94498cf 241 strlcpy(ppd->record.languages[0], language,
242 sizeof(ppd->record.languages[0]));
243 strlcpy(ppd->record.products[0], product, sizeof(ppd->record.products[0]));
244 strlcpy(ppd->record.psversions[0], psversion,
245 sizeof(ppd->record.psversions[0]));
ef416fc2 246 strlcpy(ppd->record.make, make, sizeof(ppd->record.make));
247 strlcpy(ppd->record.make_and_model, make_and_model,
248 sizeof(ppd->record.make_and_model));
bd7854cb 249 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
ed6e7faf 250 strlcpy(ppd->record.scheme, scheme, sizeof(ppd->record.scheme));
ef416fc2 251
b86bc4cf 252 /*
253 * Strip confusing (and often wrong) "recommended" suffix added by
254 * Foomatic drivers...
255 */
256
b94498cf 257 if ((recommended = strstr(ppd->record.make_and_model,
258 " (recommended)")) != NULL)
b86bc4cf 259 *recommended = '\0';
260
66ab9486
MS
261 /*
262 * Add the PPD to the PPD arrays...
263 */
264
265 cupsArrayAdd(PPDsByName, ppd);
266 cupsArrayAdd(PPDsByMakeModel, ppd);
267
ef416fc2 268 /*
269 * Return the new PPD pointer...
270 */
271
272 return (ppd);
273}
274
275
4509bb49
MS
276/*
277 * 'cat_drv()' - Generate a PPD from a driver info file.
278 */
279
280static int /* O - Exit code */
281cat_drv(const char *name, /* I - PPD name */
282 int request_id) /* I - Request ID for response? */
283{
284 const char *datadir; // CUPS_DATADIR env var
285 ppdcSource *src; // PPD source file data
286 ppdcDriver *d; // Current driver
88f9aafc 287 cups_file_t *out; // Stdout via CUPS file API
4509bb49
MS
288 char message[2048], // status-message
289 filename[1024], // Full path to .drv file(s)
290 scheme[32], // URI scheme ("drv")
291 userpass[256], // User/password info (unused)
292 host[2], // Hostname (unused)
293 resource[1024], // Resource path (/dir/to/filename.drv)
294 *pc_file_name; // Filename portion of URI
295 int port; // Port number (unused)
296
297
298 // Determine where CUPS has installed the data files...
299 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
300 datadir = CUPS_DATADIR;
301
eac3a0a0 302 // Pull out the path to the .drv file...
4509bb49
MS
303 if (httpSeparateURI(HTTP_URI_CODING_ALL, name, scheme, sizeof(scheme),
304 userpass, sizeof(userpass), host, sizeof(host), &port,
305 resource, sizeof(resource)) < HTTP_URI_OK ||
306 strstr(resource, "../") ||
307 (pc_file_name = strrchr(resource, '/')) == NULL ||
308 pc_file_name == resource)
309 {
310 fprintf(stderr, "ERROR: Bad PPD name \"%s\"!\n", name);
311
312 if (request_id)
313 {
314 snprintf(message, sizeof(message), "Bad PPD name \"%s\"!", name);
315
316 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
317 cupsdSendIPPGroup(IPP_TAG_OPERATION);
318 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
319 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
320 "en-US");
321 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
322 cupsdSendIPPTrailer();
323 }
324
325 return (1);
326 }
327
328 *pc_file_name++ = '\0';
329
330#ifdef __APPLE__
18ecb428
MS
331 if (!strncmp(resource, "/Library/Printers/PPDs/Contents/Resources/", 42) ||
332 !strncmp(resource, "/System/Library/Printers/PPDs/Contents/Resources/", 49))
4509bb49
MS
333 strlcpy(filename, resource, sizeof(filename));
334 else
335#endif // __APPLE__
336 {
337 snprintf(filename, sizeof(filename), "%s/drv%s", datadir, resource);
338 if (access(filename, 0))
339 snprintf(filename, sizeof(filename), "%s/model%s", datadir, resource);
340 }
341
342 src = new ppdcSource(filename);
343
344 for (d = (ppdcDriver *)src->drivers->first();
345 d;
346 d = (ppdcDriver *)src->drivers->next())
d1c13e16
MS
347 if (!strcmp(pc_file_name, d->pc_file_name->value) ||
348 (d->file_name && !strcmp(pc_file_name, d->file_name->value)))
4509bb49
MS
349 break;
350
351 if (d)
352 {
353 ppdcArray *locales; // Locale names
354 ppdcCatalog *catalog; // Message catalog in .drv file
355
356
f0ab5bff 357 fprintf(stderr, "DEBUG2: [cups-driverd] %d locales defined in \"%s\"...\n",
4509bb49
MS
358 src->po_files->count, filename);
359
360 locales = new ppdcArray();
361 for (catalog = (ppdcCatalog *)src->po_files->first();
362 catalog;
363 catalog = (ppdcCatalog *)src->po_files->next())
364 {
f0ab5bff 365 fprintf(stderr, "DEBUG2: [cups-driverd] Adding locale \"%s\"...\n",
4509bb49 366 catalog->locale->value);
97c9a8d7 367 catalog->locale->retain();
4509bb49
MS
368 locales->add(catalog->locale);
369 }
370
371 if (request_id)
372 {
373 cupsdSendIPPHeader(IPP_OK, request_id);
374 cupsdSendIPPGroup(IPP_TAG_OPERATION);
375 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
376 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
377 "en-US");
378 cupsdSendIPPTrailer();
379 fflush(stdout);
380 }
381
382 out = cupsFileStdout();
383 d->write_ppd_file(out, NULL, locales, src, PPDC_LFONLY);
384 cupsFileClose(out);
385
e4572d57 386 locales->release();
4509bb49
MS
387 }
388 else
389 {
390 fprintf(stderr, "ERROR: PPD \"%s\" not found!\n", name);
391
392 if (request_id)
393 {
394 snprintf(message, sizeof(message), "PPD \"%s\" not found!", name);
395
396 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
397 cupsdSendIPPGroup(IPP_TAG_OPERATION);
398 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
399 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
400 "en-US");
401 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
402 cupsdSendIPPTrailer();
403 }
404 }
405
e4572d57 406 src->release();
4509bb49
MS
407
408 return (!d);
409}
410
411
ef416fc2 412/*
413 * 'cat_ppd()' - Copy a PPD file to stdout.
414 */
415
bd7854cb 416static int /* O - Exit code */
b94498cf 417cat_ppd(const char *name, /* I - PPD name */
418 int request_id) /* I - Request ID for response? */
ef416fc2 419{
420 char scheme[256], /* Scheme from PPD name */
4509bb49
MS
421 *sptr, /* Pointer into scheme */
422 line[1024], /* Line/filename */
423 message[2048]; /* status-message */
ef416fc2 424
425
426 /*
427 * Figure out if this is a static or dynamic PPD file...
428 */
429
430 strlcpy(scheme, name, sizeof(scheme));
431 if ((sptr = strchr(scheme, ':')) != NULL)
432 {
433 *sptr = '\0';
434
435 if (!strcmp(scheme, "file"))
436 {
437 /*
438 * "file:name" == "name"...
439 */
440
441 name += 5;
442 scheme[0] = '\0';
443 }
444 }
445 else
446 scheme[0] = '\0';
447
09a101d6 448 if (request_id > 0)
449 puts("Content-Type: application/ipp\n");
b94498cf 450
4509bb49
MS
451 if (!scheme[0])
452 return (cat_static(name, request_id));
453 else if (!strcmp(scheme, "drv"))
454 return (cat_drv(name, request_id));
455 else
ef416fc2 456 {
457 /*
458 * Dynamic PPD, see if we have a driver program to support it...
459 */
460
461 const char *serverbin; /* CUPS_SERVERBIN env var */
c934a06c 462 char *argv[4]; /* Arguments for program */
ef416fc2 463
464
465 if ((serverbin = getenv("CUPS_SERVERBIN")) == NULL)
466 serverbin = CUPS_SERVERBIN;
467
468 snprintf(line, sizeof(line), "%s/driver/%s", serverbin, scheme);
469 if (access(line, X_OK))
470 {
471 /*
472 * File does not exist or is not executable...
473 */
474
475 fprintf(stderr, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n",
476 line, strerror(errno));
b94498cf 477
478 if (request_id > 0)
479 {
480 snprintf(message, sizeof(message), "Unable to access \"%s\" - %s",
481 line, strerror(errno));
482
483 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
484 cupsdSendIPPGroup(IPP_TAG_OPERATION);
485 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
486 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
487 "en-US");
488 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
489 cupsdSendIPPTrailer();
490 }
491
ef416fc2 492 return (1);
493 }
494
495 /*
496 * Yes, let it cat the PPD file...
497 */
498
b94498cf 499 if (request_id)
500 {
501 cupsdSendIPPHeader(IPP_OK, request_id);
502 cupsdSendIPPGroup(IPP_TAG_OPERATION);
503 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
504 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
505 "en-US");
506 cupsdSendIPPTrailer();
507 }
508
c934a06c
MS
509 argv[0] = scheme;
510 argv[1] = (char *)"cat";
511 argv[2] = (char *)name;
512 argv[3] = NULL;
513
514 if (cupsdExec(line, argv))
ef416fc2 515 {
516 /*
517 * Unable to execute driver...
518 */
519
520 fprintf(stderr, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n",
521 line, strerror(errno));
522 return (1);
523 }
524 }
ef416fc2 525
4509bb49
MS
526 /*
527 * Return with no errors...
528 */
ef416fc2 529
4509bb49
MS
530 return (0);
531}
ef416fc2 532
b94498cf 533
4509bb49
MS
534/*
535 * 'copy_static()' - Copy a static PPD file to stdout.
536 */
b94498cf 537
4509bb49
MS
538static int /* O - Exit code */
539cat_static(const char *name, /* I - PPD name */
540 int request_id) /* I - Request ID for response? */
541{
542 cups_file_t *fp; /* PPD file */
543 const char *datadir; /* CUPS_DATADIR env var */
544 char line[1024], /* Line/filename */
545 message[2048]; /* status-message */
22c9029b
MS
546#ifdef __APPLE__
547 const char *printerDriver, /* Pointer to .printerDriver extension */
548 *slash; /* Pointer to next slash */
549#endif /* __APPLE__ */
ef416fc2 550
eac3a0a0 551
4509bb49
MS
552 if (name[0] == '/' || strstr(name, "../") || strstr(name, "/.."))
553 {
ef416fc2 554 /*
4509bb49 555 * Bad name...
ef416fc2 556 */
557
4509bb49
MS
558 fprintf(stderr, "ERROR: [cups-driverd] Bad PPD name \"%s\"!\n", name);
559
560 if (request_id)
b94498cf 561 {
4509bb49 562 snprintf(message, sizeof(message), "Bad PPD name \"%s\"!", name);
b94498cf 563
4509bb49
MS
564 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
565 cupsdSendIPPGroup(IPP_TAG_OPERATION);
566 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
567 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
568 "en-US");
569 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
570 cupsdSendIPPTrailer();
b94498cf 571 }
b94498cf 572
4509bb49
MS
573 return (1);
574 }
b94498cf 575
4509bb49
MS
576 /*
577 * Try opening the file...
578 */
b94498cf 579
4509bb49
MS
580#ifdef __APPLE__
581 if (!strncmp(name, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
22c9029b
MS
582 !strncmp(name, "Library/Printers/PPDs/Contents/Resources/", 41) ||
583 (!strncmp(name, "System/Library/Printers/", 24) &&
584 (printerDriver =
585 strstr(name + 24,
586 ".printerDriver/Contents/Resources/PPDs")) != NULL &&
587 (slash = strchr(name + 24, '/')) != NULL &&
588 slash > printerDriver) ||
589 (!strncmp(name, "Library/Printers/", 17) &&
590 (printerDriver =
591 strstr(name + 17,
592 ".printerDriver/Contents/Resources/PPDs")) != NULL &&
593 (slash = strchr(name + 17, '/')) != NULL &&
594 slash > printerDriver))
4509bb49
MS
595 {
596 /*
597 * Map ppd-name to Mac OS X standard locations...
598 */
b94498cf 599
4509bb49
MS
600 snprintf(line, sizeof(line), "/%s", name);
601 }
eac3a0a0 602 else
b94498cf 603
4509bb49
MS
604#elif defined(__linux)
605 if (!strncmp(name, "lsb/usr/", 8))
606 {
607 /*
608 * Map ppd-name to LSB standard /usr/share/ppd location...
609 */
b94498cf 610
4509bb49
MS
611 snprintf(line, sizeof(line), "/usr/share/ppd/%s", name + 8);
612 }
613 else if (!strncmp(name, "lsb/opt/", 8))
614 {
615 /*
616 * Map ppd-name to LSB standard /opt/share/ppd location...
617 */
ef416fc2 618
4509bb49
MS
619 snprintf(line, sizeof(line), "/opt/share/ppd/%s", name + 8);
620 }
621 else if (!strncmp(name, "lsb/local/", 10))
622 {
623 /*
624 * Map ppd-name to LSB standard /usr/local/share/ppd location...
625 */
b94498cf 626
4509bb49
MS
627 snprintf(line, sizeof(line), "/usr/local/share/ppd/%s", name + 10);
628 }
629 else
b94498cf 630
4509bb49
MS
631#endif /* __APPLE__ */
632 {
633 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
634 datadir = CUPS_DATADIR;
b94498cf 635
4509bb49
MS
636 snprintf(line, sizeof(line), "%s/model/%s", datadir, name);
637 }
638
639 if ((fp = cupsFileOpen(line, "r")) == NULL)
640 {
641 fprintf(stderr, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n",
642 line, strerror(errno));
ef416fc2 643
b94498cf 644 if (request_id)
645 {
4509bb49
MS
646 snprintf(message, sizeof(message), "Unable to open \"%s\" - %s",
647 line, strerror(errno));
648
649 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
b94498cf 650 cupsdSendIPPGroup(IPP_TAG_OPERATION);
651 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
652 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
4509bb49
MS
653 "en-US");
654 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
b94498cf 655 cupsdSendIPPTrailer();
656 }
657
4509bb49
MS
658 return (1);
659 }
ef416fc2 660
4509bb49
MS
661 if (request_id)
662 {
663 cupsdSendIPPHeader(IPP_OK, request_id);
664 cupsdSendIPPGroup(IPP_TAG_OPERATION);
665 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
666 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
667 "en-US");
668 cupsdSendIPPTrailer();
ef416fc2 669 }
670
671 /*
4509bb49 672 * Now copy the file to stdout...
ef416fc2 673 */
674
4509bb49
MS
675 while (cupsFileGets(fp, line, sizeof(line)))
676 puts(line);
677
678 cupsFileClose(fp);
679
ef416fc2 680 return (0);
681}
682
683
178cb736
MS
684/*
685 * 'compare_inodes()' - Compare two inodes.
686 */
687
688static int /* O - Result of comparison */
689compare_inodes(struct stat *a, /* I - First inode */
690 struct stat *b) /* I - Second inode */
691{
692 if (a->st_dev != b->st_dev)
693 return (a->st_dev - b->st_dev);
694 else
695 return (a->st_ino - b->st_ino);
696}
697
698
58dc1933
MS
699/*
700 * 'compare_matches()' - Compare PPD match scores for sorting.
701 */
702
703static int
704compare_matches(const ppd_info_t *p0, /* I - First PPD */
705 const ppd_info_t *p1) /* I - Second PPD */
706{
707 if (p1->matches != p0->matches)
708 return (p1->matches - p0->matches);
709 else
e4572d57
MS
710 return (cupsdCompareNames(p0->record.make_and_model,
711 p1->record.make_and_model));
58dc1933
MS
712}
713
714
ef416fc2 715/*
716 * 'compare_names()' - Compare PPD filenames for sorting.
717 */
718
bd7854cb 719static int /* O - Result of comparison */
ef416fc2 720compare_names(const ppd_info_t *p0, /* I - First PPD file */
721 const ppd_info_t *p1) /* I - Second PPD file */
722{
66ab9486
MS
723 int diff; /* Difference between strings */
724
725
d1c13e16 726 if ((diff = strcmp(p0->record.filename, p1->record.filename)) != 0)
66ab9486
MS
727 return (diff);
728 else
d1c13e16 729 return (strcmp(p0->record.name, p1->record.name));
ef416fc2 730}
731
732
733/*
734 * 'compare_ppds()' - Compare PPD file make and model names for sorting.
735 */
736
bd7854cb 737static int /* O - Result of comparison */
ef416fc2 738compare_ppds(const ppd_info_t *p0, /* I - First PPD file */
739 const ppd_info_t *p1) /* I - Second PPD file */
740{
741 int diff; /* Difference between strings */
742
bd7854cb 743
ef416fc2 744 /*
745 * First compare manufacturers...
746 */
747
88f9aafc 748 if ((diff = _cups_strcasecmp(p0->record.make, p1->record.make)) != 0)
ef416fc2 749 return (diff);
750 else if ((diff = cupsdCompareNames(p0->record.make_and_model,
751 p1->record.make_and_model)) != 0)
752 return (diff);
f99f3698
MS
753 else if ((diff = strcmp(p0->record.languages[0],
754 p1->record.languages[0])) != 0)
755 return (diff);
ef416fc2 756 else
f99f3698 757 return (compare_names(p0, p1));
b94498cf 758}
759
760
f0ab5bff
MS
761/*
762 * 'dump_ppds_dat()' - Dump the contents of the ppds.dat file.
763 */
764
765static int /* O - Exit status */
766dump_ppds_dat(void)
767{
768 char filename[1024]; /* ppds.dat filename */
769 ppd_info_t *ppd; /* Current PPD */
770
771
772 /*
773 * See if we a PPD database file...
774 */
775
776 load_ppds_dat(filename, sizeof(filename), 0);
777
778 puts("mtime,size,model_number,type,filename,name,languages0,products0,"
779 "psversions0,make,make_and_model,device_id,scheme");
780 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
781 ppd;
782 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
783 printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
784 "\"%s\",\"%s\"\n",
785 (int)ppd->record.mtime, (long)ppd->record.size,
786 ppd->record.model_number, ppd->record.type, ppd->record.filename,
787 ppd->record.name, ppd->record.languages[0], ppd->record.products[0],
788 ppd->record.psversions[0], ppd->record.make,
789 ppd->record.make_and_model, ppd->record.device_id,
790 ppd->record.scheme);
791
792 return (0);
793}
794
795
b94498cf 796/*
797 * 'free_array()' - Free an array of strings.
798 */
799
800static void
801free_array(cups_array_t *a) /* I - Array to free */
802{
803 char *ptr; /* Pointer to string */
804
805
806 for (ptr = (char *)cupsArrayFirst(a);
807 ptr;
808 ptr = (char *)cupsArrayNext(a))
809 free(ptr);
810
811 cupsArrayDelete(a);
ef416fc2 812}
813
814
815/*
816 * 'list_ppds()' - List PPD files.
817 */
818
bd7854cb 819static int /* O - Exit code */
ef416fc2 820list_ppds(int request_id, /* I - Request ID */
821 int limit, /* I - Limit */
822 const char *opt) /* I - Option argument */
823{
66ab9486 824 int i; /* Looping vars */
ef416fc2 825 int count; /* Number of PPDs to send */
826 ppd_info_t *ppd; /* Current PPD file */
827 cups_file_t *fp; /* ppds.dat file */
ef416fc2 828 char filename[1024], /* ppds.dat filename */
829 model[1024]; /* Model directory */
ef416fc2 830 const char *cups_datadir; /* CUPS_DATADIR environment variable */
831 int num_options; /* Number of options */
832 cups_option_t *options; /* Options */
ed6e7faf
MS
833 cups_array_t *requested, /* requested-attributes values */
834 *include, /* PPD schemes to include */
835 *exclude; /* PPD schemes to exclude */
836 const char *device_id, /* ppd-device-id option */
b94498cf 837 *language, /* ppd-natural-language option */
838 *make, /* ppd-make option */
839 *make_and_model, /* ppd-make-and-model option */
3d8365b8 840 *model_number_str, /* ppd-model-number option */
b94498cf 841 *product, /* ppd-product option */
3d8365b8 842 *psversion, /* ppd-psversion option */
843 *type_str; /* ppd-type option */
844 int model_number, /* ppd-model-number value */
845 type, /* ppd-type value */
58dc1933
MS
846 make_and_model_len, /* Length of ppd-make-and-model */
847 product_len, /* Length of ppd-product */
3d8365b8 848 send_device_id, /* Send ppd-device-id? */
b94498cf 849 send_make, /* Send ppd-make? */
850 send_make_and_model, /* Send ppd-make-and-model? */
3d8365b8 851 send_model_number, /* Send ppd-model-number? */
b94498cf 852 send_name, /* Send ppd-name? */
3d8365b8 853 send_natural_language, /* Send ppd-natural-language? */
b94498cf 854 send_product, /* Send ppd-product? */
855 send_psversion, /* Send ppd-psversion? */
3d8365b8 856 send_type, /* Send ppd-type? */
b94498cf 857 sent_header; /* Sent the IPP header? */
58dc1933
MS
858 regex_t *device_id_re, /* Regular expression for matching device ID */
859 *make_and_model_re; /* Regular expression for matching make and model */
860 regmatch_t re_matches[6]; /* Regular expression matches */
861 cups_array_t *matches; /* Matching PPDs */
b94498cf 862
863
864 fprintf(stderr,
865 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
866 "opt=\"%s\"\n", request_id, limit, opt);
ef416fc2 867
868 /*
869 * See if we a PPD database file...
870 */
871
f0ab5bff 872 load_ppds_dat(filename, sizeof(filename), 1);
ef416fc2 873
ef416fc2 874 /*
875 * Load all PPDs in the specified directory and below...
876 */
877
ef416fc2 878 if ((cups_datadir = getenv("CUPS_DATADIR")) == NULL)
879 cups_datadir = CUPS_DATADIR;
880
178cb736
MS
881 Inodes = cupsArrayNew((cups_array_func_t)compare_inodes, NULL);
882
ef416fc2 883 snprintf(model, sizeof(model), "%s/model", cups_datadir);
b94498cf 884 load_ppds(model, "", 1);
885
4509bb49
MS
886 snprintf(model, sizeof(model), "%s/drv", cups_datadir);
887 load_ppds(model, "", 1);
888
b94498cf 889#ifdef __APPLE__
890 /*
891 * Load PPDs from standard Mac OS X locations...
892 */
893
22c9029b
MS
894 load_ppds("/Library/Printers",
895 "Library/Printers", 0);
b94498cf 896 load_ppds("/Library/Printers/PPDs/Contents/Resources",
897 "Library/Printers/PPDs/Contents/Resources", 0);
898 load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
899 "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
22c9029b
MS
900 load_ppds("/System/Library/Printers",
901 "System/Library/Printers", 0);
b94498cf 902 load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
903 "System/Library/Printers/PPDs/Contents/Resources", 0);
904 load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
905 "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
906
907#elif defined(__linux)
908 /*
909 * Load PPDs from LSB-defined locations...
910 */
911
568fa3fa
MS
912 if (!access("/usr/local/share/ppd", 0))
913 load_ppds("/usr/local/share/ppd", "lsb/local", 1);
914 if (!access("/usr/share/ppd", 0))
915 load_ppds("/usr/share/ppd", "lsb/usr", 1);
916 if (!access("/opt/share/ppd", 0))
917 load_ppds("/opt/share/ppd", "lsb/opt", 1);
b94498cf 918#endif /* __APPLE__ */
ef416fc2 919
920 /*
921 * Cull PPD files that are no longer present...
922 */
923
66ab9486
MS
924 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
925 ppd;
926 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
ef416fc2 927 if (!ppd->found)
928 {
929 /*
930 * Remove this PPD file from the list...
931 */
932
66ab9486
MS
933 cupsArrayRemove(PPDsByName, ppd);
934 cupsArrayRemove(PPDsByMakeModel, ppd);
935 free(ppd);
f0ab5bff
MS
936
937 ChangedPPD = 1;
ef416fc2 938 }
939
ef416fc2 940 /*
941 * Write the new ppds.dat file...
942 */
943
f0ab5bff
MS
944 fprintf(stderr, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD);
945
ef416fc2 946 if (ChangedPPD)
947 {
07ed0e9a
MS
948 char newname[1024]; /* New filename */
949
950 snprintf(newname, sizeof(newname), "%s.%d", filename, (int)getpid());
951
952 if ((fp = cupsFileOpen(newname, "w")) != NULL)
ef416fc2 953 {
b94498cf 954 unsigned ppdsync = PPD_SYNC; /* Sync word */
955
b94498cf 956 cupsFileWrite(fp, (char *)&ppdsync, sizeof(ppdsync));
957
66ab9486
MS
958 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
959 ppd;
960 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
ef416fc2 961 cupsFileWrite(fp, (char *)&(ppd->record), sizeof(ppd_rec_t));
962
963 cupsFileClose(fp);
964
07ed0e9a
MS
965 if (rename(newname, filename))
966 fprintf(stderr, "ERROR: [cups-driverd] Unable to rename \"%s\" - %s\n",
967 newname, strerror(errno));
968 else
969 fprintf(stderr, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
970 filename, cupsArrayCount(PPDsByName));
ef416fc2 971 }
972 else
973 fprintf(stderr, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
974 filename, strerror(errno));
975 }
976 else
977 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr);
978
979 /*
980 * Scan for dynamic PPD files...
981 */
982
ed6e7faf
MS
983 num_options = cupsParseOptions(opt, 0, &options);
984 exclude = cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
985 num_options, options));
8b450588 986 include = cupsdCreateStringsArray(cupsGetOption("include-schemes",
ed6e7faf
MS
987 num_options, options));
988
989 load_drivers(include, exclude);
ef416fc2 990
991 /*
992 * Add the raw driver...
993 */
994
66ab9486 995 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
ed6e7faf 996 PPD_TYPE_UNKNOWN, "raw");
ef416fc2 997
ef416fc2 998 /*
999 * Send IPP attributes...
1000 */
1001
ed6e7faf
MS
1002 requested = cupsdCreateStringsArray(
1003 cupsGetOption("requested-attributes", num_options,
1004 options));
3d8365b8 1005 device_id = cupsGetOption("ppd-device-id", num_options, options);
1006 language = cupsGetOption("ppd-natural-language", num_options, options);
1007 make = cupsGetOption("ppd-make", num_options, options);
1008 make_and_model = cupsGetOption("ppd-make-and-model", num_options, options);
1009 model_number_str = cupsGetOption("ppd-model-number", num_options, options);
1010 product = cupsGetOption("ppd-product", num_options, options);
1011 psversion = cupsGetOption("ppd-psversion", num_options, options);
1012 type_str = cupsGetOption("ppd-type", num_options, options);
b94498cf 1013
1014 if (make_and_model)
58dc1933 1015 make_and_model_len = strlen(make_and_model);
b94498cf 1016 else
58dc1933 1017 make_and_model_len = 0;
ef416fc2 1018
58dc1933
MS
1019 if (product)
1020 product_len = strlen(product);
b94498cf 1021 else
58dc1933 1022 product_len = 0;
b94498cf 1023
3d8365b8 1024 if (model_number_str)
1025 model_number = atoi(model_number_str);
1026 else
1027 model_number = 0;
1028
1029 if (type_str)
1030 {
1031 for (type = 0;
1032 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
1033 type ++)
1034 if (!strcmp(type_str, ppd_types[type]))
1035 break;
1036
1037 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
1038 {
1039 fprintf(stderr, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
1040 type_str);
1041 type_str = NULL;
1042 }
1043 }
bc44d920 1044 else
1045 type = 0;
3d8365b8 1046
ed6e7faf 1047 for (i = 0; i < num_options; i ++)
f0ab5bff 1048 fprintf(stderr, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options[i].name,
ed6e7faf 1049 options[i].value);
ef416fc2 1050
ed6e7faf 1051 if (!requested || cupsArrayFind(requested, (void *)"all") != NULL)
ef416fc2 1052 {
1053 send_name = 1;
1054 send_make = 1;
1055 send_make_and_model = 1;
3d8365b8 1056 send_model_number = 1;
ef416fc2 1057 send_natural_language = 1;
bd7854cb 1058 send_device_id = 1;
f899b121 1059 send_product = 1;
b94498cf 1060 send_psversion = 1;
3d8365b8 1061 send_type = 1;
ef416fc2 1062 }
1063 else
1064 {
ed6e7faf
MS
1065 send_name = cupsArrayFind(requested,
1066 (void *)"ppd-name") != NULL;
1067 send_make = cupsArrayFind(requested,
1068 (void *)"ppd-make") != NULL;
1069 send_make_and_model = cupsArrayFind(requested,
1070 (void *)"ppd-make-and-model") != NULL;
1071 send_model_number = cupsArrayFind(requested,
1072 (void *)"ppd-model-number") != NULL;
1073 send_natural_language = cupsArrayFind(requested,
1074 (void *)"ppd-natural-language") != NULL;
1075 send_device_id = cupsArrayFind(requested,
1076 (void *)"ppd-device-id") != NULL;
1077 send_product = cupsArrayFind(requested,
1078 (void *)"ppd-product") != NULL;
1079 send_psversion = cupsArrayFind(requested,
1080 (void *)"ppd-psversion") != NULL;
1081 send_type = cupsArrayFind(requested,
1082 (void *)"ppd-type") != NULL;
ef416fc2 1083 }
1084
f0ab5bff
MS
1085 /*
1086 * Send the content type header to the scheduler; request_id can only be
1087 * 0 when run manually since the scheduler enforces the IPP requirement for
1088 * a request ID from 1 to 2^31-1...
1089 */
1090
1091 if (request_id > 0)
1092 puts("Content-Type: application/ipp\n");
ef416fc2 1093
b94498cf 1094 sent_header = 0;
ef416fc2 1095
66ab9486
MS
1096 if (limit <= 0 || limit > cupsArrayCount(PPDsByMakeModel))
1097 count = cupsArrayCount(PPDsByMakeModel);
ef416fc2 1098 else
1099 count = limit;
1100
58dc1933 1101 if (device_id || language || make || make_and_model || model_number_str ||
8b450588 1102 product)
b94498cf 1103 {
58dc1933 1104 matches = cupsArrayNew((cups_array_func_t)compare_matches, NULL);
b94498cf 1105
58dc1933
MS
1106 if (device_id)
1107 device_id_re = regex_device_id(device_id);
1108 else
1109 device_id_re = NULL;
66ab9486 1110
58dc1933
MS
1111 if (make_and_model)
1112 make_and_model_re = regex_string(make_and_model);
1113 else
1114 make_and_model_re = NULL;
b94498cf 1115
58dc1933
MS
1116 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1117 ppd;
1118 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
ef416fc2 1119 {
58dc1933
MS
1120 /*
1121 * Filter PPDs based on make, model, product, language, model number,
1122 * and/or device ID using the "matches" score value. An exact match
1123 * for product, make-and-model, or device-id adds 3 to the score.
1124 * Partial matches for make-and-model yield 1 or 2 points, and matches
1125 * for the make and language add a single point. Results are then sorted
1126 * by score, highest score first.
1127 */
b94498cf 1128
58dc1933
MS
1129 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1130 ppd->record.type >= PPD_TYPE_DRV)
b94498cf 1131 continue;
b94498cf 1132
ed6e7faf
MS
1133 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1134 (include && !cupsArrayFind(include, ppd->record.scheme)))
1135 continue;
1136
58dc1933 1137 ppd->matches = 0;
b94498cf 1138
58dc1933
MS
1139 if (device_id_re &&
1140 !regexec(device_id_re, ppd->record.device_id,
1141 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1142 re_matches, 0))
1143 {
1144 /*
1145 * Add the number of matching values from the device ID - it will be
1146 * at least 2 (manufacturer and model), and as much as 3 (command set).
1147 */
b94498cf 1148
58dc1933
MS
1149 for (i = 1; i < (int)(sizeof(re_matches) / sizeof(re_matches[0])); i ++)
1150 if (re_matches[i].rm_so >= 0)
1151 ppd->matches ++;
1152 }
3d8365b8 1153
58dc1933
MS
1154 if (language)
1155 {
1156 for (i = 0; i < PPD_MAX_LANG; i ++)
238c3832
MS
1157 if (!ppd->record.languages[i][0])
1158 break;
1159 else if (!strcmp(ppd->record.languages[i], language))
58dc1933
MS
1160 {
1161 ppd->matches ++;
1162 break;
1163 }
1164 }
ef416fc2 1165
88f9aafc 1166 if (make && !_cups_strcasecmp(ppd->record.make, make))
58dc1933 1167 ppd->matches ++;
ef416fc2 1168
58dc1933
MS
1169 if (make_and_model_re &&
1170 !regexec(make_and_model_re, ppd->record.make_and_model,
1171 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1172 re_matches, 0))
1173 {
1174 // See how much of the make-and-model string we matched...
1175 if (re_matches[0].rm_so == 0)
1176 {
1177 if (re_matches[0].rm_eo == make_and_model_len)
1178 ppd->matches += 3; // Exact match
1179 else
1180 ppd->matches += 2; // Prefix match
1181 }
1182 else
1183 ppd->matches ++; // Infix match
1184 }
ef416fc2 1185
58dc1933
MS
1186 if (model_number_str && ppd->record.model_number == model_number)
1187 ppd->matches ++;
1188
1189 if (product)
1190 {
1191 for (i = 0; i < PPD_MAX_PROD; i ++)
238c3832
MS
1192 if (!ppd->record.products[i][0])
1193 break;
88f9aafc 1194 else if (!_cups_strcasecmp(ppd->record.products[i], product))
58dc1933
MS
1195 {
1196 ppd->matches += 3;
1197 break;
1198 }
83e08001
MS
1199 else if (!_cups_strncasecmp(ppd->record.products[i], product,
1200 product_len))
1201 {
1202 ppd->matches += 2;
1203 break;
1204 }
58dc1933
MS
1205 }
1206
1207 if (psversion)
1208 {
1209 for (i = 0; i < PPD_MAX_VERS; i ++)
238c3832
MS
1210 if (!ppd->record.psversions[i][0])
1211 break;
88f9aafc 1212 else if (!_cups_strcasecmp(ppd->record.psversions[i], psversion))
58dc1933
MS
1213 {
1214 ppd->matches ++;
1215 break;
1216 }
1217 }
1218
1219 if (type_str && ppd->record.type == type)
1220 ppd->matches ++;
1221
1222 if (ppd->matches)
1223 {
f0ab5bff 1224 fprintf(stderr, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
58dc1933
MS
1225 ppd->record.name, ppd->matches);
1226 cupsArrayAdd(matches, ppd);
1227 }
b94498cf 1228 }
58dc1933 1229 }
8b450588
MS
1230 else if (include || exclude)
1231 {
1232 matches = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
1233
1234 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1235 ppd;
1236 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
1237 {
1238 /*
1239 * Filter PPDs based on the include/exclude lists.
1240 */
1241
1242 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1243 ppd->record.type >= PPD_TYPE_DRV)
1244 continue;
1245
1246 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1247 (include && !cupsArrayFind(include, ppd->record.scheme)))
1248 continue;
1249
1250 cupsArrayAdd(matches, ppd);
1251 }
1252 }
58dc1933
MS
1253 else
1254 matches = PPDsByMakeModel;
ef416fc2 1255
58dc1933
MS
1256 for (ppd = (ppd_info_t *)cupsArrayFirst(matches);
1257 count > 0 && ppd;
1258 ppd = (ppd_info_t *)cupsArrayNext(matches))
1259 {
1260 /*
1261 * Skip invalid PPDs...
1262 */
1263
1264 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1265 ppd->record.type >= PPD_TYPE_DRV)
3d8365b8 1266 continue;
1267
b94498cf 1268 /*
1269 * Send this PPD...
1270 */
ef416fc2 1271
b94498cf 1272 if (!sent_header)
1273 {
1274 sent_header = 1;
ef416fc2 1275
b94498cf 1276 cupsdSendIPPHeader(IPP_OK, request_id);
1277 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1278 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
58dc1933
MS
1279 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
1280 "en-US");
b94498cf 1281 }
ef416fc2 1282
f0ab5bff 1283 fprintf(stderr, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
b94498cf 1284 ppd->record.name, ppd->record.make_and_model);
ef416fc2 1285
b94498cf 1286 count --;
bd7854cb 1287
b94498cf 1288 cupsdSendIPPGroup(IPP_TAG_PRINTER);
f899b121 1289
b94498cf 1290 if (send_name)
1291 cupsdSendIPPString(IPP_TAG_NAME, "ppd-name", ppd->record.name);
ef416fc2 1292
b94498cf 1293 if (send_natural_language)
1294 {
1295 cupsdSendIPPString(IPP_TAG_LANGUAGE, "ppd-natural-language",
1296 ppd->record.languages[0]);
ef416fc2 1297
66ab9486
MS
1298 for (i = 1; i < PPD_MAX_LANG && ppd->record.languages[i][0]; i ++)
1299 cupsdSendIPPString(IPP_TAG_LANGUAGE, "", ppd->record.languages[i]);
b94498cf 1300 }
ef416fc2 1301
b94498cf 1302 if (send_make)
1303 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make", ppd->record.make);
ef416fc2 1304
b94498cf 1305 if (send_make_and_model)
1306 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make-and-model",
1307 ppd->record.make_and_model);
1308
1309 if (send_device_id)
1310 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-device-id",
1311 ppd->record.device_id);
1312
1313 if (send_product)
1314 {
1315 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-product",
1316 ppd->record.products[0]);
1317
66ab9486
MS
1318 for (i = 1; i < PPD_MAX_PROD && ppd->record.products[i][0]; i ++)
1319 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.products[i]);
b94498cf 1320 }
1321
1322 if (send_psversion)
1323 {
1324 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-psversion",
1325 ppd->record.psversions[0]);
1326
66ab9486
MS
1327 for (i = 1; i < PPD_MAX_VERS && ppd->record.psversions[i][0]; i ++)
1328 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.psversions[i]);
b94498cf 1329 }
1330
3d8365b8 1331 if (send_type)
1332 cupsdSendIPPString(IPP_TAG_KEYWORD, "ppd-type",
1333 ppd_types[ppd->record.type]);
1334
1335 if (send_model_number)
1336 cupsdSendIPPInteger(IPP_TAG_INTEGER, "ppd-model-number",
1337 ppd->record.model_number);
1338
b94498cf 1339 /*
1340 * If we have only requested the ppd-make attribute, then skip
1341 * the remaining PPDs with this make...
1342 */
1343
ed6e7faf
MS
1344 if (cupsArrayFind(requested, (void *)"ppd-make") &&
1345 cupsArrayCount(requested) == 1)
b94498cf 1346 {
1347 const char *this_make; /* This ppd-make */
1348
1349
66ab9486 1350 for (this_make = ppd->record.make,
58dc1933 1351 ppd = (ppd_info_t *)cupsArrayNext(matches);
66ab9486 1352 ppd;
58dc1933 1353 ppd = (ppd_info_t *)cupsArrayNext(matches))
88f9aafc 1354 if (_cups_strcasecmp(this_make, ppd->record.make))
b94498cf 1355 break;
1356
58dc1933 1357 cupsArrayPrev(matches);
ef416fc2 1358 }
b94498cf 1359 }
1360
1361 if (!sent_header)
1362 {
1363 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
1364 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1365 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
1366 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
1367 }
ef416fc2 1368
1369 cupsdSendIPPTrailer();
1370
1371 return (0);
1372}
1373
1374
1375/*
07ed0e9a 1376 * 'load_drv()' - Load the PPDs from a driver information file.
ef416fc2 1377 */
1378
bd7854cb 1379static int /* O - 1 on success, 0 on failure */
07ed0e9a
MS
1380load_drv(const char *filename, /* I - Actual filename */
1381 const char *name, /* I - Name to the rest of the world */
1382 cups_file_t *fp, /* I - File to read from */
1383 time_t mtime, /* I - Mod time of driver info file */
1384 off_t size) /* I - Size of driver info file */
ef416fc2 1385{
07ed0e9a
MS
1386 ppdcSource *src; // Driver information file
1387 ppdcDriver *d; // Current driver
1388 ppdcAttr *device_id, // 1284DeviceID attribute
1389 *product, // Current product value
1390 *ps_version, // PSVersion attribute
1391 *cups_fax, // cupsFax attribute
1392 *nick_name; // NickName attribute
1393 ppdcFilter *filter; // Current filter
1394 ppd_info_t *ppd; // Current PPD
1395 int products_found; // Number of products found
1396 char uri[1024], // Driver URI
1397 make_model[1024]; // Make and model
1398 int type; // Driver type
ef416fc2 1399
1400
178cb736 1401 /*
07ed0e9a 1402 * Load the driver info file...
178cb736
MS
1403 */
1404
07ed0e9a 1405 src = new ppdcSource(filename, fp);
178cb736 1406
07ed0e9a 1407 if (src->drivers->count == 0)
178cb736 1408 {
07ed0e9a
MS
1409 fprintf(stderr,
1410 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1411 filename);
1412 src->release();
178cb736
MS
1413 return (0);
1414 }
1415
1416 /*
07ed0e9a 1417 * Add a dummy entry for the file...
178cb736
MS
1418 */
1419
a4845881 1420 add_ppd(name, name, "", "", "", "", "", "", mtime, size, 0,
07ed0e9a
MS
1421 PPD_TYPE_DRV, "drv");
1422 ChangedPPD = 1;
ef416fc2 1423
07ed0e9a
MS
1424 /*
1425 * Then the drivers in the file...
1426 */
178cb736 1427
07ed0e9a
MS
1428 for (d = (ppdcDriver *)src->drivers->first();
1429 d;
1430 d = (ppdcDriver *)src->drivers->next())
ef416fc2 1431 {
07ed0e9a
MS
1432 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0,
1433 "/%s/%s", name,
1434 d->file_name ? d->file_name->value :
1435 d->pc_file_name->value);
ef416fc2 1436
07ed0e9a
MS
1437 device_id = d->find_attr("1284DeviceID", NULL);
1438 ps_version = d->find_attr("PSVersion", NULL);
1439 nick_name = d->find_attr("NickName", NULL);
ef416fc2 1440
07ed0e9a
MS
1441 if (nick_name)
1442 strlcpy(make_model, nick_name->value->value, sizeof(make_model));
88f9aafc 1443 else if (_cups_strncasecmp(d->model_name->value, d->manufacturer->value,
07ed0e9a
MS
1444 strlen(d->manufacturer->value)))
1445 snprintf(make_model, sizeof(make_model), "%s %s, %s",
1446 d->manufacturer->value, d->model_name->value,
1447 d->version->value);
ef416fc2 1448 else
07ed0e9a
MS
1449 snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value,
1450 d->version->value);
ef416fc2 1451
07ed0e9a 1452 if ((cups_fax = d->find_attr("cupsFax", NULL)) != NULL &&
88f9aafc 1453 !_cups_strcasecmp(cups_fax->value->value, "true"))
07ed0e9a
MS
1454 type = PPD_TYPE_FAX;
1455 else if (d->type == PPDC_DRIVER_PS)
1456 type = PPD_TYPE_POSTSCRIPT;
1457 else if (d->type != PPDC_DRIVER_CUSTOM)
1458 type = PPD_TYPE_RASTER;
1459 else
cc754834 1460 {
07ed0e9a
MS
1461 for (filter = (ppdcFilter *)d->filters->first(),
1462 type = PPD_TYPE_POSTSCRIPT;
1463 filter;
1464 filter = (ppdcFilter *)d->filters->next())
88f9aafc 1465 if (_cups_strcasecmp(filter->mime_type->value, "application/vnd.cups-raster"))
07ed0e9a 1466 type = PPD_TYPE_RASTER;
88f9aafc 1467 else if (_cups_strcasecmp(filter->mime_type->value,
07ed0e9a
MS
1468 "application/vnd.cups-pdf"))
1469 type = PPD_TYPE_PDF;
cc754834 1470 }
ef416fc2 1471
07ed0e9a
MS
1472 for (product = (ppdcAttr *)d->attrs->first(), products_found = 0,
1473 ppd = NULL;
1474 product;
1475 product = (ppdcAttr *)d->attrs->next())
1476 if (!strcmp(product->name->value, "Product"))
1477 {
1478 if (!products_found)
1479 ppd = add_ppd(name, uri, "en", d->manufacturer->value, make_model,
1480 device_id ? device_id->value->value : "",
1481 product->value->value,
1482 ps_version ? ps_version->value->value : "(3010) 0",
1483 mtime, size, d->model_number, type, "drv");
1484 else if (products_found < PPD_MAX_PROD)
1485 strlcpy(ppd->record.products[products_found], product->value->value,
1486 sizeof(ppd->record.products[0]));
1487 else
1488 break;
ef416fc2 1489
07ed0e9a
MS
1490 products_found ++;
1491 }
ef416fc2 1492
07ed0e9a
MS
1493 if (!products_found)
1494 add_ppd(name, uri, "en", d->manufacturer->value, make_model,
1495 device_id ? device_id->value->value : "",
1496 d->model_name->value,
1497 ps_version ? ps_version->value->value : "(3010) 0",
1498 mtime, size, d->model_number, type, "drv");
1499 }
ef416fc2 1500
07ed0e9a 1501 src->release();
7a0cbd5e 1502
07ed0e9a
MS
1503 return (1);
1504}
66ab9486 1505
7a0cbd5e 1506
07ed0e9a
MS
1507/*
1508 * 'load_drivers()' - Load driver-generated PPD files.
1509 */
7a0cbd5e 1510
07ed0e9a
MS
1511static int /* O - 1 on success, 0 on failure */
1512load_drivers(cups_array_t *include, /* I - Drivers to include */
1513 cups_array_t *exclude) /* I - Drivers to exclude */
1514{
1515 int i; /* Looping var */
1516 char *start, /* Start of value */
1517 *ptr; /* Pointer into string */
1518 const char *server_bin, /* CUPS_SERVERBIN env variable */
1519 *scheme, /* Scheme for this driver */
1520 *scheme_end; /* Pointer to end of scheme */
1521 char drivers[1024]; /* Location of driver programs */
1522 int pid; /* Process ID for driver program */
1523 cups_file_t *fp; /* Pipe to driver program */
1524 cups_dir_t *dir; /* Directory pointer */
1525 cups_dentry_t *dent; /* Directory entry */
1526 char *argv[3], /* Arguments for command */
1527 filename[1024], /* Name of driver */
1528 line[2048], /* Line from driver */
1529 name[512], /* ppd-name */
1530 make[128], /* ppd-make */
1531 make_and_model[128], /* ppd-make-and-model */
771bd8cb 1532 device_id[256], /* ppd-device-id */
07ed0e9a
MS
1533 languages[128], /* ppd-natural-language */
1534 product[128], /* ppd-product */
1535 psversion[128], /* ppd-psversion */
1536 type_str[128]; /* ppd-type */
1537 int type; /* PPD type */
1538 ppd_info_t *ppd; /* Newly added PPD */
1539
1540
1541 /*
1542 * Try opening the driver directory...
1543 */
1544
1545 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
1546 server_bin = CUPS_SERVERBIN;
1547
1548 snprintf(drivers, sizeof(drivers), "%s/driver", server_bin);
1549
1550 if ((dir = cupsDirOpen(drivers)) == NULL)
1551 {
1552 fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory "
1553 "\"%s\": %s\n",
1554 drivers, strerror(errno));
1555 return (0);
1556 }
1557
1558 /*
1559 * Loop through all of the device drivers...
1560 */
1561
1562 argv[1] = (char *)"list";
1563 argv[2] = NULL;
ef416fc2 1564
07ed0e9a
MS
1565 while ((dent = cupsDirRead(dir)) != NULL)
1566 {
ef416fc2 1567 /*
07ed0e9a 1568 * Only look at executable files...
ef416fc2 1569 */
1570
07ed0e9a 1571 if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode))
ef416fc2 1572 continue;
1573
1574 /*
07ed0e9a 1575 * Include/exclude specific drivers...
ef416fc2 1576 */
1577
07ed0e9a
MS
1578 if (exclude)
1579 {
1580 /*
1581 * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
1582 */
ef416fc2 1583
07ed0e9a
MS
1584 for (scheme = (char *)cupsArrayFirst(exclude);
1585 scheme;
1586 scheme = (char *)cupsArrayNext(exclude))
1587 {
1588 fprintf(stderr, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
1589 dent->filename, scheme);
1590 scheme_end = scheme + strlen(scheme) - 1;
1591
1592 if ((scheme_end > scheme && *scheme_end == '*' &&
1593 !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
1594 !strcmp(scheme, dent->filename))
1595 {
1596 fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr);
1597 break;
1598 }
1599 }
1600
1601 if (scheme)
1602 continue;
1603 }
1604
1605 if (include)
ef416fc2 1606 {
1607 /*
07ed0e9a 1608 * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
ef416fc2 1609 */
1610
07ed0e9a
MS
1611 for (scheme = (char *)cupsArrayFirst(include);
1612 scheme;
1613 scheme = (char *)cupsArrayNext(include))
1614 {
1615 fprintf(stderr, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
1616 dent->filename, scheme);
1617 scheme_end = scheme + strlen(scheme) - 1;
1618
1619 if ((scheme_end > scheme && *scheme_end == '*' &&
1620 !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
1621 !strcmp(scheme, dent->filename))
1622 {
1623 fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr);
1624 break;
1625 }
1626 }
1627
1628 if (!scheme)
1629 continue;
ef416fc2 1630 }
07ed0e9a
MS
1631 else
1632 scheme = dent->filename;
ef416fc2 1633
1634 /*
07ed0e9a 1635 * Run the driver with no arguments and collect the output...
ef416fc2 1636 */
1637
07ed0e9a 1638 snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename);
ef416fc2 1639
22c9029b
MS
1640 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_PROGRAM, !geteuid(),
1641 _cupsFileCheckFilter, NULL))
1642 continue;
1643
1644 argv[0] = dent->filename;
1645
07ed0e9a 1646 if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL)
ef416fc2 1647 {
07ed0e9a 1648 while (cupsFileGets(fp, line, sizeof(line)))
58dc1933 1649 {
07ed0e9a
MS
1650 /*
1651 * Each line is of the form:
1652 *
1653 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
1654 * "ppd-device-id" "ppd-product" "ppd-psversion"
1655 */
58dc1933 1656
07ed0e9a
MS
1657 device_id[0] = '\0';
1658 product[0] = '\0';
1659 psversion[0] = '\0';
1660 strcpy(type_str, "postscript");
1661
1662 if (sscanf(line, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
771bd8cb 1663 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%255[^\"]\""
07ed0e9a
MS
1664 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1665 "%*[ \t]\"%127[^\"]\"",
1666 name, languages, make, make_and_model,
1667 device_id, product, psversion, type_str) < 4)
1668 {
1669 /*
1670 * Bad format; strip trailing newline and write an error message.
1671 */
1672
1673 if (line[strlen(line) - 1] == '\n')
1674 line[strlen(line) - 1] = '\0';
1675
1676 fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1677 dent->filename, line);
1678 break;
1679 }
1680 else
c168a833
MS
1681 {
1682 /*
07ed0e9a 1683 * Add the device to the array of available devices...
c168a833
MS
1684 */
1685
07ed0e9a
MS
1686 if ((start = strchr(languages, ',')) != NULL)
1687 *start++ = '\0';
c168a833 1688
07ed0e9a
MS
1689 for (type = 0;
1690 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
1691 type ++)
1692 if (!strcmp(type_str, ppd_types[type]))
1693 break;
b94498cf 1694
07ed0e9a
MS
1695 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
1696 {
1697 fprintf(stderr,
1698 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
1699 type_str);
1700 type = PPD_TYPE_UNKNOWN;
1701 }
b94498cf 1702
07ed0e9a
MS
1703 ppd = add_ppd(filename, name, languages, make, make_and_model,
1704 device_id, product, psversion, 0, 0, 0, type, scheme);
b94498cf 1705
07ed0e9a 1706 if (!ppd)
b94498cf 1707 {
07ed0e9a
MS
1708 cupsDirClose(dir);
1709 cupsFileClose(fp);
1710 return (0);
1711 }
b94498cf 1712
07ed0e9a
MS
1713 if (start && *start)
1714 {
1715 for (i = 1; i < PPD_MAX_LANG && *start; i ++)
b94498cf 1716 {
07ed0e9a 1717 if ((ptr = strchr(start, ',')) != NULL)
b94498cf 1718 *ptr++ = '\0';
07ed0e9a
MS
1719 else
1720 ptr = start + strlen(start);
b94498cf 1721
07ed0e9a
MS
1722 strlcpy(ppd->record.languages[i], start,
1723 sizeof(ppd->record.languages[0]));
3d8365b8 1724
07ed0e9a
MS
1725 start = ptr;
1726 }
1727 }
ef416fc2 1728
07ed0e9a
MS
1729 fprintf(stderr, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1730 name);
1731 }
ef416fc2 1732 }
07ed0e9a
MS
1733
1734 cupsFileClose(fp);
ef416fc2 1735 }
07ed0e9a
MS
1736 else
1737 fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1738 filename, strerror(errno));
1739 }
ef416fc2 1740
07ed0e9a 1741 cupsDirClose(dir);
ef416fc2 1742
07ed0e9a
MS
1743 return (1);
1744}
ef416fc2 1745
ef416fc2 1746
07ed0e9a
MS
1747/*
1748 * 'load_ppds()' - Load PPD files recursively.
1749 */
ef416fc2 1750
07ed0e9a
MS
1751static int /* O - 1 on success, 0 on failure */
1752load_ppds(const char *d, /* I - Actual directory */
1753 const char *p, /* I - Virtual path in name */
1754 int descend) /* I - Descend into directories? */
1755{
1756 struct stat dinfo, /* Directory information */
1757 *dinfoptr; /* Pointer to match */
1758 int i; /* Looping var */
1759 cups_file_t *fp; /* Pointer to file */
1760 cups_dir_t *dir; /* Directory pointer */
1761 cups_dentry_t *dent; /* Directory entry */
1762 char filename[1024], /* Name of PPD or directory */
1763 line[256], /* Line from backend */
1764 *ptr, /* Pointer into name */
1765 name[128], /* Name of PPD file */
1766 lang_version[64], /* PPD LanguageVersion */
1767 lang_encoding[64], /* PPD LanguageEncoding */
1768 country[64], /* Country code */
1769 manufacturer[256], /* Manufacturer */
1770 make_model[256], /* Make and Model */
1771 model_name[256], /* ModelName */
1772 nick_name[256], /* NickName */
1773 device_id[256], /* 1284DeviceID */
1774 product[256], /* Product */
1775 psversion[256], /* PSVersion */
1776 temp[512]; /* Temporary make and model */
a2326b5b
MS
1777 int install_group, /* In the installable options group? */
1778 model_number, /* cupsModelNumber */
07ed0e9a
MS
1779 type; /* ppd-type */
1780 cups_array_t *products, /* Product array */
1781 *psversions, /* PSVersion array */
1782 *cups_languages; /* cupsLanguages array */
1783 ppd_info_t *ppd, /* New PPD file */
1784 key; /* Search key */
1785 int new_ppd; /* Is this a new PPD? */
1786 struct /* LanguageVersion translation table */
1787 {
1788 const char *version, /* LanguageVersion string */
1789 *language; /* Language code */
1790 } languages[] =
1791 {
1792 { "chinese", "zh" },
1793 { "czech", "cs" },
1794 { "danish", "da" },
1795 { "dutch", "nl" },
1796 { "english", "en" },
1797 { "finnish", "fi" },
1798 { "french", "fr" },
1799 { "german", "de" },
1800 { "greek", "el" },
1801 { "hungarian", "hu" },
1802 { "italian", "it" },
1803 { "japanese", "ja" },
1804 { "korean", "ko" },
1805 { "norwegian", "no" },
1806 { "polish", "pl" },
1807 { "portuguese", "pt" },
1808 { "russian", "ru" },
1809 { "simplified chinese", "zh_CN" },
1810 { "slovak", "sk" },
1811 { "spanish", "es" },
1812 { "swedish", "sv" },
1813 { "traditional chinese", "zh_TW" },
1814 { "turkish", "tr" }
1815 };
ef416fc2 1816
f899b121 1817
07ed0e9a
MS
1818 /*
1819 * See if we've loaded this directory before...
1820 */
f899b121 1821
07ed0e9a
MS
1822 if (stat(d, &dinfo))
1823 {
1824 if (errno != ENOENT)
1825 fprintf(stderr, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d,
1826 strerror(errno));
f899b121 1827
07ed0e9a
MS
1828 return (0);
1829 }
1830 else if (cupsArrayFind(Inodes, &dinfo))
1831 {
1832 fprintf(stderr, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
1833 d);
1834 return (0);
1835 }
f899b121 1836
07ed0e9a
MS
1837 /*
1838 * Nope, add it to the Inodes array and continue...
1839 */
f899b121 1840
07ed0e9a
MS
1841 dinfoptr = (struct stat *)malloc(sizeof(struct stat));
1842 memcpy(dinfoptr, &dinfo, sizeof(struct stat));
1843 cupsArrayAdd(Inodes, dinfoptr);
ef416fc2 1844
22c9029b
MS
1845 /*
1846 * Check permissions...
1847 */
1848
1849 if (_cupsFileCheck(d, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
1850 _cupsFileCheckFilter, NULL))
1851 return (0);
1852
07ed0e9a
MS
1853 if ((dir = cupsDirOpen(d)) == NULL)
1854 {
1855 if (errno != ENOENT)
1856 fprintf(stderr,
1857 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1858 d, strerror(errno));
3d8365b8 1859
07ed0e9a
MS
1860 return (0);
1861 }
ef416fc2 1862
07ed0e9a 1863 fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d);
ef416fc2 1864
07ed0e9a
MS
1865 while ((dent = cupsDirRead(dir)) != NULL)
1866 {
1867 /*
1868 * Skip files/directories starting with "."...
1869 */
5eb9da71 1870
07ed0e9a
MS
1871 if (dent->filename[0] == '.')
1872 continue;
5eb9da71
MS
1873
1874 /*
07ed0e9a 1875 * See if this is a file...
5eb9da71
MS
1876 */
1877
07ed0e9a
MS
1878 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
1879
1880 if (p[0])
1881 snprintf(name, sizeof(name), "%s/%s", p, dent->filename);
1882 else
1883 strlcpy(name, dent->filename, sizeof(name));
1884
1885 if (S_ISDIR(dent->fileinfo.st_mode))
ef416fc2 1886 {
1887 /*
07ed0e9a 1888 * Do subdirectory...
ef416fc2 1889 */
1890
07ed0e9a 1891 if (descend)
22c9029b 1892 {
07ed0e9a
MS
1893 if (!load_ppds(filename, name, 1))
1894 {
1895 cupsDirClose(dir);
1896 return (1);
1897 }
22c9029b
MS
1898 }
1899 else if ((ptr = filename + strlen(filename) - 14) > filename &&
1900 !strcmp(ptr, ".printerDriver"))
1901 {
1902 /*
1903 * Load PPDs in a printer driver bundle.
1904 */
1905
1906 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
1907 _cupsFileCheckFilter, NULL))
1908 continue;
1909
1910 strlcat(filename, "/Contents/Resources/PPDs", sizeof(filename));
1911 strlcat(name, "/Contents/Resources/PPDs", sizeof(name));
1912
1913 load_ppds(filename, name, 0);
1914 }
ef416fc2 1915
07ed0e9a
MS
1916 continue;
1917 }
1918 else if ((ptr = filename + strlen(filename) - 6) > filename &&
1919 !strcmp(ptr, ".plist"))
1920 {
ef416fc2 1921 /*
07ed0e9a 1922 * Skip plist files in the PPDs directory...
ef416fc2 1923 */
1924
07ed0e9a 1925 continue;
ef416fc2 1926 }
22c9029b
MS
1927 else if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_FILE_ONLY, !geteuid(),
1928 _cupsFileCheckFilter, NULL))
1929 continue;
ef416fc2 1930
1931 /*
07ed0e9a 1932 * See if this file has been scanned before...
ef416fc2 1933 */
1934
07ed0e9a
MS
1935 strcpy(key.record.filename, name);
1936 strcpy(key.record.name, name);
ef416fc2 1937
07ed0e9a
MS
1938 ppd = (ppd_info_t *)cupsArrayFind(PPDsByName, &key);
1939
1940 if (ppd &&
1941 ppd->record.size == dent->fileinfo.st_size &&
1942 ppd->record.mtime == dent->fileinfo.st_mtime)
ef416fc2 1943 {
1944 /*
07ed0e9a 1945 * Rewind to the first entry for this file...
ef416fc2 1946 */
1947
07ed0e9a
MS
1948 while ((ppd = (ppd_info_t *)cupsArrayPrev(PPDsByName)) != NULL &&
1949 !strcmp(ppd->record.filename, name));
1950
ef416fc2 1951 /*
07ed0e9a 1952 * Then mark all of the matches for this file as found...
ef416fc2 1953 */
1954
07ed0e9a
MS
1955 while ((ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) != NULL &&
1956 !strcmp(ppd->record.filename, name))
1957 ppd->found = 1;
ef416fc2 1958
07ed0e9a 1959 continue;
ef416fc2 1960 }
1961
1962 /*
07ed0e9a 1963 * No, file is new/changed, so re-scan it...
ef416fc2 1964 */
1965
07ed0e9a
MS
1966 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1967 continue;
ef416fc2 1968
07ed0e9a
MS
1969 /*
1970 * Now see if this is a PPD file...
1971 */
ef416fc2 1972
07ed0e9a
MS
1973 line[0] = '\0';
1974 cupsFileGets(fp, line, sizeof(line));
b94498cf 1975
07ed0e9a 1976 if (strncmp(line, "*PPD-Adobe:", 11))
ef416fc2 1977 {
1978 /*
07ed0e9a 1979 * Nope, treat it as a driver information file...
ef416fc2 1980 */
1981
07ed0e9a
MS
1982 load_drv(filename, name, fp, dent->fileinfo.st_mtime,
1983 dent->fileinfo.st_size);
1984 continue;
ef416fc2 1985 }
1986
f899b121 1987 /*
07ed0e9a 1988 * Now read until we get the NickName field...
b94498cf 1989 */
1990
07ed0e9a
MS
1991 cups_languages = cupsArrayNew(NULL, NULL);
1992 products = cupsArrayNew(NULL, NULL);
1993 psversions = cupsArrayNew(NULL, NULL);
4509bb49 1994
07ed0e9a
MS
1995 model_name[0] = '\0';
1996 nick_name[0] = '\0';
1997 manufacturer[0] = '\0';
1998 device_id[0] = '\0';
1999 lang_encoding[0] = '\0';
2000 strcpy(lang_version, "en");
2001 model_number = 0;
a2326b5b 2002 install_group = 0;
07ed0e9a 2003 type = PPD_TYPE_POSTSCRIPT;
4509bb49 2004
07ed0e9a
MS
2005 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
2006 {
2007 if (!strncmp(line, "*Manufacturer:", 14))
2008 sscanf(line, "%*[^\"]\"%255[^\"]", manufacturer);
2009 else if (!strncmp(line, "*ModelName:", 11))
2010 sscanf(line, "%*[^\"]\"%127[^\"]", model_name);
2011 else if (!strncmp(line, "*LanguageEncoding:", 18))
2012 sscanf(line, "%*[^:]:%63s", lang_encoding);
2013 else if (!strncmp(line, "*LanguageVersion:", 17))
2014 sscanf(line, "%*[^:]:%63s", lang_version);
2015 else if (!strncmp(line, "*NickName:", 10))
2016 sscanf(line, "%*[^\"]\"%255[^\"]", nick_name);
88f9aafc 2017 else if (!_cups_strncasecmp(line, "*1284DeviceID:", 14))
07ed0e9a
MS
2018 {
2019 sscanf(line, "%*[^\"]\"%255[^\"]", device_id);
4509bb49 2020
07ed0e9a
MS
2021 // Make sure device ID ends with a semicolon...
2022 if (device_id[0] && device_id[strlen(device_id) - 1] != ';')
2023 strlcat(device_id, ";", sizeof(device_id));
2024 }
2025 else if (!strncmp(line, "*Product:", 9))
2026 {
2027 if (sscanf(line, "%*[^\"]\"(%255[^\"]", product) == 1)
2028 {
2029 /*
2030 * Make sure the value ends with a right parenthesis - can't stop at
2031 * the first right paren since the product name may contain escaped
2032 * parenthesis...
2033 */
66ab9486 2034
07ed0e9a
MS
2035 ptr = product + strlen(product) - 1;
2036 if (ptr > product && *ptr == ')')
2037 {
2038 /*
2039 * Yes, ends with a parenthesis, so remove it from the end and
2040 * add the product to the list...
2041 */
4509bb49 2042
07ed0e9a
MS
2043 *ptr = '\0';
2044 cupsArrayAdd(products, strdup(product));
2045 }
2046 }
2047 }
2048 else if (!strncmp(line, "*PSVersion:", 11))
2049 {
2050 sscanf(line, "%*[^\"]\"%255[^\"]", psversion);
2051 cupsArrayAdd(psversions, strdup(psversion));
2052 }
2053 else if (!strncmp(line, "*cupsLanguages:", 15))
2054 {
2055 char *start; /* Start of language */
4509bb49 2056
66ab9486 2057
07ed0e9a 2058 for (start = line + 15; *start && isspace(*start & 255); start ++);
66ab9486 2059
07ed0e9a
MS
2060 if (*start++ == '\"')
2061 {
2062 while (*start)
2063 {
2064 for (ptr = start + 1;
2065 *ptr && *ptr != '\"' && !isspace(*ptr & 255);
2066 ptr ++);
66ab9486 2067
07ed0e9a
MS
2068 if (*ptr)
2069 {
2070 *ptr++ = '\0';
4509bb49 2071
07ed0e9a
MS
2072 while (isspace(*ptr & 255))
2073 *ptr++ = '\0';
2074 }
4509bb49 2075
07ed0e9a
MS
2076 cupsArrayAdd(cups_languages, strdup(start));
2077 start = ptr;
2078 }
2079 }
2080 }
2081 else if (!strncmp(line, "*cupsFax:", 9))
2082 {
2083 for (ptr = line + 9; isspace(*ptr & 255); ptr ++);
4509bb49 2084
88f9aafc 2085 if (!_cups_strncasecmp(ptr, "true", 4))
07ed0e9a
MS
2086 type = PPD_TYPE_FAX;
2087 }
2088 else if (!strncmp(line, "*cupsFilter:", 12) && type == PPD_TYPE_POSTSCRIPT)
2089 {
2090 if (strstr(line + 12, "application/vnd.cups-raster"))
4509bb49 2091 type = PPD_TYPE_RASTER;
07ed0e9a 2092 else if (strstr(line + 12, "application/vnd.cups-pdf"))
4509bb49 2093 type = PPD_TYPE_PDF;
07ed0e9a
MS
2094 }
2095 else if (!strncmp(line, "*cupsModelNumber:", 17))
2096 sscanf(line, "*cupsModelNumber:%d", &model_number);
a2326b5b
MS
2097 else if (!strncmp(line, "*OpenGroup: Installable", 23))
2098 install_group = 1;
2099 else if (!strncmp(line, "*CloseGroup:", 12))
2100 install_group = 0;
07ed0e9a 2101 else if (!strncmp(line, "*OpenUI", 7))
4509bb49 2102 {
07ed0e9a
MS
2103 /*
2104 * Stop early if we have a NickName or ModelName attributes
a2326b5b 2105 * before the first non-installable OpenUI...
07ed0e9a 2106 */
4509bb49 2107
a2326b5b
MS
2108 if (!install_group && (model_name[0] || nick_name[0]) &&
2109 cupsArrayCount(products) > 0 && cupsArrayCount(psversions) > 0)
07ed0e9a 2110 break;
4509bb49 2111 }
07ed0e9a 2112 }
4509bb49 2113
07ed0e9a
MS
2114 /*
2115 * Close the file...
2116 */
4509bb49 2117
07ed0e9a 2118 cupsFileClose(fp);
4509bb49 2119
07ed0e9a
MS
2120 /*
2121 * See if we got all of the required info...
2122 */
4509bb49 2123
07ed0e9a
MS
2124 if (nick_name[0])
2125 cupsCharsetToUTF8((cups_utf8_t *)make_model, nick_name,
2126 sizeof(make_model), _ppdGetEncoding(lang_encoding));
2127 else
2128 strcpy(make_model, model_name);
ef416fc2 2129
07ed0e9a
MS
2130 while (isspace(make_model[0] & 255))
2131 _cups_strcpy(make_model, make_model + 1);
ef416fc2 2132
07ed0e9a
MS
2133 if (!make_model[0] || cupsArrayCount(products) == 0 ||
2134 cupsArrayCount(psversions) == 0)
2135 {
2136 /*
2137 * We don't have all the info needed, so skip this file...
2138 */
ef416fc2 2139
07ed0e9a
MS
2140 if (!make_model[0])
2141 fprintf(stderr, "WARNING: Missing NickName and ModelName in %s!\n",
2142 filename);
ef416fc2 2143
07ed0e9a
MS
2144 if (cupsArrayCount(products) == 0)
2145 fprintf(stderr, "WARNING: Missing Product in %s!\n", filename);
ef416fc2 2146
07ed0e9a
MS
2147 if (cupsArrayCount(psversions) == 0)
2148 fprintf(stderr, "WARNING: Missing PSVersion in %s!\n", filename);
ef416fc2 2149
07ed0e9a
MS
2150 free_array(products);
2151 free_array(psversions);
2152 free_array(cups_languages);
ef416fc2 2153
07ed0e9a
MS
2154 continue;
2155 }
ef416fc2 2156
07ed0e9a
MS
2157 if (model_name[0])
2158 cupsArrayAdd(products, strdup(model_name));
c934a06c 2159
ef416fc2 2160 /*
07ed0e9a 2161 * Normalize the make and model string...
ef416fc2 2162 */
2163
07ed0e9a
MS
2164 while (isspace(manufacturer[0] & 255))
2165 _cups_strcpy(manufacturer, manufacturer + 1);
2166
88f9aafc 2167 if (!_cups_strncasecmp(make_model, manufacturer, strlen(manufacturer)))
07ed0e9a
MS
2168 strlcpy(temp, make_model, sizeof(temp));
2169 else
2170 snprintf(temp, sizeof(temp), "%s %s", manufacturer, make_model);
2171
2172 _ppdNormalizeMakeAndModel(temp, make_model, sizeof(make_model));
ef416fc2 2173
ed6e7faf 2174 /*
07ed0e9a 2175 * See if we got a manufacturer...
ed6e7faf
MS
2176 */
2177
07ed0e9a 2178 if (!manufacturer[0] || !strcmp(manufacturer, "ESP"))
8b450588
MS
2179 {
2180 /*
07ed0e9a 2181 * Nope, copy the first part of the make and model then...
8b450588
MS
2182 */
2183
07ed0e9a 2184 strlcpy(manufacturer, make_model, sizeof(manufacturer));
8b450588 2185
07ed0e9a
MS
2186 /*
2187 * Truncate at the first space, dash, or slash, or make the
2188 * manufacturer "Other"...
2189 */
2190
2191 for (ptr = manufacturer; *ptr; ptr ++)
2192 if (*ptr == ' ' || *ptr == '-' || *ptr == '/')
8b450588 2193 break;
8b450588 2194
07ed0e9a
MS
2195 if (*ptr && ptr > manufacturer)
2196 *ptr = '\0';
2197 else
2198 strcpy(manufacturer, "Other");
8b450588 2199 }
88f9aafc
MS
2200 else if (!_cups_strncasecmp(manufacturer, "LHAG", 4) ||
2201 !_cups_strncasecmp(manufacturer, "linotype", 8))
07ed0e9a 2202 strcpy(manufacturer, "LHAG");
88f9aafc 2203 else if (!_cups_strncasecmp(manufacturer, "Hewlett", 7))
07ed0e9a 2204 strcpy(manufacturer, "HP");
8b450588 2205
07ed0e9a
MS
2206 /*
2207 * Fix the lang_version as needed...
2208 */
2209
2210 if ((ptr = strchr(lang_version, '-')) != NULL)
2211 *ptr++ = '\0';
2212 else if ((ptr = strchr(lang_version, '_')) != NULL)
2213 *ptr++ = '\0';
2214
2215 if (ptr)
8b450588
MS
2216 {
2217 /*
07ed0e9a 2218 * Setup the country suffix...
8b450588
MS
2219 */
2220
07ed0e9a
MS
2221 country[0] = '_';
2222 _cups_strcpy(country + 1, ptr);
2223 }
2224 else
2225 {
2226 /*
2227 * No country suffix...
2228 */
8b450588 2229
07ed0e9a
MS
2230 country[0] = '\0';
2231 }
8b450588 2232
07ed0e9a 2233 for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++)
88f9aafc 2234 if (!_cups_strcasecmp(languages[i].version, lang_version))
07ed0e9a
MS
2235 break;
2236
2237 if (i < (int)(sizeof(languages) / sizeof(languages[0])))
2238 {
2239 /*
2240 * Found a known language...
2241 */
2242
2243 snprintf(lang_version, sizeof(lang_version), "%s%s",
2244 languages[i].language, country);
8b450588
MS
2245 }
2246 else
07ed0e9a
MS
2247 {
2248 /*
2249 * Unknown language; use "xx"...
2250 */
2251
2252 strcpy(lang_version, "xx");
2253 }
ed6e7faf 2254
ef416fc2 2255 /*
07ed0e9a 2256 * Record the PPD file...
ef416fc2 2257 */
2258
07ed0e9a 2259 new_ppd = !ppd;
c934a06c 2260
07ed0e9a 2261 if (new_ppd)
ef416fc2 2262 {
07ed0e9a
MS
2263 /*
2264 * Add new PPD file...
2265 */
ef416fc2 2266
07ed0e9a 2267 fprintf(stderr, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name);
bd7854cb 2268
07ed0e9a
MS
2269 ppd = add_ppd(name, name, lang_version, manufacturer, make_model,
2270 device_id, (char *)cupsArrayFirst(products),
2271 (char *)cupsArrayFirst(psversions),
2272 dent->fileinfo.st_mtime, dent->fileinfo.st_size,
2273 model_number, type, "file");
ef416fc2 2274
07ed0e9a
MS
2275 if (!ppd)
2276 {
2277 cupsDirClose(dir);
2278 return (0);
2279 }
2280 }
2281 else
2282 {
2283 /*
2284 * Update existing record...
2285 */
ef416fc2 2286
07ed0e9a 2287 fprintf(stderr, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name);
ef416fc2 2288
07ed0e9a 2289 memset(ppd, 0, sizeof(ppd_info_t));
b94498cf 2290
07ed0e9a
MS
2291 ppd->found = 1;
2292 ppd->record.mtime = dent->fileinfo.st_mtime;
2293 ppd->record.size = dent->fileinfo.st_size;
2294 ppd->record.model_number = model_number;
2295 ppd->record.type = type;
3d8365b8 2296
07ed0e9a
MS
2297 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
2298 strlcpy(ppd->record.make, manufacturer, sizeof(ppd->record.make));
2299 strlcpy(ppd->record.make_and_model, make_model,
2300 sizeof(ppd->record.make_and_model));
2301 strlcpy(ppd->record.languages[0], lang_version,
2302 sizeof(ppd->record.languages[0]));
2303 strlcpy(ppd->record.products[0], (char *)cupsArrayFirst(products),
2304 sizeof(ppd->record.products[0]));
2305 strlcpy(ppd->record.psversions[0], (char *)cupsArrayFirst(psversions),
2306 sizeof(ppd->record.psversions[0]));
2307 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
2308 }
3d8365b8 2309
07ed0e9a
MS
2310 /*
2311 * Add remaining products, versions, and languages...
2312 */
b94498cf 2313
07ed0e9a
MS
2314 for (i = 1;
2315 i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL;
2316 i ++)
2317 strlcpy(ppd->record.products[i], ptr,
2318 sizeof(ppd->record.products[0]));
ef416fc2 2319
07ed0e9a
MS
2320 for (i = 1;
2321 i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL;
2322 i ++)
2323 strlcpy(ppd->record.psversions[i], ptr,
2324 sizeof(ppd->record.psversions[0]));
b94498cf 2325
07ed0e9a
MS
2326 for (i = 1, ptr = (char *)cupsArrayFirst(cups_languages);
2327 i < PPD_MAX_LANG && ptr;
2328 i ++, ptr = (char *)cupsArrayNext(cups_languages))
2329 strlcpy(ppd->record.languages[i], ptr,
2330 sizeof(ppd->record.languages[0]));
b94498cf 2331
07ed0e9a
MS
2332 /*
2333 * Free products, versions, and languages...
2334 */
b94498cf 2335
07ed0e9a
MS
2336 free_array(cups_languages);
2337 free_array(products);
2338 free_array(psversions);
ef416fc2 2339
07ed0e9a 2340 ChangedPPD = 1;
ef416fc2 2341 }
2342
2343 cupsDirClose(dir);
2344
2345 return (1);
2346}
2347
2348
f0ab5bff
MS
2349/*
2350 * 'load_ppds_dat()' - Load the ppds.dat file.
2351 */
2352
2353static void
2354load_ppds_dat(char *filename, /* I - Filename buffer */
2355 size_t filesize, /* I - Size of filename buffer */
2356 int verbose) /* I - Be verbose? */
2357{
2358 ppd_info_t *ppd; /* Current PPD file */
2359 cups_file_t *fp; /* ppds.dat file */
2360 struct stat fileinfo; /* ppds.dat information */
2361 const char *cups_cachedir; /* CUPS_CACHEDIR environment variable */
2362
2363
2364 PPDsByName = cupsArrayNew((cups_array_func_t)compare_names, NULL);
2365 PPDsByMakeModel = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
2366 ChangedPPD = 0;
2367
2368 if ((cups_cachedir = getenv("CUPS_CACHEDIR")) == NULL)
2369 cups_cachedir = CUPS_CACHEDIR;
2370
2371 snprintf(filename, filesize, "%s/ppds.dat", cups_cachedir);
2372 if ((fp = cupsFileOpen(filename, "r")) != NULL)
2373 {
2374 /*
2375 * See if we have the right sync word...
2376 */
2377
2378 unsigned ppdsync; /* Sync word */
2379 int num_ppds; /* Number of PPDs */
2380
f0ab5bff
MS
2381 if (cupsFileRead(fp, (char *)&ppdsync, sizeof(ppdsync))
2382 == sizeof(ppdsync) &&
2383 ppdsync == PPD_SYNC &&
2384 !stat(filename, &fileinfo) &&
2385 ((fileinfo.st_size - sizeof(ppdsync)) % sizeof(ppd_rec_t)) == 0 &&
2386 (num_ppds = (fileinfo.st_size - sizeof(ppdsync)) /
2387 sizeof(ppd_rec_t)) > 0)
2388 {
2389 /*
2390 * We have a ppds.dat file, so read it!
2391 */
2392
2393 for (; num_ppds > 0; num_ppds --)
2394 {
2395 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
2396 {
2397 if (verbose)
2398 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
2399 stderr);
2400 exit(1);
2401 }
2402
2403 if (cupsFileRead(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)) > 0)
2404 {
2405 cupsArrayAdd(PPDsByName, ppd);
2406 cupsArrayAdd(PPDsByMakeModel, ppd);
2407 }
2408 else
2409 {
2410 free(ppd);
2411 break;
2412 }
2413 }
2414
2415 if (verbose)
2416 fprintf(stderr, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
2417 filename, cupsArrayCount(PPDsByName));
2418 }
2419
2420 cupsFileClose(fp);
2421 }
2422}
2423
2424
58dc1933
MS
2425/*
2426 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2427 * ID.
2428 */
2429
2430static regex_t * /* O - Regular expression */
2431regex_device_id(const char *device_id) /* I - IEEE-1284 device ID */
2432{
2433 char res[2048], /* Regular expression string */
2434 *ptr; /* Pointer into string */
2435 regex_t *re; /* Regular expression */
2436 int cmd; /* Command set string? */
2437
2438
2439 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id);
2440
2441 /*
2442 * Scan the device ID string and insert class, command set, manufacturer, and
2443 * model attributes to match. We assume that the device ID in the PPD and the
2444 * device ID reported by the device itself use the same attribute names and
2445 * order of attributes.
2446 */
2447
2448 ptr = res;
2449
2450 while (*device_id && ptr < (res + sizeof(res) - 6))
2451 {
88f9aafc
MS
2452 cmd = !_cups_strncasecmp(device_id, "COMMAND SET:", 12) ||
2453 !_cups_strncasecmp(device_id, "CMD:", 4);
2454
2455 if (cmd || !_cups_strncasecmp(device_id, "MANUFACTURER:", 13) ||
2456 !_cups_strncasecmp(device_id, "MFG:", 4) ||
2457 !_cups_strncasecmp(device_id, "MFR:", 4) ||
2458 !_cups_strncasecmp(device_id, "MODEL:", 6) ||
2459 !_cups_strncasecmp(device_id, "MDL:", 4))
58dc1933
MS
2460 {
2461 if (ptr > res)
2462 {
2463 *ptr++ = '.';
2464 *ptr++ = '*';
2465 }
2466
2467 *ptr++ = '(';
2468
f14324a7 2469 while (*device_id && *device_id != ';' && ptr < (res + sizeof(res) - 8))
58dc1933
MS
2470 {
2471 if (strchr("[]{}().*\\|", *device_id))
2472 *ptr++ = '\\';
f14324a7
MS
2473 if (*device_id == ':')
2474 {
2475 /*
2476 * KEY:.*value
2477 */
2478
2479 *ptr++ = *device_id++;
2480 *ptr++ = '.';
2481 *ptr++ = '*';
2482 }
2483 else
2484 *ptr++ = *device_id++;
58dc1933
MS
2485 }
2486
2487 if (*device_id == ';' || !*device_id)
f14324a7
MS
2488 {
2489 /*
2490 * KEY:.*value.*;
2491 */
2492
2493 *ptr++ = '.';
2494 *ptr++ = '*';
58dc1933 2495 *ptr++ = ';';
f14324a7 2496 }
58dc1933
MS
2497 *ptr++ = ')';
2498 if (cmd)
2499 *ptr++ = '?';
2500 }
2501 else if ((device_id = strchr(device_id, ';')) == NULL)
2502 break;
2503 else
2504 device_id ++;
2505 }
2506
2507 *ptr = '\0';
2508
2509 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res);
2510
2511 /*
2512 * Compile the regular expression and return...
2513 */
2514
2515 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2516 {
2517 if (!regcomp(re, res, REG_EXTENDED | REG_ICASE))
2518 {
2519 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr);
2520 return (re);
2521 }
2522
2523 free(re);
2524 }
2525
2526 return (NULL);
2527}
2528
2529
2530/*
2531 * 'regex_string()' - Construct a regular expression to compare a simple string.
2532 */
2533
2534static regex_t * /* O - Regular expression */
2535regex_string(const char *s) /* I - String to compare */
2536{
2537 char res[2048], /* Regular expression string */
2538 *ptr; /* Pointer into string */
2539 regex_t *re; /* Regular expression */
2540
2541
2542 fprintf(stderr, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s);
2543
2544 /*
2545 * Convert the string to a regular expression, escaping special characters
2546 * as needed.
2547 */
2548
2549 ptr = res;
2550
2551 while (*s && ptr < (res + sizeof(res) - 2))
2552 {
2553 if (strchr("[]{}().*\\", *s))
2554 *ptr++ = '\\';
2555
2556 *ptr++ = *s++;
2557 }
2558
2559 *ptr = '\0';
2560
2561 fprintf(stderr, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res);
2562
2563 /*
2564 * Create a case-insensitive regular expression...
2565 */
2566
2567 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2568 {
2569 if (!regcomp(re, res, REG_ICASE))
2570 {
2571 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr);
2572 return (re);
2573 }
2574
2575 free(re);
2576 }
2577
2578 return (NULL);
2579}
2580
2581
ef416fc2 2582/*
4509bb49 2583 * End of "$Id$".
ef416fc2 2584 */