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