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