]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/cups-driverd.cxx
Merge changes from CUPS 1.5b1-r9774.
[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 */
b94498cf 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
287 cups_file_t *out; // Stdout via CUPS file API
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
748 if ((diff = strcasecmp(p0->record.make, p1->record.make)) != 0)
749 return (diff);
750 else if ((diff = cupsdCompareNames(p0->record.make_and_model,
751 p1->record.make_and_model)) != 0)
752 return (diff);
753 else
d1c13e16 754 return (strcmp(p0->record.languages[0], p1->record.languages[0]));
b94498cf 755}
756
757
f0ab5bff
MS
758/*
759 * 'dump_ppds_dat()' - Dump the contents of the ppds.dat file.
760 */
761
762static int /* O - Exit status */
763dump_ppds_dat(void)
764{
765 char filename[1024]; /* ppds.dat filename */
766 ppd_info_t *ppd; /* Current PPD */
767
768
769 /*
770 * See if we a PPD database file...
771 */
772
773 load_ppds_dat(filename, sizeof(filename), 0);
774
775 puts("mtime,size,model_number,type,filename,name,languages0,products0,"
776 "psversions0,make,make_and_model,device_id,scheme");
777 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
778 ppd;
779 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
780 printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
781 "\"%s\",\"%s\"\n",
782 (int)ppd->record.mtime, (long)ppd->record.size,
783 ppd->record.model_number, ppd->record.type, ppd->record.filename,
784 ppd->record.name, ppd->record.languages[0], ppd->record.products[0],
785 ppd->record.psversions[0], ppd->record.make,
786 ppd->record.make_and_model, ppd->record.device_id,
787 ppd->record.scheme);
788
789 return (0);
790}
791
792
b94498cf 793/*
794 * 'free_array()' - Free an array of strings.
795 */
796
797static void
798free_array(cups_array_t *a) /* I - Array to free */
799{
800 char *ptr; /* Pointer to string */
801
802
803 for (ptr = (char *)cupsArrayFirst(a);
804 ptr;
805 ptr = (char *)cupsArrayNext(a))
806 free(ptr);
807
808 cupsArrayDelete(a);
ef416fc2 809}
810
811
812/*
813 * 'list_ppds()' - List PPD files.
814 */
815
bd7854cb 816static int /* O - Exit code */
ef416fc2 817list_ppds(int request_id, /* I - Request ID */
818 int limit, /* I - Limit */
819 const char *opt) /* I - Option argument */
820{
66ab9486 821 int i; /* Looping vars */
ef416fc2 822 int count; /* Number of PPDs to send */
823 ppd_info_t *ppd; /* Current PPD file */
824 cups_file_t *fp; /* ppds.dat file */
ef416fc2 825 char filename[1024], /* ppds.dat filename */
826 model[1024]; /* Model directory */
ef416fc2 827 const char *cups_datadir; /* CUPS_DATADIR environment variable */
828 int num_options; /* Number of options */
829 cups_option_t *options; /* Options */
ed6e7faf
MS
830 cups_array_t *requested, /* requested-attributes values */
831 *include, /* PPD schemes to include */
832 *exclude; /* PPD schemes to exclude */
833 const char *device_id, /* ppd-device-id option */
b94498cf 834 *language, /* ppd-natural-language option */
835 *make, /* ppd-make option */
836 *make_and_model, /* ppd-make-and-model option */
3d8365b8 837 *model_number_str, /* ppd-model-number option */
b94498cf 838 *product, /* ppd-product option */
3d8365b8 839 *psversion, /* ppd-psversion option */
840 *type_str; /* ppd-type option */
841 int model_number, /* ppd-model-number value */
842 type, /* ppd-type value */
58dc1933
MS
843 make_and_model_len, /* Length of ppd-make-and-model */
844 product_len, /* Length of ppd-product */
3d8365b8 845 send_device_id, /* Send ppd-device-id? */
b94498cf 846 send_make, /* Send ppd-make? */
847 send_make_and_model, /* Send ppd-make-and-model? */
3d8365b8 848 send_model_number, /* Send ppd-model-number? */
b94498cf 849 send_name, /* Send ppd-name? */
3d8365b8 850 send_natural_language, /* Send ppd-natural-language? */
b94498cf 851 send_product, /* Send ppd-product? */
852 send_psversion, /* Send ppd-psversion? */
3d8365b8 853 send_type, /* Send ppd-type? */
b94498cf 854 sent_header; /* Sent the IPP header? */
58dc1933
MS
855 regex_t *device_id_re, /* Regular expression for matching device ID */
856 *make_and_model_re; /* Regular expression for matching make and model */
857 regmatch_t re_matches[6]; /* Regular expression matches */
858 cups_array_t *matches; /* Matching PPDs */
b94498cf 859
860
861 fprintf(stderr,
862 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
863 "opt=\"%s\"\n", request_id, limit, opt);
ef416fc2 864
865 /*
866 * See if we a PPD database file...
867 */
868
f0ab5bff 869 load_ppds_dat(filename, sizeof(filename), 1);
ef416fc2 870
ef416fc2 871 /*
872 * Load all PPDs in the specified directory and below...
873 */
874
ef416fc2 875 if ((cups_datadir = getenv("CUPS_DATADIR")) == NULL)
876 cups_datadir = CUPS_DATADIR;
877
178cb736
MS
878 Inodes = cupsArrayNew((cups_array_func_t)compare_inodes, NULL);
879
ef416fc2 880 snprintf(model, sizeof(model), "%s/model", cups_datadir);
b94498cf 881 load_ppds(model, "", 1);
882
4509bb49
MS
883 snprintf(model, sizeof(model), "%s/drv", cups_datadir);
884 load_ppds(model, "", 1);
885
b94498cf 886#ifdef __APPLE__
887 /*
888 * Load PPDs from standard Mac OS X locations...
889 */
890
22c9029b
MS
891 load_ppds("/Library/Printers",
892 "Library/Printers", 0);
b94498cf 893 load_ppds("/Library/Printers/PPDs/Contents/Resources",
894 "Library/Printers/PPDs/Contents/Resources", 0);
895 load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
896 "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
22c9029b
MS
897 load_ppds("/System/Library/Printers",
898 "System/Library/Printers", 0);
b94498cf 899 load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
900 "System/Library/Printers/PPDs/Contents/Resources", 0);
901 load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
902 "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
903
904#elif defined(__linux)
905 /*
906 * Load PPDs from LSB-defined locations...
907 */
908
568fa3fa
MS
909 if (!access("/usr/local/share/ppd", 0))
910 load_ppds("/usr/local/share/ppd", "lsb/local", 1);
911 if (!access("/usr/share/ppd", 0))
912 load_ppds("/usr/share/ppd", "lsb/usr", 1);
913 if (!access("/opt/share/ppd", 0))
914 load_ppds("/opt/share/ppd", "lsb/opt", 1);
b94498cf 915#endif /* __APPLE__ */
ef416fc2 916
917 /*
918 * Cull PPD files that are no longer present...
919 */
920
66ab9486
MS
921 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
922 ppd;
923 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
ef416fc2 924 if (!ppd->found)
925 {
926 /*
927 * Remove this PPD file from the list...
928 */
929
66ab9486
MS
930 cupsArrayRemove(PPDsByName, ppd);
931 cupsArrayRemove(PPDsByMakeModel, ppd);
932 free(ppd);
f0ab5bff
MS
933
934 ChangedPPD = 1;
ef416fc2 935 }
936
ef416fc2 937 /*
938 * Write the new ppds.dat file...
939 */
940
f0ab5bff
MS
941 fprintf(stderr, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD);
942
ef416fc2 943 if (ChangedPPD)
944 {
07ed0e9a
MS
945 char newname[1024]; /* New filename */
946
947 snprintf(newname, sizeof(newname), "%s.%d", filename, (int)getpid());
948
949 if ((fp = cupsFileOpen(newname, "w")) != NULL)
ef416fc2 950 {
b94498cf 951 unsigned ppdsync = PPD_SYNC; /* Sync word */
952
b94498cf 953 cupsFileWrite(fp, (char *)&ppdsync, sizeof(ppdsync));
954
66ab9486
MS
955 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
956 ppd;
957 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
ef416fc2 958 cupsFileWrite(fp, (char *)&(ppd->record), sizeof(ppd_rec_t));
959
960 cupsFileClose(fp);
961
07ed0e9a
MS
962 if (rename(newname, filename))
963 fprintf(stderr, "ERROR: [cups-driverd] Unable to rename \"%s\" - %s\n",
964 newname, strerror(errno));
965 else
966 fprintf(stderr, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
967 filename, cupsArrayCount(PPDsByName));
ef416fc2 968 }
969 else
970 fprintf(stderr, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
971 filename, strerror(errno));
972 }
973 else
974 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr);
975
976 /*
977 * Scan for dynamic PPD files...
978 */
979
ed6e7faf
MS
980 num_options = cupsParseOptions(opt, 0, &options);
981 exclude = cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
982 num_options, options));
8b450588 983 include = cupsdCreateStringsArray(cupsGetOption("include-schemes",
ed6e7faf
MS
984 num_options, options));
985
986 load_drivers(include, exclude);
ef416fc2 987
988 /*
989 * Add the raw driver...
990 */
991
66ab9486 992 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
ed6e7faf 993 PPD_TYPE_UNKNOWN, "raw");
ef416fc2 994
ef416fc2 995 /*
996 * Send IPP attributes...
997 */
998
ed6e7faf
MS
999 requested = cupsdCreateStringsArray(
1000 cupsGetOption("requested-attributes", num_options,
1001 options));
3d8365b8 1002 device_id = cupsGetOption("ppd-device-id", num_options, options);
1003 language = cupsGetOption("ppd-natural-language", num_options, options);
1004 make = cupsGetOption("ppd-make", num_options, options);
1005 make_and_model = cupsGetOption("ppd-make-and-model", num_options, options);
1006 model_number_str = cupsGetOption("ppd-model-number", num_options, options);
1007 product = cupsGetOption("ppd-product", num_options, options);
1008 psversion = cupsGetOption("ppd-psversion", num_options, options);
1009 type_str = cupsGetOption("ppd-type", num_options, options);
b94498cf 1010
1011 if (make_and_model)
58dc1933 1012 make_and_model_len = strlen(make_and_model);
b94498cf 1013 else
58dc1933 1014 make_and_model_len = 0;
ef416fc2 1015
58dc1933
MS
1016 if (product)
1017 product_len = strlen(product);
b94498cf 1018 else
58dc1933 1019 product_len = 0;
b94498cf 1020
3d8365b8 1021 if (model_number_str)
1022 model_number = atoi(model_number_str);
1023 else
1024 model_number = 0;
1025
1026 if (type_str)
1027 {
1028 for (type = 0;
1029 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
1030 type ++)
1031 if (!strcmp(type_str, ppd_types[type]))
1032 break;
1033
1034 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
1035 {
1036 fprintf(stderr, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
1037 type_str);
1038 type_str = NULL;
1039 }
1040 }
bc44d920 1041 else
1042 type = 0;
3d8365b8 1043
ed6e7faf 1044 for (i = 0; i < num_options; i ++)
f0ab5bff 1045 fprintf(stderr, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options[i].name,
ed6e7faf 1046 options[i].value);
ef416fc2 1047
ed6e7faf 1048 if (!requested || cupsArrayFind(requested, (void *)"all") != NULL)
ef416fc2 1049 {
1050 send_name = 1;
1051 send_make = 1;
1052 send_make_and_model = 1;
3d8365b8 1053 send_model_number = 1;
ef416fc2 1054 send_natural_language = 1;
bd7854cb 1055 send_device_id = 1;
f899b121 1056 send_product = 1;
b94498cf 1057 send_psversion = 1;
3d8365b8 1058 send_type = 1;
ef416fc2 1059 }
1060 else
1061 {
ed6e7faf
MS
1062 send_name = cupsArrayFind(requested,
1063 (void *)"ppd-name") != NULL;
1064 send_make = cupsArrayFind(requested,
1065 (void *)"ppd-make") != NULL;
1066 send_make_and_model = cupsArrayFind(requested,
1067 (void *)"ppd-make-and-model") != NULL;
1068 send_model_number = cupsArrayFind(requested,
1069 (void *)"ppd-model-number") != NULL;
1070 send_natural_language = cupsArrayFind(requested,
1071 (void *)"ppd-natural-language") != NULL;
1072 send_device_id = cupsArrayFind(requested,
1073 (void *)"ppd-device-id") != NULL;
1074 send_product = cupsArrayFind(requested,
1075 (void *)"ppd-product") != NULL;
1076 send_psversion = cupsArrayFind(requested,
1077 (void *)"ppd-psversion") != NULL;
1078 send_type = cupsArrayFind(requested,
1079 (void *)"ppd-type") != NULL;
ef416fc2 1080 }
1081
f0ab5bff
MS
1082 /*
1083 * Send the content type header to the scheduler; request_id can only be
1084 * 0 when run manually since the scheduler enforces the IPP requirement for
1085 * a request ID from 1 to 2^31-1...
1086 */
1087
1088 if (request_id > 0)
1089 puts("Content-Type: application/ipp\n");
ef416fc2 1090
b94498cf 1091 sent_header = 0;
ef416fc2 1092
66ab9486
MS
1093 if (limit <= 0 || limit > cupsArrayCount(PPDsByMakeModel))
1094 count = cupsArrayCount(PPDsByMakeModel);
ef416fc2 1095 else
1096 count = limit;
1097
58dc1933 1098 if (device_id || language || make || make_and_model || model_number_str ||
8b450588 1099 product)
b94498cf 1100 {
58dc1933 1101 matches = cupsArrayNew((cups_array_func_t)compare_matches, NULL);
b94498cf 1102
58dc1933
MS
1103 if (device_id)
1104 device_id_re = regex_device_id(device_id);
1105 else
1106 device_id_re = NULL;
66ab9486 1107
58dc1933
MS
1108 if (make_and_model)
1109 make_and_model_re = regex_string(make_and_model);
1110 else
1111 make_and_model_re = NULL;
b94498cf 1112
58dc1933
MS
1113 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1114 ppd;
1115 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
ef416fc2 1116 {
58dc1933
MS
1117 /*
1118 * Filter PPDs based on make, model, product, language, model number,
1119 * and/or device ID using the "matches" score value. An exact match
1120 * for product, make-and-model, or device-id adds 3 to the score.
1121 * Partial matches for make-and-model yield 1 or 2 points, and matches
1122 * for the make and language add a single point. Results are then sorted
1123 * by score, highest score first.
1124 */
b94498cf 1125
58dc1933
MS
1126 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1127 ppd->record.type >= PPD_TYPE_DRV)
b94498cf 1128 continue;
b94498cf 1129
ed6e7faf
MS
1130 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1131 (include && !cupsArrayFind(include, ppd->record.scheme)))
1132 continue;
1133
58dc1933 1134 ppd->matches = 0;
b94498cf 1135
58dc1933
MS
1136 if (device_id_re &&
1137 !regexec(device_id_re, ppd->record.device_id,
1138 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1139 re_matches, 0))
1140 {
1141 /*
1142 * Add the number of matching values from the device ID - it will be
1143 * at least 2 (manufacturer and model), and as much as 3 (command set).
1144 */
b94498cf 1145
58dc1933
MS
1146 for (i = 1; i < (int)(sizeof(re_matches) / sizeof(re_matches[0])); i ++)
1147 if (re_matches[i].rm_so >= 0)
1148 ppd->matches ++;
1149 }
3d8365b8 1150
58dc1933
MS
1151 if (language)
1152 {
1153 for (i = 0; i < PPD_MAX_LANG; i ++)
238c3832
MS
1154 if (!ppd->record.languages[i][0])
1155 break;
1156 else if (!strcmp(ppd->record.languages[i], language))
58dc1933
MS
1157 {
1158 ppd->matches ++;
1159 break;
1160 }
1161 }
ef416fc2 1162
58dc1933
MS
1163 if (make && !strcasecmp(ppd->record.make, make))
1164 ppd->matches ++;
ef416fc2 1165
58dc1933
MS
1166 if (make_and_model_re &&
1167 !regexec(make_and_model_re, ppd->record.make_and_model,
1168 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1169 re_matches, 0))
1170 {
1171 // See how much of the make-and-model string we matched...
1172 if (re_matches[0].rm_so == 0)
1173 {
1174 if (re_matches[0].rm_eo == make_and_model_len)
1175 ppd->matches += 3; // Exact match
1176 else
1177 ppd->matches += 2; // Prefix match
1178 }
1179 else
1180 ppd->matches ++; // Infix match
1181 }
ef416fc2 1182
58dc1933
MS
1183 if (model_number_str && ppd->record.model_number == model_number)
1184 ppd->matches ++;
1185
1186 if (product)
1187 {
1188 for (i = 0; i < PPD_MAX_PROD; i ++)
238c3832
MS
1189 if (!ppd->record.products[i][0])
1190 break;
1191 else if (!strcasecmp(ppd->record.products[i], product))
58dc1933
MS
1192 {
1193 ppd->matches += 3;
1194 break;
1195 }
1196 }
1197
1198 if (psversion)
1199 {
1200 for (i = 0; i < PPD_MAX_VERS; i ++)
238c3832
MS
1201 if (!ppd->record.psversions[i][0])
1202 break;
1203 else if (!strcasecmp(ppd->record.psversions[i], psversion))
58dc1933
MS
1204 {
1205 ppd->matches ++;
1206 break;
1207 }
1208 }
1209
1210 if (type_str && ppd->record.type == type)
1211 ppd->matches ++;
1212
1213 if (ppd->matches)
1214 {
f0ab5bff 1215 fprintf(stderr, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
58dc1933
MS
1216 ppd->record.name, ppd->matches);
1217 cupsArrayAdd(matches, ppd);
1218 }
b94498cf 1219 }
58dc1933 1220 }
8b450588
MS
1221 else if (include || exclude)
1222 {
1223 matches = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
1224
1225 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1226 ppd;
1227 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
1228 {
1229 /*
1230 * Filter PPDs based on the include/exclude lists.
1231 */
1232
1233 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1234 ppd->record.type >= PPD_TYPE_DRV)
1235 continue;
1236
1237 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1238 (include && !cupsArrayFind(include, ppd->record.scheme)))
1239 continue;
1240
1241 cupsArrayAdd(matches, ppd);
1242 }
1243 }
58dc1933
MS
1244 else
1245 matches = PPDsByMakeModel;
ef416fc2 1246
58dc1933
MS
1247 for (ppd = (ppd_info_t *)cupsArrayFirst(matches);
1248 count > 0 && ppd;
1249 ppd = (ppd_info_t *)cupsArrayNext(matches))
1250 {
1251 /*
1252 * Skip invalid PPDs...
1253 */
1254
1255 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1256 ppd->record.type >= PPD_TYPE_DRV)
3d8365b8 1257 continue;
1258
b94498cf 1259 /*
1260 * Send this PPD...
1261 */
ef416fc2 1262
b94498cf 1263 if (!sent_header)
1264 {
1265 sent_header = 1;
ef416fc2 1266
b94498cf 1267 cupsdSendIPPHeader(IPP_OK, request_id);
1268 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1269 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
58dc1933
MS
1270 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
1271 "en-US");
b94498cf 1272 }
ef416fc2 1273
f0ab5bff 1274 fprintf(stderr, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
b94498cf 1275 ppd->record.name, ppd->record.make_and_model);
ef416fc2 1276
b94498cf 1277 count --;
bd7854cb 1278
b94498cf 1279 cupsdSendIPPGroup(IPP_TAG_PRINTER);
f899b121 1280
b94498cf 1281 if (send_name)
1282 cupsdSendIPPString(IPP_TAG_NAME, "ppd-name", ppd->record.name);
ef416fc2 1283
b94498cf 1284 if (send_natural_language)
1285 {
1286 cupsdSendIPPString(IPP_TAG_LANGUAGE, "ppd-natural-language",
1287 ppd->record.languages[0]);
ef416fc2 1288
66ab9486
MS
1289 for (i = 1; i < PPD_MAX_LANG && ppd->record.languages[i][0]; i ++)
1290 cupsdSendIPPString(IPP_TAG_LANGUAGE, "", ppd->record.languages[i]);
b94498cf 1291 }
ef416fc2 1292
b94498cf 1293 if (send_make)
1294 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make", ppd->record.make);
ef416fc2 1295
b94498cf 1296 if (send_make_and_model)
1297 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make-and-model",
1298 ppd->record.make_and_model);
1299
1300 if (send_device_id)
1301 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-device-id",
1302 ppd->record.device_id);
1303
1304 if (send_product)
1305 {
1306 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-product",
1307 ppd->record.products[0]);
1308
66ab9486
MS
1309 for (i = 1; i < PPD_MAX_PROD && ppd->record.products[i][0]; i ++)
1310 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.products[i]);
b94498cf 1311 }
1312
1313 if (send_psversion)
1314 {
1315 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-psversion",
1316 ppd->record.psversions[0]);
1317
66ab9486
MS
1318 for (i = 1; i < PPD_MAX_VERS && ppd->record.psversions[i][0]; i ++)
1319 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.psversions[i]);
b94498cf 1320 }
1321
3d8365b8 1322 if (send_type)
1323 cupsdSendIPPString(IPP_TAG_KEYWORD, "ppd-type",
1324 ppd_types[ppd->record.type]);
1325
1326 if (send_model_number)
1327 cupsdSendIPPInteger(IPP_TAG_INTEGER, "ppd-model-number",
1328 ppd->record.model_number);
1329
b94498cf 1330 /*
1331 * If we have only requested the ppd-make attribute, then skip
1332 * the remaining PPDs with this make...
1333 */
1334
ed6e7faf
MS
1335 if (cupsArrayFind(requested, (void *)"ppd-make") &&
1336 cupsArrayCount(requested) == 1)
b94498cf 1337 {
1338 const char *this_make; /* This ppd-make */
1339
1340
66ab9486 1341 for (this_make = ppd->record.make,
58dc1933 1342 ppd = (ppd_info_t *)cupsArrayNext(matches);
66ab9486 1343 ppd;
58dc1933 1344 ppd = (ppd_info_t *)cupsArrayNext(matches))
b94498cf 1345 if (strcasecmp(this_make, ppd->record.make))
1346 break;
1347
58dc1933 1348 cupsArrayPrev(matches);
ef416fc2 1349 }
b94498cf 1350 }
1351
1352 if (!sent_header)
1353 {
1354 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
1355 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1356 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
1357 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
1358 }
ef416fc2 1359
1360 cupsdSendIPPTrailer();
1361
1362 return (0);
1363}
1364
1365
1366/*
07ed0e9a 1367 * 'load_drv()' - Load the PPDs from a driver information file.
ef416fc2 1368 */
1369
bd7854cb 1370static int /* O - 1 on success, 0 on failure */
07ed0e9a
MS
1371load_drv(const char *filename, /* I - Actual filename */
1372 const char *name, /* I - Name to the rest of the world */
1373 cups_file_t *fp, /* I - File to read from */
1374 time_t mtime, /* I - Mod time of driver info file */
1375 off_t size) /* I - Size of driver info file */
ef416fc2 1376{
07ed0e9a
MS
1377 ppdcSource *src; // Driver information file
1378 ppdcDriver *d; // Current driver
1379 ppdcAttr *device_id, // 1284DeviceID attribute
1380 *product, // Current product value
1381 *ps_version, // PSVersion attribute
1382 *cups_fax, // cupsFax attribute
1383 *nick_name; // NickName attribute
1384 ppdcFilter *filter; // Current filter
1385 ppd_info_t *ppd; // Current PPD
1386 int products_found; // Number of products found
1387 char uri[1024], // Driver URI
1388 make_model[1024]; // Make and model
1389 int type; // Driver type
ef416fc2 1390
1391
178cb736 1392 /*
07ed0e9a 1393 * Load the driver info file...
178cb736
MS
1394 */
1395
07ed0e9a 1396 src = new ppdcSource(filename, fp);
178cb736 1397
07ed0e9a 1398 if (src->drivers->count == 0)
178cb736 1399 {
07ed0e9a
MS
1400 fprintf(stderr,
1401 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1402 filename);
1403 src->release();
178cb736
MS
1404 return (0);
1405 }
1406
1407 /*
07ed0e9a 1408 * Add a dummy entry for the file...
178cb736
MS
1409 */
1410
07ed0e9a
MS
1411 add_ppd(name, name, "", "", "", "", "", "", mtime, size, 0,
1412 PPD_TYPE_DRV, "drv");
1413 ChangedPPD = 1;
ef416fc2 1414
07ed0e9a
MS
1415 /*
1416 * Then the drivers in the file...
1417 */
178cb736 1418
07ed0e9a
MS
1419 for (d = (ppdcDriver *)src->drivers->first();
1420 d;
1421 d = (ppdcDriver *)src->drivers->next())
ef416fc2 1422 {
07ed0e9a
MS
1423 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0,
1424 "/%s/%s", name,
1425 d->file_name ? d->file_name->value :
1426 d->pc_file_name->value);
ef416fc2 1427
07ed0e9a
MS
1428 device_id = d->find_attr("1284DeviceID", NULL);
1429 ps_version = d->find_attr("PSVersion", NULL);
1430 nick_name = d->find_attr("NickName", NULL);
ef416fc2 1431
07ed0e9a
MS
1432 if (nick_name)
1433 strlcpy(make_model, nick_name->value->value, sizeof(make_model));
1434 else if (strncasecmp(d->model_name->value, d->manufacturer->value,
1435 strlen(d->manufacturer->value)))
1436 snprintf(make_model, sizeof(make_model), "%s %s, %s",
1437 d->manufacturer->value, d->model_name->value,
1438 d->version->value);
ef416fc2 1439 else
07ed0e9a
MS
1440 snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value,
1441 d->version->value);
ef416fc2 1442
07ed0e9a
MS
1443 if ((cups_fax = d->find_attr("cupsFax", NULL)) != NULL &&
1444 !strcasecmp(cups_fax->value->value, "true"))
1445 type = PPD_TYPE_FAX;
1446 else if (d->type == PPDC_DRIVER_PS)
1447 type = PPD_TYPE_POSTSCRIPT;
1448 else if (d->type != PPDC_DRIVER_CUSTOM)
1449 type = PPD_TYPE_RASTER;
1450 else
cc754834 1451 {
07ed0e9a
MS
1452 for (filter = (ppdcFilter *)d->filters->first(),
1453 type = PPD_TYPE_POSTSCRIPT;
1454 filter;
1455 filter = (ppdcFilter *)d->filters->next())
1456 if (strcasecmp(filter->mime_type->value, "application/vnd.cups-raster"))
1457 type = PPD_TYPE_RASTER;
1458 else if (strcasecmp(filter->mime_type->value,
1459 "application/vnd.cups-pdf"))
1460 type = PPD_TYPE_PDF;
cc754834 1461 }
ef416fc2 1462
07ed0e9a
MS
1463 for (product = (ppdcAttr *)d->attrs->first(), products_found = 0,
1464 ppd = NULL;
1465 product;
1466 product = (ppdcAttr *)d->attrs->next())
1467 if (!strcmp(product->name->value, "Product"))
1468 {
1469 if (!products_found)
1470 ppd = add_ppd(name, uri, "en", d->manufacturer->value, make_model,
1471 device_id ? device_id->value->value : "",
1472 product->value->value,
1473 ps_version ? ps_version->value->value : "(3010) 0",
1474 mtime, size, d->model_number, type, "drv");
1475 else if (products_found < PPD_MAX_PROD)
1476 strlcpy(ppd->record.products[products_found], product->value->value,
1477 sizeof(ppd->record.products[0]));
1478 else
1479 break;
ef416fc2 1480
07ed0e9a
MS
1481 products_found ++;
1482 }
ef416fc2 1483
07ed0e9a
MS
1484 if (!products_found)
1485 add_ppd(name, uri, "en", d->manufacturer->value, make_model,
1486 device_id ? device_id->value->value : "",
1487 d->model_name->value,
1488 ps_version ? ps_version->value->value : "(3010) 0",
1489 mtime, size, d->model_number, type, "drv");
1490 }
ef416fc2 1491
07ed0e9a 1492 src->release();
7a0cbd5e 1493
07ed0e9a
MS
1494 return (1);
1495}
66ab9486 1496
7a0cbd5e 1497
07ed0e9a
MS
1498/*
1499 * 'load_drivers()' - Load driver-generated PPD files.
1500 */
7a0cbd5e 1501
07ed0e9a
MS
1502static int /* O - 1 on success, 0 on failure */
1503load_drivers(cups_array_t *include, /* I - Drivers to include */
1504 cups_array_t *exclude) /* I - Drivers to exclude */
1505{
1506 int i; /* Looping var */
1507 char *start, /* Start of value */
1508 *ptr; /* Pointer into string */
1509 const char *server_bin, /* CUPS_SERVERBIN env variable */
1510 *scheme, /* Scheme for this driver */
1511 *scheme_end; /* Pointer to end of scheme */
1512 char drivers[1024]; /* Location of driver programs */
1513 int pid; /* Process ID for driver program */
1514 cups_file_t *fp; /* Pipe to driver program */
1515 cups_dir_t *dir; /* Directory pointer */
1516 cups_dentry_t *dent; /* Directory entry */
1517 char *argv[3], /* Arguments for command */
1518 filename[1024], /* Name of driver */
1519 line[2048], /* Line from driver */
1520 name[512], /* ppd-name */
1521 make[128], /* ppd-make */
1522 make_and_model[128], /* ppd-make-and-model */
1523 device_id[128], /* ppd-device-id */
1524 languages[128], /* ppd-natural-language */
1525 product[128], /* ppd-product */
1526 psversion[128], /* ppd-psversion */
1527 type_str[128]; /* ppd-type */
1528 int type; /* PPD type */
1529 ppd_info_t *ppd; /* Newly added PPD */
1530
1531
1532 /*
1533 * Try opening the driver directory...
1534 */
1535
1536 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
1537 server_bin = CUPS_SERVERBIN;
1538
1539 snprintf(drivers, sizeof(drivers), "%s/driver", server_bin);
1540
1541 if ((dir = cupsDirOpen(drivers)) == NULL)
1542 {
1543 fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory "
1544 "\"%s\": %s\n",
1545 drivers, strerror(errno));
1546 return (0);
1547 }
1548
1549 /*
1550 * Loop through all of the device drivers...
1551 */
1552
1553 argv[1] = (char *)"list";
1554 argv[2] = NULL;
ef416fc2 1555
07ed0e9a
MS
1556 while ((dent = cupsDirRead(dir)) != NULL)
1557 {
ef416fc2 1558 /*
07ed0e9a 1559 * Only look at executable files...
ef416fc2 1560 */
1561
07ed0e9a 1562 if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode))
ef416fc2 1563 continue;
1564
1565 /*
07ed0e9a 1566 * Include/exclude specific drivers...
ef416fc2 1567 */
1568
07ed0e9a
MS
1569 if (exclude)
1570 {
1571 /*
1572 * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
1573 */
ef416fc2 1574
07ed0e9a
MS
1575 for (scheme = (char *)cupsArrayFirst(exclude);
1576 scheme;
1577 scheme = (char *)cupsArrayNext(exclude))
1578 {
1579 fprintf(stderr, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
1580 dent->filename, scheme);
1581 scheme_end = scheme + strlen(scheme) - 1;
1582
1583 if ((scheme_end > scheme && *scheme_end == '*' &&
1584 !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
1585 !strcmp(scheme, dent->filename))
1586 {
1587 fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr);
1588 break;
1589 }
1590 }
1591
1592 if (scheme)
1593 continue;
1594 }
1595
1596 if (include)
ef416fc2 1597 {
1598 /*
07ed0e9a 1599 * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
ef416fc2 1600 */
1601
07ed0e9a
MS
1602 for (scheme = (char *)cupsArrayFirst(include);
1603 scheme;
1604 scheme = (char *)cupsArrayNext(include))
1605 {
1606 fprintf(stderr, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
1607 dent->filename, scheme);
1608 scheme_end = scheme + strlen(scheme) - 1;
1609
1610 if ((scheme_end > scheme && *scheme_end == '*' &&
1611 !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
1612 !strcmp(scheme, dent->filename))
1613 {
1614 fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr);
1615 break;
1616 }
1617 }
1618
1619 if (!scheme)
1620 continue;
ef416fc2 1621 }
07ed0e9a
MS
1622 else
1623 scheme = dent->filename;
ef416fc2 1624
1625 /*
07ed0e9a 1626 * Run the driver with no arguments and collect the output...
ef416fc2 1627 */
1628
07ed0e9a 1629 snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename);
ef416fc2 1630
22c9029b
MS
1631 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_PROGRAM, !geteuid(),
1632 _cupsFileCheckFilter, NULL))
1633 continue;
1634
1635 argv[0] = dent->filename;
1636
07ed0e9a 1637 if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL)
ef416fc2 1638 {
07ed0e9a 1639 while (cupsFileGets(fp, line, sizeof(line)))
58dc1933 1640 {
07ed0e9a
MS
1641 /*
1642 * Each line is of the form:
1643 *
1644 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
1645 * "ppd-device-id" "ppd-product" "ppd-psversion"
1646 */
58dc1933 1647
07ed0e9a
MS
1648 device_id[0] = '\0';
1649 product[0] = '\0';
1650 psversion[0] = '\0';
1651 strcpy(type_str, "postscript");
1652
1653 if (sscanf(line, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
1654 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1655 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1656 "%*[ \t]\"%127[^\"]\"",
1657 name, languages, make, make_and_model,
1658 device_id, product, psversion, type_str) < 4)
1659 {
1660 /*
1661 * Bad format; strip trailing newline and write an error message.
1662 */
1663
1664 if (line[strlen(line) - 1] == '\n')
1665 line[strlen(line) - 1] = '\0';
1666
1667 fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1668 dent->filename, line);
1669 break;
1670 }
1671 else
c168a833
MS
1672 {
1673 /*
07ed0e9a 1674 * Add the device to the array of available devices...
c168a833
MS
1675 */
1676
07ed0e9a
MS
1677 if ((start = strchr(languages, ',')) != NULL)
1678 *start++ = '\0';
c168a833 1679
07ed0e9a
MS
1680 for (type = 0;
1681 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
1682 type ++)
1683 if (!strcmp(type_str, ppd_types[type]))
1684 break;
b94498cf 1685
07ed0e9a
MS
1686 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
1687 {
1688 fprintf(stderr,
1689 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
1690 type_str);
1691 type = PPD_TYPE_UNKNOWN;
1692 }
b94498cf 1693
07ed0e9a
MS
1694 ppd = add_ppd(filename, name, languages, make, make_and_model,
1695 device_id, product, psversion, 0, 0, 0, type, scheme);
b94498cf 1696
07ed0e9a 1697 if (!ppd)
b94498cf 1698 {
07ed0e9a
MS
1699 cupsDirClose(dir);
1700 cupsFileClose(fp);
1701 return (0);
1702 }
b94498cf 1703
07ed0e9a
MS
1704 if (start && *start)
1705 {
1706 for (i = 1; i < PPD_MAX_LANG && *start; i ++)
b94498cf 1707 {
07ed0e9a 1708 if ((ptr = strchr(start, ',')) != NULL)
b94498cf 1709 *ptr++ = '\0';
07ed0e9a
MS
1710 else
1711 ptr = start + strlen(start);
b94498cf 1712
07ed0e9a
MS
1713 strlcpy(ppd->record.languages[i], start,
1714 sizeof(ppd->record.languages[0]));
3d8365b8 1715
07ed0e9a
MS
1716 start = ptr;
1717 }
1718 }
ef416fc2 1719
07ed0e9a
MS
1720 fprintf(stderr, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1721 name);
1722 }
ef416fc2 1723 }
07ed0e9a
MS
1724
1725 cupsFileClose(fp);
ef416fc2 1726 }
07ed0e9a
MS
1727 else
1728 fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1729 filename, strerror(errno));
1730 }
ef416fc2 1731
07ed0e9a 1732 cupsDirClose(dir);
ef416fc2 1733
07ed0e9a
MS
1734 return (1);
1735}
ef416fc2 1736
ef416fc2 1737
07ed0e9a
MS
1738/*
1739 * 'load_ppds()' - Load PPD files recursively.
1740 */
ef416fc2 1741
07ed0e9a
MS
1742static int /* O - 1 on success, 0 on failure */
1743load_ppds(const char *d, /* I - Actual directory */
1744 const char *p, /* I - Virtual path in name */
1745 int descend) /* I - Descend into directories? */
1746{
1747 struct stat dinfo, /* Directory information */
1748 *dinfoptr; /* Pointer to match */
1749 int i; /* Looping var */
1750 cups_file_t *fp; /* Pointer to file */
1751 cups_dir_t *dir; /* Directory pointer */
1752 cups_dentry_t *dent; /* Directory entry */
1753 char filename[1024], /* Name of PPD or directory */
1754 line[256], /* Line from backend */
1755 *ptr, /* Pointer into name */
1756 name[128], /* Name of PPD file */
1757 lang_version[64], /* PPD LanguageVersion */
1758 lang_encoding[64], /* PPD LanguageEncoding */
1759 country[64], /* Country code */
1760 manufacturer[256], /* Manufacturer */
1761 make_model[256], /* Make and Model */
1762 model_name[256], /* ModelName */
1763 nick_name[256], /* NickName */
1764 device_id[256], /* 1284DeviceID */
1765 product[256], /* Product */
1766 psversion[256], /* PSVersion */
1767 temp[512]; /* Temporary make and model */
1768 int model_number, /* cupsModelNumber */
1769 type; /* ppd-type */
1770 cups_array_t *products, /* Product array */
1771 *psversions, /* PSVersion array */
1772 *cups_languages; /* cupsLanguages array */
1773 ppd_info_t *ppd, /* New PPD file */
1774 key; /* Search key */
1775 int new_ppd; /* Is this a new PPD? */
1776 struct /* LanguageVersion translation table */
1777 {
1778 const char *version, /* LanguageVersion string */
1779 *language; /* Language code */
1780 } languages[] =
1781 {
1782 { "chinese", "zh" },
1783 { "czech", "cs" },
1784 { "danish", "da" },
1785 { "dutch", "nl" },
1786 { "english", "en" },
1787 { "finnish", "fi" },
1788 { "french", "fr" },
1789 { "german", "de" },
1790 { "greek", "el" },
1791 { "hungarian", "hu" },
1792 { "italian", "it" },
1793 { "japanese", "ja" },
1794 { "korean", "ko" },
1795 { "norwegian", "no" },
1796 { "polish", "pl" },
1797 { "portuguese", "pt" },
1798 { "russian", "ru" },
1799 { "simplified chinese", "zh_CN" },
1800 { "slovak", "sk" },
1801 { "spanish", "es" },
1802 { "swedish", "sv" },
1803 { "traditional chinese", "zh_TW" },
1804 { "turkish", "tr" }
1805 };
ef416fc2 1806
f899b121 1807
07ed0e9a
MS
1808 /*
1809 * See if we've loaded this directory before...
1810 */
f899b121 1811
07ed0e9a
MS
1812 if (stat(d, &dinfo))
1813 {
1814 if (errno != ENOENT)
1815 fprintf(stderr, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d,
1816 strerror(errno));
f899b121 1817
07ed0e9a
MS
1818 return (0);
1819 }
1820 else if (cupsArrayFind(Inodes, &dinfo))
1821 {
1822 fprintf(stderr, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
1823 d);
1824 return (0);
1825 }
f899b121 1826
07ed0e9a
MS
1827 /*
1828 * Nope, add it to the Inodes array and continue...
1829 */
f899b121 1830
07ed0e9a
MS
1831 dinfoptr = (struct stat *)malloc(sizeof(struct stat));
1832 memcpy(dinfoptr, &dinfo, sizeof(struct stat));
1833 cupsArrayAdd(Inodes, dinfoptr);
ef416fc2 1834
22c9029b
MS
1835 /*
1836 * Check permissions...
1837 */
1838
1839 if (_cupsFileCheck(d, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
1840 _cupsFileCheckFilter, NULL))
1841 return (0);
1842
07ed0e9a
MS
1843 if ((dir = cupsDirOpen(d)) == NULL)
1844 {
1845 if (errno != ENOENT)
1846 fprintf(stderr,
1847 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1848 d, strerror(errno));
3d8365b8 1849
07ed0e9a
MS
1850 return (0);
1851 }
ef416fc2 1852
07ed0e9a 1853 fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d);
ef416fc2 1854
07ed0e9a
MS
1855 while ((dent = cupsDirRead(dir)) != NULL)
1856 {
1857 /*
1858 * Skip files/directories starting with "."...
1859 */
5eb9da71 1860
07ed0e9a
MS
1861 if (dent->filename[0] == '.')
1862 continue;
5eb9da71
MS
1863
1864 /*
07ed0e9a 1865 * See if this is a file...
5eb9da71
MS
1866 */
1867
07ed0e9a
MS
1868 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
1869
1870 if (p[0])
1871 snprintf(name, sizeof(name), "%s/%s", p, dent->filename);
1872 else
1873 strlcpy(name, dent->filename, sizeof(name));
1874
1875 if (S_ISDIR(dent->fileinfo.st_mode))
ef416fc2 1876 {
1877 /*
07ed0e9a 1878 * Do subdirectory...
ef416fc2 1879 */
1880
07ed0e9a 1881 if (descend)
22c9029b 1882 {
07ed0e9a
MS
1883 if (!load_ppds(filename, name, 1))
1884 {
1885 cupsDirClose(dir);
1886 return (1);
1887 }
22c9029b
MS
1888 }
1889 else if ((ptr = filename + strlen(filename) - 14) > filename &&
1890 !strcmp(ptr, ".printerDriver"))
1891 {
1892 /*
1893 * Load PPDs in a printer driver bundle.
1894 */
1895
1896 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
1897 _cupsFileCheckFilter, NULL))
1898 continue;
1899
1900 strlcat(filename, "/Contents/Resources/PPDs", sizeof(filename));
1901 strlcat(name, "/Contents/Resources/PPDs", sizeof(name));
1902
1903 load_ppds(filename, name, 0);
1904 }
ef416fc2 1905
07ed0e9a
MS
1906 continue;
1907 }
1908 else if ((ptr = filename + strlen(filename) - 6) > filename &&
1909 !strcmp(ptr, ".plist"))
1910 {
ef416fc2 1911 /*
07ed0e9a 1912 * Skip plist files in the PPDs directory...
ef416fc2 1913 */
1914
07ed0e9a 1915 continue;
ef416fc2 1916 }
22c9029b
MS
1917 else if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_FILE_ONLY, !geteuid(),
1918 _cupsFileCheckFilter, NULL))
1919 continue;
ef416fc2 1920
1921 /*
07ed0e9a 1922 * See if this file has been scanned before...
ef416fc2 1923 */
1924
07ed0e9a
MS
1925 strcpy(key.record.filename, name);
1926 strcpy(key.record.name, name);
ef416fc2 1927
07ed0e9a
MS
1928 ppd = (ppd_info_t *)cupsArrayFind(PPDsByName, &key);
1929
1930 if (ppd &&
1931 ppd->record.size == dent->fileinfo.st_size &&
1932 ppd->record.mtime == dent->fileinfo.st_mtime)
ef416fc2 1933 {
1934 /*
07ed0e9a 1935 * Rewind to the first entry for this file...
ef416fc2 1936 */
1937
07ed0e9a
MS
1938 while ((ppd = (ppd_info_t *)cupsArrayPrev(PPDsByName)) != NULL &&
1939 !strcmp(ppd->record.filename, name));
1940
ef416fc2 1941 /*
07ed0e9a 1942 * Then mark all of the matches for this file as found...
ef416fc2 1943 */
1944
07ed0e9a
MS
1945 while ((ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) != NULL &&
1946 !strcmp(ppd->record.filename, name))
1947 ppd->found = 1;
ef416fc2 1948
07ed0e9a 1949 continue;
ef416fc2 1950 }
1951
1952 /*
07ed0e9a 1953 * No, file is new/changed, so re-scan it...
ef416fc2 1954 */
1955
07ed0e9a
MS
1956 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1957 continue;
ef416fc2 1958
07ed0e9a
MS
1959 /*
1960 * Now see if this is a PPD file...
1961 */
ef416fc2 1962
07ed0e9a
MS
1963 line[0] = '\0';
1964 cupsFileGets(fp, line, sizeof(line));
b94498cf 1965
07ed0e9a 1966 if (strncmp(line, "*PPD-Adobe:", 11))
ef416fc2 1967 {
1968 /*
07ed0e9a 1969 * Nope, treat it as a driver information file...
ef416fc2 1970 */
1971
07ed0e9a
MS
1972 load_drv(filename, name, fp, dent->fileinfo.st_mtime,
1973 dent->fileinfo.st_size);
1974 continue;
ef416fc2 1975 }
1976
f899b121 1977 /*
07ed0e9a 1978 * Now read until we get the NickName field...
b94498cf 1979 */
1980
07ed0e9a
MS
1981 cups_languages = cupsArrayNew(NULL, NULL);
1982 products = cupsArrayNew(NULL, NULL);
1983 psversions = cupsArrayNew(NULL, NULL);
4509bb49 1984
07ed0e9a
MS
1985 model_name[0] = '\0';
1986 nick_name[0] = '\0';
1987 manufacturer[0] = '\0';
1988 device_id[0] = '\0';
1989 lang_encoding[0] = '\0';
1990 strcpy(lang_version, "en");
1991 model_number = 0;
1992 type = PPD_TYPE_POSTSCRIPT;
4509bb49 1993
07ed0e9a
MS
1994 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
1995 {
1996 if (!strncmp(line, "*Manufacturer:", 14))
1997 sscanf(line, "%*[^\"]\"%255[^\"]", manufacturer);
1998 else if (!strncmp(line, "*ModelName:", 11))
1999 sscanf(line, "%*[^\"]\"%127[^\"]", model_name);
2000 else if (!strncmp(line, "*LanguageEncoding:", 18))
2001 sscanf(line, "%*[^:]:%63s", lang_encoding);
2002 else if (!strncmp(line, "*LanguageVersion:", 17))
2003 sscanf(line, "%*[^:]:%63s", lang_version);
2004 else if (!strncmp(line, "*NickName:", 10))
2005 sscanf(line, "%*[^\"]\"%255[^\"]", nick_name);
2006 else if (!strncasecmp(line, "*1284DeviceID:", 14))
2007 {
2008 sscanf(line, "%*[^\"]\"%255[^\"]", device_id);
4509bb49 2009
07ed0e9a
MS
2010 // Make sure device ID ends with a semicolon...
2011 if (device_id[0] && device_id[strlen(device_id) - 1] != ';')
2012 strlcat(device_id, ";", sizeof(device_id));
2013 }
2014 else if (!strncmp(line, "*Product:", 9))
2015 {
2016 if (sscanf(line, "%*[^\"]\"(%255[^\"]", product) == 1)
2017 {
2018 /*
2019 * Make sure the value ends with a right parenthesis - can't stop at
2020 * the first right paren since the product name may contain escaped
2021 * parenthesis...
2022 */
66ab9486 2023
07ed0e9a
MS
2024 ptr = product + strlen(product) - 1;
2025 if (ptr > product && *ptr == ')')
2026 {
2027 /*
2028 * Yes, ends with a parenthesis, so remove it from the end and
2029 * add the product to the list...
2030 */
4509bb49 2031
07ed0e9a
MS
2032 *ptr = '\0';
2033 cupsArrayAdd(products, strdup(product));
2034 }
2035 }
2036 }
2037 else if (!strncmp(line, "*PSVersion:", 11))
2038 {
2039 sscanf(line, "%*[^\"]\"%255[^\"]", psversion);
2040 cupsArrayAdd(psversions, strdup(psversion));
2041 }
2042 else if (!strncmp(line, "*cupsLanguages:", 15))
2043 {
2044 char *start; /* Start of language */
4509bb49 2045
66ab9486 2046
07ed0e9a 2047 for (start = line + 15; *start && isspace(*start & 255); start ++);
66ab9486 2048
07ed0e9a
MS
2049 if (*start++ == '\"')
2050 {
2051 while (*start)
2052 {
2053 for (ptr = start + 1;
2054 *ptr && *ptr != '\"' && !isspace(*ptr & 255);
2055 ptr ++);
66ab9486 2056
07ed0e9a
MS
2057 if (*ptr)
2058 {
2059 *ptr++ = '\0';
4509bb49 2060
07ed0e9a
MS
2061 while (isspace(*ptr & 255))
2062 *ptr++ = '\0';
2063 }
4509bb49 2064
07ed0e9a
MS
2065 cupsArrayAdd(cups_languages, strdup(start));
2066 start = ptr;
2067 }
2068 }
2069 }
2070 else if (!strncmp(line, "*cupsFax:", 9))
2071 {
2072 for (ptr = line + 9; isspace(*ptr & 255); ptr ++);
4509bb49 2073
07ed0e9a
MS
2074 if (!strncasecmp(ptr, "true", 4))
2075 type = PPD_TYPE_FAX;
2076 }
2077 else if (!strncmp(line, "*cupsFilter:", 12) && type == PPD_TYPE_POSTSCRIPT)
2078 {
2079 if (strstr(line + 12, "application/vnd.cups-raster"))
4509bb49 2080 type = PPD_TYPE_RASTER;
07ed0e9a 2081 else if (strstr(line + 12, "application/vnd.cups-pdf"))
4509bb49 2082 type = PPD_TYPE_PDF;
07ed0e9a
MS
2083 }
2084 else if (!strncmp(line, "*cupsModelNumber:", 17))
2085 sscanf(line, "*cupsModelNumber:%d", &model_number);
2086 else if (!strncmp(line, "*OpenUI", 7))
4509bb49 2087 {
07ed0e9a
MS
2088 /*
2089 * Stop early if we have a NickName or ModelName attributes
2090 * before the first OpenUI...
2091 */
4509bb49 2092
07ed0e9a
MS
2093 if ((model_name[0] || nick_name[0]) && cupsArrayCount(products) > 0 &&
2094 cupsArrayCount(psversions) > 0)
2095 break;
4509bb49 2096 }
07ed0e9a 2097 }
4509bb49 2098
07ed0e9a
MS
2099 /*
2100 * Close the file...
2101 */
4509bb49 2102
07ed0e9a 2103 cupsFileClose(fp);
4509bb49 2104
07ed0e9a
MS
2105 /*
2106 * See if we got all of the required info...
2107 */
4509bb49 2108
07ed0e9a
MS
2109 if (nick_name[0])
2110 cupsCharsetToUTF8((cups_utf8_t *)make_model, nick_name,
2111 sizeof(make_model), _ppdGetEncoding(lang_encoding));
2112 else
2113 strcpy(make_model, model_name);
ef416fc2 2114
07ed0e9a
MS
2115 while (isspace(make_model[0] & 255))
2116 _cups_strcpy(make_model, make_model + 1);
ef416fc2 2117
07ed0e9a
MS
2118 if (!make_model[0] || cupsArrayCount(products) == 0 ||
2119 cupsArrayCount(psversions) == 0)
2120 {
2121 /*
2122 * We don't have all the info needed, so skip this file...
2123 */
ef416fc2 2124
07ed0e9a
MS
2125 if (!make_model[0])
2126 fprintf(stderr, "WARNING: Missing NickName and ModelName in %s!\n",
2127 filename);
ef416fc2 2128
07ed0e9a
MS
2129 if (cupsArrayCount(products) == 0)
2130 fprintf(stderr, "WARNING: Missing Product in %s!\n", filename);
ef416fc2 2131
07ed0e9a
MS
2132 if (cupsArrayCount(psversions) == 0)
2133 fprintf(stderr, "WARNING: Missing PSVersion in %s!\n", filename);
ef416fc2 2134
07ed0e9a
MS
2135 free_array(products);
2136 free_array(psversions);
2137 free_array(cups_languages);
ef416fc2 2138
07ed0e9a
MS
2139 continue;
2140 }
ef416fc2 2141
07ed0e9a
MS
2142 if (model_name[0])
2143 cupsArrayAdd(products, strdup(model_name));
c934a06c 2144
ef416fc2 2145 /*
07ed0e9a 2146 * Normalize the make and model string...
ef416fc2 2147 */
2148
07ed0e9a
MS
2149 while (isspace(manufacturer[0] & 255))
2150 _cups_strcpy(manufacturer, manufacturer + 1);
2151
2152 if (!strncasecmp(make_model, manufacturer, strlen(manufacturer)))
2153 strlcpy(temp, make_model, sizeof(temp));
2154 else
2155 snprintf(temp, sizeof(temp), "%s %s", manufacturer, make_model);
2156
2157 _ppdNormalizeMakeAndModel(temp, make_model, sizeof(make_model));
ef416fc2 2158
ed6e7faf 2159 /*
07ed0e9a 2160 * See if we got a manufacturer...
ed6e7faf
MS
2161 */
2162
07ed0e9a 2163 if (!manufacturer[0] || !strcmp(manufacturer, "ESP"))
8b450588
MS
2164 {
2165 /*
07ed0e9a 2166 * Nope, copy the first part of the make and model then...
8b450588
MS
2167 */
2168
07ed0e9a 2169 strlcpy(manufacturer, make_model, sizeof(manufacturer));
8b450588 2170
07ed0e9a
MS
2171 /*
2172 * Truncate at the first space, dash, or slash, or make the
2173 * manufacturer "Other"...
2174 */
2175
2176 for (ptr = manufacturer; *ptr; ptr ++)
2177 if (*ptr == ' ' || *ptr == '-' || *ptr == '/')
8b450588 2178 break;
8b450588 2179
07ed0e9a
MS
2180 if (*ptr && ptr > manufacturer)
2181 *ptr = '\0';
2182 else
2183 strcpy(manufacturer, "Other");
8b450588 2184 }
07ed0e9a
MS
2185 else if (!strncasecmp(manufacturer, "LHAG", 4) ||
2186 !strncasecmp(manufacturer, "linotype", 8))
2187 strcpy(manufacturer, "LHAG");
2188 else if (!strncasecmp(manufacturer, "Hewlett", 7))
2189 strcpy(manufacturer, "HP");
8b450588 2190
07ed0e9a
MS
2191 /*
2192 * Fix the lang_version as needed...
2193 */
2194
2195 if ((ptr = strchr(lang_version, '-')) != NULL)
2196 *ptr++ = '\0';
2197 else if ((ptr = strchr(lang_version, '_')) != NULL)
2198 *ptr++ = '\0';
2199
2200 if (ptr)
8b450588
MS
2201 {
2202 /*
07ed0e9a 2203 * Setup the country suffix...
8b450588
MS
2204 */
2205
07ed0e9a
MS
2206 country[0] = '_';
2207 _cups_strcpy(country + 1, ptr);
2208 }
2209 else
2210 {
2211 /*
2212 * No country suffix...
2213 */
8b450588 2214
07ed0e9a
MS
2215 country[0] = '\0';
2216 }
8b450588 2217
07ed0e9a
MS
2218 for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++)
2219 if (!strcasecmp(languages[i].version, lang_version))
2220 break;
2221
2222 if (i < (int)(sizeof(languages) / sizeof(languages[0])))
2223 {
2224 /*
2225 * Found a known language...
2226 */
2227
2228 snprintf(lang_version, sizeof(lang_version), "%s%s",
2229 languages[i].language, country);
8b450588
MS
2230 }
2231 else
07ed0e9a
MS
2232 {
2233 /*
2234 * Unknown language; use "xx"...
2235 */
2236
2237 strcpy(lang_version, "xx");
2238 }
ed6e7faf 2239
ef416fc2 2240 /*
07ed0e9a 2241 * Record the PPD file...
ef416fc2 2242 */
2243
07ed0e9a 2244 new_ppd = !ppd;
c934a06c 2245
07ed0e9a 2246 if (new_ppd)
ef416fc2 2247 {
07ed0e9a
MS
2248 /*
2249 * Add new PPD file...
2250 */
ef416fc2 2251
07ed0e9a 2252 fprintf(stderr, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name);
bd7854cb 2253
07ed0e9a
MS
2254 ppd = add_ppd(name, name, lang_version, manufacturer, make_model,
2255 device_id, (char *)cupsArrayFirst(products),
2256 (char *)cupsArrayFirst(psversions),
2257 dent->fileinfo.st_mtime, dent->fileinfo.st_size,
2258 model_number, type, "file");
ef416fc2 2259
07ed0e9a
MS
2260 if (!ppd)
2261 {
2262 cupsDirClose(dir);
2263 return (0);
2264 }
2265 }
2266 else
2267 {
2268 /*
2269 * Update existing record...
2270 */
ef416fc2 2271
07ed0e9a 2272 fprintf(stderr, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name);
ef416fc2 2273
07ed0e9a 2274 memset(ppd, 0, sizeof(ppd_info_t));
b94498cf 2275
07ed0e9a
MS
2276 ppd->found = 1;
2277 ppd->record.mtime = dent->fileinfo.st_mtime;
2278 ppd->record.size = dent->fileinfo.st_size;
2279 ppd->record.model_number = model_number;
2280 ppd->record.type = type;
3d8365b8 2281
07ed0e9a
MS
2282 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
2283 strlcpy(ppd->record.make, manufacturer, sizeof(ppd->record.make));
2284 strlcpy(ppd->record.make_and_model, make_model,
2285 sizeof(ppd->record.make_and_model));
2286 strlcpy(ppd->record.languages[0], lang_version,
2287 sizeof(ppd->record.languages[0]));
2288 strlcpy(ppd->record.products[0], (char *)cupsArrayFirst(products),
2289 sizeof(ppd->record.products[0]));
2290 strlcpy(ppd->record.psversions[0], (char *)cupsArrayFirst(psversions),
2291 sizeof(ppd->record.psversions[0]));
2292 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
2293 }
3d8365b8 2294
07ed0e9a
MS
2295 /*
2296 * Add remaining products, versions, and languages...
2297 */
b94498cf 2298
07ed0e9a
MS
2299 for (i = 1;
2300 i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL;
2301 i ++)
2302 strlcpy(ppd->record.products[i], ptr,
2303 sizeof(ppd->record.products[0]));
ef416fc2 2304
07ed0e9a
MS
2305 for (i = 1;
2306 i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL;
2307 i ++)
2308 strlcpy(ppd->record.psversions[i], ptr,
2309 sizeof(ppd->record.psversions[0]));
b94498cf 2310
07ed0e9a
MS
2311 for (i = 1, ptr = (char *)cupsArrayFirst(cups_languages);
2312 i < PPD_MAX_LANG && ptr;
2313 i ++, ptr = (char *)cupsArrayNext(cups_languages))
2314 strlcpy(ppd->record.languages[i], ptr,
2315 sizeof(ppd->record.languages[0]));
b94498cf 2316
07ed0e9a
MS
2317 /*
2318 * Free products, versions, and languages...
2319 */
b94498cf 2320
07ed0e9a
MS
2321 free_array(cups_languages);
2322 free_array(products);
2323 free_array(psversions);
ef416fc2 2324
07ed0e9a 2325 ChangedPPD = 1;
ef416fc2 2326 }
2327
2328 cupsDirClose(dir);
2329
2330 return (1);
2331}
2332
2333
f0ab5bff
MS
2334/*
2335 * 'load_ppds_dat()' - Load the ppds.dat file.
2336 */
2337
2338static void
2339load_ppds_dat(char *filename, /* I - Filename buffer */
2340 size_t filesize, /* I - Size of filename buffer */
2341 int verbose) /* I - Be verbose? */
2342{
2343 ppd_info_t *ppd; /* Current PPD file */
2344 cups_file_t *fp; /* ppds.dat file */
2345 struct stat fileinfo; /* ppds.dat information */
2346 const char *cups_cachedir; /* CUPS_CACHEDIR environment variable */
2347
2348
2349 PPDsByName = cupsArrayNew((cups_array_func_t)compare_names, NULL);
2350 PPDsByMakeModel = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
2351 ChangedPPD = 0;
2352
2353 if ((cups_cachedir = getenv("CUPS_CACHEDIR")) == NULL)
2354 cups_cachedir = CUPS_CACHEDIR;
2355
2356 snprintf(filename, filesize, "%s/ppds.dat", cups_cachedir);
2357 if ((fp = cupsFileOpen(filename, "r")) != NULL)
2358 {
2359 /*
2360 * See if we have the right sync word...
2361 */
2362
2363 unsigned ppdsync; /* Sync word */
2364 int num_ppds; /* Number of PPDs */
2365
f0ab5bff
MS
2366 if (cupsFileRead(fp, (char *)&ppdsync, sizeof(ppdsync))
2367 == sizeof(ppdsync) &&
2368 ppdsync == PPD_SYNC &&
2369 !stat(filename, &fileinfo) &&
2370 ((fileinfo.st_size - sizeof(ppdsync)) % sizeof(ppd_rec_t)) == 0 &&
2371 (num_ppds = (fileinfo.st_size - sizeof(ppdsync)) /
2372 sizeof(ppd_rec_t)) > 0)
2373 {
2374 /*
2375 * We have a ppds.dat file, so read it!
2376 */
2377
2378 for (; num_ppds > 0; num_ppds --)
2379 {
2380 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
2381 {
2382 if (verbose)
2383 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
2384 stderr);
2385 exit(1);
2386 }
2387
2388 if (cupsFileRead(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)) > 0)
2389 {
2390 cupsArrayAdd(PPDsByName, ppd);
2391 cupsArrayAdd(PPDsByMakeModel, ppd);
2392 }
2393 else
2394 {
2395 free(ppd);
2396 break;
2397 }
2398 }
2399
2400 if (verbose)
2401 fprintf(stderr, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
2402 filename, cupsArrayCount(PPDsByName));
2403 }
2404
2405 cupsFileClose(fp);
2406 }
2407}
2408
2409
58dc1933
MS
2410/*
2411 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2412 * ID.
2413 */
2414
2415static regex_t * /* O - Regular expression */
2416regex_device_id(const char *device_id) /* I - IEEE-1284 device ID */
2417{
2418 char res[2048], /* Regular expression string */
2419 *ptr; /* Pointer into string */
2420 regex_t *re; /* Regular expression */
2421 int cmd; /* Command set string? */
2422
2423
2424 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id);
2425
2426 /*
2427 * Scan the device ID string and insert class, command set, manufacturer, and
2428 * model attributes to match. We assume that the device ID in the PPD and the
2429 * device ID reported by the device itself use the same attribute names and
2430 * order of attributes.
2431 */
2432
2433 ptr = res;
2434
2435 while (*device_id && ptr < (res + sizeof(res) - 6))
2436 {
2437 cmd = !strncasecmp(device_id, "COMMAND SET:", 12) ||
2438 !strncasecmp(device_id, "CMD:", 4);
2439
2440 if (cmd || !strncasecmp(device_id, "MANUFACTURER:", 13) ||
2441 !strncasecmp(device_id, "MFG:", 4) ||
2442 !strncasecmp(device_id, "MFR:", 4) ||
2443 !strncasecmp(device_id, "MODEL:", 6) ||
2444 !strncasecmp(device_id, "MDL:", 4))
2445 {
2446 if (ptr > res)
2447 {
2448 *ptr++ = '.';
2449 *ptr++ = '*';
2450 }
2451
2452 *ptr++ = '(';
2453
f14324a7 2454 while (*device_id && *device_id != ';' && ptr < (res + sizeof(res) - 8))
58dc1933
MS
2455 {
2456 if (strchr("[]{}().*\\|", *device_id))
2457 *ptr++ = '\\';
f14324a7
MS
2458 if (*device_id == ':')
2459 {
2460 /*
2461 * KEY:.*value
2462 */
2463
2464 *ptr++ = *device_id++;
2465 *ptr++ = '.';
2466 *ptr++ = '*';
2467 }
2468 else
2469 *ptr++ = *device_id++;
58dc1933
MS
2470 }
2471
2472 if (*device_id == ';' || !*device_id)
f14324a7
MS
2473 {
2474 /*
2475 * KEY:.*value.*;
2476 */
2477
2478 *ptr++ = '.';
2479 *ptr++ = '*';
58dc1933 2480 *ptr++ = ';';
f14324a7 2481 }
58dc1933
MS
2482 *ptr++ = ')';
2483 if (cmd)
2484 *ptr++ = '?';
2485 }
2486 else if ((device_id = strchr(device_id, ';')) == NULL)
2487 break;
2488 else
2489 device_id ++;
2490 }
2491
2492 *ptr = '\0';
2493
2494 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res);
2495
2496 /*
2497 * Compile the regular expression and return...
2498 */
2499
2500 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2501 {
2502 if (!regcomp(re, res, REG_EXTENDED | REG_ICASE))
2503 {
2504 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr);
2505 return (re);
2506 }
2507
2508 free(re);
2509 }
2510
2511 return (NULL);
2512}
2513
2514
2515/*
2516 * 'regex_string()' - Construct a regular expression to compare a simple string.
2517 */
2518
2519static regex_t * /* O - Regular expression */
2520regex_string(const char *s) /* I - String to compare */
2521{
2522 char res[2048], /* Regular expression string */
2523 *ptr; /* Pointer into string */
2524 regex_t *re; /* Regular expression */
2525
2526
2527 fprintf(stderr, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s);
2528
2529 /*
2530 * Convert the string to a regular expression, escaping special characters
2531 * as needed.
2532 */
2533
2534 ptr = res;
2535
2536 while (*s && ptr < (res + sizeof(res) - 2))
2537 {
2538 if (strchr("[]{}().*\\", *s))
2539 *ptr++ = '\\';
2540
2541 *ptr++ = *s++;
2542 }
2543
2544 *ptr = '\0';
2545
2546 fprintf(stderr, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res);
2547
2548 /*
2549 * Create a case-insensitive regular expression...
2550 */
2551
2552 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2553 {
2554 if (!regcomp(re, res, REG_ICASE))
2555 {
2556 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr);
2557 return (re);
2558 }
2559
2560 free(re);
2561 }
2562
2563 return (NULL);
2564}
2565
2566
ef416fc2 2567/*
4509bb49 2568 * End of "$Id$".
ef416fc2 2569 */