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