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