]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/cups-driverd.cxx
Merge changes from CUPS 1.4svn-r8162.
[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));
945 include = cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
946 num_options, options));
947
948 load_drivers(include, exclude);
ef416fc2 949
950 /*
951 * Add the raw driver...
952 */
953
66ab9486 954 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
ed6e7faf 955 PPD_TYPE_UNKNOWN, "raw");
ef416fc2 956
ef416fc2 957 /*
958 * Send IPP attributes...
959 */
960
ed6e7faf
MS
961 requested = cupsdCreateStringsArray(
962 cupsGetOption("requested-attributes", num_options,
963 options));
3d8365b8 964 device_id = cupsGetOption("ppd-device-id", num_options, options);
965 language = cupsGetOption("ppd-natural-language", num_options, options);
966 make = cupsGetOption("ppd-make", num_options, options);
967 make_and_model = cupsGetOption("ppd-make-and-model", num_options, options);
968 model_number_str = cupsGetOption("ppd-model-number", num_options, options);
969 product = cupsGetOption("ppd-product", num_options, options);
970 psversion = cupsGetOption("ppd-psversion", num_options, options);
971 type_str = cupsGetOption("ppd-type", num_options, options);
b94498cf 972
973 if (make_and_model)
58dc1933 974 make_and_model_len = strlen(make_and_model);
b94498cf 975 else
58dc1933 976 make_and_model_len = 0;
ef416fc2 977
58dc1933
MS
978 if (product)
979 product_len = strlen(product);
b94498cf 980 else
58dc1933 981 product_len = 0;
b94498cf 982
3d8365b8 983 if (model_number_str)
984 model_number = atoi(model_number_str);
985 else
986 model_number = 0;
987
988 if (type_str)
989 {
990 for (type = 0;
991 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
992 type ++)
993 if (!strcmp(type_str, ppd_types[type]))
994 break;
995
996 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
997 {
998 fprintf(stderr, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
999 type_str);
1000 type_str = NULL;
1001 }
1002 }
bc44d920 1003 else
1004 type = 0;
3d8365b8 1005
ed6e7faf
MS
1006 for (i = 0; i < num_options; i ++)
1007 fprintf(stderr, "DEBUG: [cups-driverd] %s=\"%s\"\n", options[i].name,
1008 options[i].value);
ef416fc2 1009
ed6e7faf 1010 if (!requested || cupsArrayFind(requested, (void *)"all") != NULL)
ef416fc2 1011 {
1012 send_name = 1;
1013 send_make = 1;
1014 send_make_and_model = 1;
3d8365b8 1015 send_model_number = 1;
ef416fc2 1016 send_natural_language = 1;
bd7854cb 1017 send_device_id = 1;
f899b121 1018 send_product = 1;
b94498cf 1019 send_psversion = 1;
3d8365b8 1020 send_type = 1;
ef416fc2 1021 }
1022 else
1023 {
ed6e7faf
MS
1024 send_name = cupsArrayFind(requested,
1025 (void *)"ppd-name") != NULL;
1026 send_make = cupsArrayFind(requested,
1027 (void *)"ppd-make") != NULL;
1028 send_make_and_model = cupsArrayFind(requested,
1029 (void *)"ppd-make-and-model") != NULL;
1030 send_model_number = cupsArrayFind(requested,
1031 (void *)"ppd-model-number") != NULL;
1032 send_natural_language = cupsArrayFind(requested,
1033 (void *)"ppd-natural-language") != NULL;
1034 send_device_id = cupsArrayFind(requested,
1035 (void *)"ppd-device-id") != NULL;
1036 send_product = cupsArrayFind(requested,
1037 (void *)"ppd-product") != NULL;
1038 send_psversion = cupsArrayFind(requested,
1039 (void *)"ppd-psversion") != NULL;
1040 send_type = cupsArrayFind(requested,
1041 (void *)"ppd-type") != NULL;
ef416fc2 1042 }
1043
1044 puts("Content-Type: application/ipp\n");
1045
b94498cf 1046 sent_header = 0;
ef416fc2 1047
66ab9486
MS
1048 if (limit <= 0 || limit > cupsArrayCount(PPDsByMakeModel))
1049 count = cupsArrayCount(PPDsByMakeModel);
ef416fc2 1050 else
1051 count = limit;
1052
58dc1933 1053 if (device_id || language || make || make_and_model || model_number_str ||
ed6e7faf 1054 product || include || exclude)
b94498cf 1055 {
58dc1933 1056 matches = cupsArrayNew((cups_array_func_t)compare_matches, NULL);
b94498cf 1057
58dc1933
MS
1058 if (device_id)
1059 device_id_re = regex_device_id(device_id);
1060 else
1061 device_id_re = NULL;
66ab9486 1062
58dc1933
MS
1063 if (make_and_model)
1064 make_and_model_re = regex_string(make_and_model);
1065 else
1066 make_and_model_re = NULL;
b94498cf 1067
58dc1933
MS
1068 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1069 ppd;
1070 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
ef416fc2 1071 {
58dc1933
MS
1072 /*
1073 * Filter PPDs based on make, model, product, language, model number,
1074 * and/or device ID using the "matches" score value. An exact match
1075 * for product, make-and-model, or device-id adds 3 to the score.
1076 * Partial matches for make-and-model yield 1 or 2 points, and matches
1077 * for the make and language add a single point. Results are then sorted
1078 * by score, highest score first.
1079 */
b94498cf 1080
58dc1933
MS
1081 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1082 ppd->record.type >= PPD_TYPE_DRV)
b94498cf 1083 continue;
b94498cf 1084
ed6e7faf
MS
1085 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1086 (include && !cupsArrayFind(include, ppd->record.scheme)))
1087 continue;
1088
58dc1933 1089 ppd->matches = 0;
b94498cf 1090
58dc1933
MS
1091 if (device_id_re &&
1092 !regexec(device_id_re, ppd->record.device_id,
1093 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1094 re_matches, 0))
1095 {
1096 /*
1097 * Add the number of matching values from the device ID - it will be
1098 * at least 2 (manufacturer and model), and as much as 3 (command set).
1099 */
b94498cf 1100
58dc1933
MS
1101 for (i = 1; i < (int)(sizeof(re_matches) / sizeof(re_matches[0])); i ++)
1102 if (re_matches[i].rm_so >= 0)
1103 ppd->matches ++;
1104 }
3d8365b8 1105
58dc1933
MS
1106 if (language)
1107 {
1108 for (i = 0; i < PPD_MAX_LANG; i ++)
1109 if (!ppd->record.languages[i][0] ||
1110 !strcasecmp(ppd->record.languages[i], language))
1111 {
1112 ppd->matches ++;
1113 break;
1114 }
1115 }
ef416fc2 1116
58dc1933
MS
1117 if (make && !strcasecmp(ppd->record.make, make))
1118 ppd->matches ++;
ef416fc2 1119
58dc1933
MS
1120 if (make_and_model_re &&
1121 !regexec(make_and_model_re, ppd->record.make_and_model,
1122 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1123 re_matches, 0))
1124 {
1125 // See how much of the make-and-model string we matched...
1126 if (re_matches[0].rm_so == 0)
1127 {
1128 if (re_matches[0].rm_eo == make_and_model_len)
1129 ppd->matches += 3; // Exact match
1130 else
1131 ppd->matches += 2; // Prefix match
1132 }
1133 else
1134 ppd->matches ++; // Infix match
1135 }
ef416fc2 1136
58dc1933
MS
1137 if (model_number_str && ppd->record.model_number == model_number)
1138 ppd->matches ++;
1139
1140 if (product)
1141 {
1142 for (i = 0; i < PPD_MAX_PROD; i ++)
1143 if (!ppd->record.products[i][0] ||
1144 !strcasecmp(ppd->record.products[i], product))
1145 {
1146 ppd->matches += 3;
1147 break;
1148 }
1149 }
1150
1151 if (psversion)
1152 {
1153 for (i = 0; i < PPD_MAX_VERS; i ++)
1154 if (!ppd->record.psversions[i][0] ||
1155 !strcasecmp(ppd->record.psversions[i], psversion))
1156 {
1157 ppd->matches ++;
1158 break;
1159 }
1160 }
1161
1162 if (type_str && ppd->record.type == type)
1163 ppd->matches ++;
1164
1165 if (ppd->matches)
1166 {
1167 fprintf(stderr, "DEBUG: [cups-driverd] %s matches with score %d!\n",
1168 ppd->record.name, ppd->matches);
1169 cupsArrayAdd(matches, ppd);
1170 }
b94498cf 1171 }
58dc1933
MS
1172 }
1173 else
1174 matches = PPDsByMakeModel;
ef416fc2 1175
58dc1933
MS
1176 for (ppd = (ppd_info_t *)cupsArrayFirst(matches);
1177 count > 0 && ppd;
1178 ppd = (ppd_info_t *)cupsArrayNext(matches))
1179 {
1180 /*
1181 * Skip invalid PPDs...
1182 */
1183
1184 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1185 ppd->record.type >= PPD_TYPE_DRV)
3d8365b8 1186 continue;
1187
b94498cf 1188 /*
1189 * Send this PPD...
1190 */
ef416fc2 1191
b94498cf 1192 if (!sent_header)
1193 {
1194 sent_header = 1;
ef416fc2 1195
b94498cf 1196 cupsdSendIPPHeader(IPP_OK, request_id);
1197 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1198 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
58dc1933
MS
1199 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
1200 "en-US");
b94498cf 1201 }
ef416fc2 1202
b94498cf 1203 fprintf(stderr, "DEBUG: [cups-driverd] Sending %s (%s)...\n",
1204 ppd->record.name, ppd->record.make_and_model);
ef416fc2 1205
b94498cf 1206 count --;
bd7854cb 1207
b94498cf 1208 cupsdSendIPPGroup(IPP_TAG_PRINTER);
f899b121 1209
b94498cf 1210 if (send_name)
1211 cupsdSendIPPString(IPP_TAG_NAME, "ppd-name", ppd->record.name);
ef416fc2 1212
b94498cf 1213 if (send_natural_language)
1214 {
1215 cupsdSendIPPString(IPP_TAG_LANGUAGE, "ppd-natural-language",
1216 ppd->record.languages[0]);
ef416fc2 1217
66ab9486
MS
1218 for (i = 1; i < PPD_MAX_LANG && ppd->record.languages[i][0]; i ++)
1219 cupsdSendIPPString(IPP_TAG_LANGUAGE, "", ppd->record.languages[i]);
b94498cf 1220 }
ef416fc2 1221
b94498cf 1222 if (send_make)
1223 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make", ppd->record.make);
ef416fc2 1224
b94498cf 1225 if (send_make_and_model)
1226 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make-and-model",
1227 ppd->record.make_and_model);
1228
1229 if (send_device_id)
1230 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-device-id",
1231 ppd->record.device_id);
1232
1233 if (send_product)
1234 {
1235 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-product",
1236 ppd->record.products[0]);
1237
66ab9486
MS
1238 for (i = 1; i < PPD_MAX_PROD && ppd->record.products[i][0]; i ++)
1239 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.products[i]);
b94498cf 1240 }
1241
1242 if (send_psversion)
1243 {
1244 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-psversion",
1245 ppd->record.psversions[0]);
1246
66ab9486
MS
1247 for (i = 1; i < PPD_MAX_VERS && ppd->record.psversions[i][0]; i ++)
1248 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.psversions[i]);
b94498cf 1249 }
1250
3d8365b8 1251 if (send_type)
1252 cupsdSendIPPString(IPP_TAG_KEYWORD, "ppd-type",
1253 ppd_types[ppd->record.type]);
1254
1255 if (send_model_number)
1256 cupsdSendIPPInteger(IPP_TAG_INTEGER, "ppd-model-number",
1257 ppd->record.model_number);
1258
b94498cf 1259 /*
1260 * If we have only requested the ppd-make attribute, then skip
1261 * the remaining PPDs with this make...
1262 */
1263
ed6e7faf
MS
1264 if (cupsArrayFind(requested, (void *)"ppd-make") &&
1265 cupsArrayCount(requested) == 1)
b94498cf 1266 {
1267 const char *this_make; /* This ppd-make */
1268
1269
66ab9486 1270 for (this_make = ppd->record.make,
58dc1933 1271 ppd = (ppd_info_t *)cupsArrayNext(matches);
66ab9486 1272 ppd;
58dc1933 1273 ppd = (ppd_info_t *)cupsArrayNext(matches))
b94498cf 1274 if (strcasecmp(this_make, ppd->record.make))
1275 break;
1276
58dc1933 1277 cupsArrayPrev(matches);
ef416fc2 1278 }
b94498cf 1279 }
1280
1281 if (!sent_header)
1282 {
1283 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
1284 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1285 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
1286 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
1287 }
ef416fc2 1288
1289 cupsdSendIPPTrailer();
1290
1291 return (0);
1292}
1293
1294
1295/*
1296 * 'load_ppds()' - Load PPD files recursively.
1297 */
1298
bd7854cb 1299static int /* O - 1 on success, 0 on failure */
ef416fc2 1300load_ppds(const char *d, /* I - Actual directory */
b94498cf 1301 const char *p, /* I - Virtual path in name */
1302 int descend) /* I - Descend into directories? */
ef416fc2 1303{
1304 int i; /* Looping var */
1305 cups_file_t *fp; /* Pointer to file */
1306 cups_dir_t *dir; /* Directory pointer */
1307 cups_dentry_t *dent; /* Directory entry */
1308 char filename[1024], /* Name of PPD or directory */
1309 line[256], /* Line from backend */
1310 *ptr, /* Pointer into name */
1311 name[128], /* Name of PPD file */
80ca4592 1312 lang_version[64], /* PPD LanguageVersion */
1313 lang_encoding[64], /* PPD LanguageEncoding */
ef416fc2 1314 country[64], /* Country code */
1315 manufacturer[256], /* Manufacturer */
1316 make_model[256], /* Make and Model */
1317 model_name[256], /* ModelName */
bd7854cb 1318 nick_name[256], /* NickName */
f899b121 1319 device_id[256], /* 1284DeviceID */
b94498cf 1320 product[256], /* Product */
5eb9da71
MS
1321 psversion[256], /* PSVersion */
1322 temp[512]; /* Temporary make and model */
3d8365b8 1323 int model_number, /* cupsModelNumber */
1324 type; /* ppd-type */
b94498cf 1325 cups_array_t *products, /* Product array */
1326 *psversions, /* PSVersion array */
1327 *cups_languages; /* cupsLanguages array */
ef416fc2 1328 ppd_info_t *ppd, /* New PPD file */
1329 key; /* Search key */
1330 int new_ppd; /* Is this a new PPD? */
1331 struct /* LanguageVersion translation table */
1332 {
1333 const char *version, /* LanguageVersion string */
1334 *language; /* Language code */
1335 } languages[] =
1336 {
dd1abb6b
MS
1337 { "chinese", "zh" },
1338 { "czech", "cs" },
1339 { "danish", "da" },
1340 { "dutch", "nl" },
1341 { "english", "en" },
1342 { "finnish", "fi" },
1343 { "french", "fr" },
1344 { "german", "de" },
1345 { "greek", "el" },
1346 { "hungarian", "hu" },
1347 { "italian", "it" },
1348 { "japanese", "ja" },
1349 { "korean", "ko" },
1350 { "norwegian", "no" },
1351 { "polish", "pl" },
1352 { "portuguese", "pt" },
1353 { "russian", "ru" },
1354 { "simplified chinese", "zh_CN" },
1355 { "slovak", "sk" },
1356 { "spanish", "es" },
1357 { "swedish", "sv" },
1358 { "traditional chinese", "zh_TW" },
1359 { "turkish", "tr" }
ef416fc2 1360 };
1361
1362
4509bb49
MS
1363 fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d);
1364
ef416fc2 1365 if ((dir = cupsDirOpen(d)) == NULL)
1366 {
839a51c8
MS
1367 if (errno != ENOENT)
1368 fprintf(stderr,
1369 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1370 d, strerror(errno));
1371
ef416fc2 1372 return (0);
1373 }
1374
1375 while ((dent = cupsDirRead(dir)) != NULL)
1376 {
bd7854cb 1377 /*
1378 * Skip files/directories starting with "."...
1379 */
1380
1381 if (dent->filename[0] == '.')
1382 continue;
1383
ef416fc2 1384 /*
1385 * See if this is a file...
1386 */
1387
1388 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
1389
1390 if (p[0])
1391 snprintf(name, sizeof(name), "%s/%s", p, dent->filename);
1392 else
1393 strlcpy(name, dent->filename, sizeof(name));
1394
1395 if (S_ISDIR(dent->fileinfo.st_mode))
1396 {
1397 /*
1398 * Do subdirectory...
1399 */
1400
b94498cf 1401 if (descend)
1402 if (!load_ppds(filename, name, 1))
1403 {
1404 cupsDirClose(dir);
1405 return (1);
1406 }
ef416fc2 1407
1408 continue;
1409 }
1410
1411 /*
1412 * See if this file has been scanned before...
1413 */
1414
66ab9486
MS
1415 strcpy(key.record.filename, name);
1416 strcpy(key.record.name, name);
ef416fc2 1417
66ab9486 1418 ppd = (ppd_info_t *)cupsArrayFind(PPDsByName, &key);
ef416fc2 1419
66ab9486
MS
1420 if (ppd &&
1421 ppd->record.size == dent->fileinfo.st_size &&
1422 ppd->record.mtime == dent->fileinfo.st_mtime)
1423 {
1424 do
ef416fc2 1425 {
1426 ppd->found = 1;
ef416fc2 1427 }
66ab9486
MS
1428 while ((ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) != NULL &&
1429 !strcasecmp(ppd->record.filename, name));
1430
1431 continue;
ef416fc2 1432 }
ef416fc2 1433
1434 /*
1435 * No, file is new/changed, so re-scan it...
1436 */
1437
1438 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1439 continue;
1440
1441 /*
1442 * Now see if this is a PPD file...
1443 */
1444
1445 line[0] = '\0';
1446 cupsFileGets(fp, line, sizeof(line));
1447
1448 if (strncmp(line, "*PPD-Adobe:", 11))
1449 {
1450 /*
4509bb49 1451 * Nope, treat it as a driver information file...
ef416fc2 1452 */
1453
4509bb49
MS
1454 load_drv(filename, name, fp, dent->fileinfo.st_mtime,
1455 dent->fileinfo.st_size);
ef416fc2 1456 continue;
1457 }
1458
1459 /*
1460 * Now read until we get the NickName field...
1461 */
1462
b94498cf 1463 cups_languages = cupsArrayNew(NULL, NULL);
1464 products = cupsArrayNew(NULL, NULL);
1465 psversions = cupsArrayNew(NULL, NULL);
f899b121 1466
80ca4592 1467 model_name[0] = '\0';
1468 nick_name[0] = '\0';
1469 manufacturer[0] = '\0';
1470 device_id[0] = '\0';
1471 lang_encoding[0] = '\0';
1472 strcpy(lang_version, "en");
3d8365b8 1473 model_number = 0;
1474 type = PPD_TYPE_POSTSCRIPT;
ef416fc2 1475
1476 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
1477 {
1478 if (!strncmp(line, "*Manufacturer:", 14))
1479 sscanf(line, "%*[^\"]\"%255[^\"]", manufacturer);
1480 else if (!strncmp(line, "*ModelName:", 11))
1481 sscanf(line, "%*[^\"]\"%127[^\"]", model_name);
80ca4592 1482 else if (!strncmp(line, "*LanguageEncoding:", 18))
1483 sscanf(line, "%*[^:]:%63s", lang_encoding);
ef416fc2 1484 else if (!strncmp(line, "*LanguageVersion:", 17))
80ca4592 1485 sscanf(line, "%*[^:]:%63s", lang_version);
ef416fc2 1486 else if (!strncmp(line, "*NickName:", 10))
1487 sscanf(line, "%*[^\"]\"%255[^\"]", nick_name);
89d46774 1488 else if (!strncasecmp(line, "*1284DeviceID:", 14))
58dc1933 1489 {
bd7854cb 1490 sscanf(line, "%*[^\"]\"%255[^\"]", device_id);
58dc1933
MS
1491
1492 // Make sure device ID ends with a semicolon...
1493 if (device_id[0] && device_id[strlen(device_id) - 1] != ';')
1494 strlcat(device_id, ";", sizeof(device_id));
1495 }
3d8365b8 1496 else if (!strncmp(line, "*Product:", 9))
f899b121 1497 {
5eb9da71
MS
1498 if (sscanf(line, "%*[^\"]\"(%255[^)]", product) == 1)
1499 cupsArrayAdd(products, strdup(product));
f899b121 1500 }
3d8365b8 1501 else if (!strncmp(line, "*PSVersion:", 11))
b94498cf 1502 {
1503 sscanf(line, "%*[^\"]\"%255[^\"]", psversion);
1504 cupsArrayAdd(psversions, strdup(psversion));
1505 }
3d8365b8 1506 else if (!strncmp(line, "*cupsLanguages:", 15))
b94498cf 1507 {
1508 char *start; /* Start of language */
1509
1510
1511 for (start = line + 15; *start && isspace(*start & 255); start ++);
1512
1513 if (*start++ == '\"')
1514 {
1515 while (*start)
1516 {
1517 for (ptr = start + 1;
1518 *ptr && *ptr != '\"' && !isspace(*ptr & 255);
1519 ptr ++);
1520
1521 if (*ptr)
1522 {
1523 *ptr++ = '\0';
1524
1525 while (isspace(*ptr & 255))
1526 *ptr++ = '\0';
1527 }
1528
1529 cupsArrayAdd(cups_languages, strdup(start));
1530 start = ptr;
1531 }
1532 }
1533 }
3d8365b8 1534 else if (!strncmp(line, "*cupsFax:", 9))
1535 {
1536 for (ptr = line + 9; isspace(*ptr & 255); ptr ++);
1537
1538 if (!strncasecmp(ptr, "true", 4))
1539 type = PPD_TYPE_FAX;
1540 }
a0f6818e 1541 else if (!strncmp(line, "*cupsFilter:", 12) && type == PPD_TYPE_POSTSCRIPT)
3d8365b8 1542 {
1543 if (strstr(line + 12, "application/vnd.cups-raster"))
1544 type = PPD_TYPE_RASTER;
1545 else if (strstr(line + 12, "application/vnd.cups-pdf"))
1546 type = PPD_TYPE_PDF;
3d8365b8 1547 }
1548 else if (!strncmp(line, "*cupsModelNumber:", 17))
1549 sscanf(line, "*cupsModelNumber:%d", &model_number);
ef416fc2 1550 else if (!strncmp(line, "*OpenUI", 7))
1551 {
1552 /*
1553 * Stop early if we have a NickName or ModelName attributes
1554 * before the first OpenUI...
1555 */
1556
b94498cf 1557 if ((model_name[0] || nick_name[0]) && cupsArrayCount(products) > 0 &&
1558 cupsArrayCount(psversions) > 0)
ef416fc2 1559 break;
1560 }
ef416fc2 1561 }
1562
1563 /*
1564 * Close the file...
1565 */
1566
1567 cupsFileClose(fp);
1568
1569 /*
1570 * See if we got all of the required info...
1571 */
1572
1573 if (nick_name[0])
80ca4592 1574 cupsCharsetToUTF8((cups_utf8_t *)make_model, nick_name,
1575 sizeof(make_model), _ppdGetEncoding(lang_encoding));
ef416fc2 1576 else
1577 strcpy(make_model, model_name);
1578
1579 while (isspace(make_model[0] & 255))
1580 _cups_strcpy(make_model, make_model + 1);
1581
b94498cf 1582 if (!make_model[0] || cupsArrayCount(products) == 0 ||
1583 cupsArrayCount(psversions) == 0)
f899b121 1584 {
1585 /*
1586 * We don't have all the info needed, so skip this file...
1587 */
1588
1589 if (!make_model[0])
1590 fprintf(stderr, "WARNING: Missing NickName and ModelName in %s!\n",
1591 filename);
1592
1593 if (cupsArrayCount(products) == 0)
1594 fprintf(stderr, "WARNING: Missing Product in %s!\n", filename);
1595
b94498cf 1596 if (cupsArrayCount(psversions) == 0)
1597 fprintf(stderr, "WARNING: Missing PSVersion in %s!\n", filename);
f899b121 1598
b94498cf 1599 free_array(products);
1600 free_array(psversions);
1601 free_array(cups_languages);
f899b121 1602
1603 continue;
1604 }
ef416fc2 1605
3d8365b8 1606 if (model_name[0])
1607 cupsArrayAdd(products, strdup(model_name));
1608
ef416fc2 1609 /*
5eb9da71 1610 * Normalize the make and model string...
ef416fc2 1611 */
1612
1613 while (isspace(manufacturer[0] & 255))
1614 _cups_strcpy(manufacturer, manufacturer + 1);
1615
5eb9da71
MS
1616 if (!strncasecmp(make_model, manufacturer, strlen(manufacturer)))
1617 strlcpy(temp, make_model, sizeof(temp));
1618 else
1619 snprintf(temp, sizeof(temp), "%s %s", manufacturer, make_model);
1620
1621 _ppdNormalizeMakeAndModel(temp, make_model, sizeof(make_model));
1622
1623 /*
1624 * See if we got a manufacturer...
1625 */
1626
ef416fc2 1627 if (!manufacturer[0] || !strcmp(manufacturer, "ESP"))
1628 {
1629 /*
1630 * Nope, copy the first part of the make and model then...
1631 */
1632
1633 strlcpy(manufacturer, make_model, sizeof(manufacturer));
1634
1635 /*
1636 * Truncate at the first space, dash, or slash, or make the
1637 * manufacturer "Other"...
1638 */
1639
1640 for (ptr = manufacturer; *ptr; ptr ++)
1641 if (*ptr == ' ' || *ptr == '-' || *ptr == '/')
1642 break;
1643
1644 if (*ptr && ptr > manufacturer)
1645 *ptr = '\0';
ef416fc2 1646 else
1647 strcpy(manufacturer, "Other");
ef416fc2 1648 }
1649 else if (!strncasecmp(manufacturer, "LHAG", 4) ||
1650 !strncasecmp(manufacturer, "linotype", 8))
1651 strcpy(manufacturer, "LHAG");
5eb9da71
MS
1652 else if (!strncasecmp(manufacturer, "Hewlett", 7))
1653 strcpy(manufacturer, "HP");
ef416fc2 1654
1655 /*
80ca4592 1656 * Fix the lang_version as needed...
ef416fc2 1657 */
1658
80ca4592 1659 if ((ptr = strchr(lang_version, '-')) != NULL)
ef416fc2 1660 *ptr++ = '\0';
80ca4592 1661 else if ((ptr = strchr(lang_version, '_')) != NULL)
ef416fc2 1662 *ptr++ = '\0';
1663
1664 if (ptr)
1665 {
1666 /*
1667 * Setup the country suffix...
1668 */
1669
1670 country[0] = '_';
1671 _cups_strcpy(country + 1, ptr);
1672 }
1673 else
1674 {
1675 /*
1676 * No country suffix...
1677 */
1678
1679 country[0] = '\0';
1680 }
1681
1682 for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++)
80ca4592 1683 if (!strcasecmp(languages[i].version, lang_version))
ef416fc2 1684 break;
1685
1686 if (i < (int)(sizeof(languages) / sizeof(languages[0])))
1687 {
1688 /*
1689 * Found a known language...
1690 */
1691
80ca4592 1692 snprintf(lang_version, sizeof(lang_version), "%s%s",
1693 languages[i].language, country);
ef416fc2 1694 }
1695 else
1696 {
1697 /*
1698 * Unknown language; use "xx"...
1699 */
1700
80ca4592 1701 strcpy(lang_version, "xx");
ef416fc2 1702 }
1703
1704 /*
5eb9da71 1705 * Record the PPD file...
ef416fc2 1706 */
1707
1708 new_ppd = !ppd;
1709
1710 if (new_ppd)
1711 {
1712 /*
1713 * Add new PPD file...
1714 */
1715
1716 fprintf(stderr, "DEBUG: [cups-driverd] Adding ppd \"%s\"...\n", name);
1717
66ab9486
MS
1718 ppd = add_ppd(name, name, lang_version, manufacturer, make_model,
1719 device_id, (char *)cupsArrayFirst(products),
b94498cf 1720 (char *)cupsArrayFirst(psversions),
3d8365b8 1721 dent->fileinfo.st_mtime, dent->fileinfo.st_size,
ed6e7faf 1722 model_number, type, "file");
b94498cf 1723
1724 if (!ppd)
ef416fc2 1725 {
1726 cupsDirClose(dir);
1727 return (0);
1728 }
1729 }
1730 else
1731 {
1732 /*
1733 * Update existing record...
1734 */
1735
1736 fprintf(stderr, "DEBUG: [cups-driverd] Updating ppd \"%s\"...\n", name);
1737
1738 memset(ppd, 0, sizeof(ppd_info_t));
1739
3d8365b8 1740 ppd->found = 1;
1741 ppd->record.mtime = dent->fileinfo.st_mtime;
1742 ppd->record.size = dent->fileinfo.st_size;
1743 ppd->record.model_number = model_number;
1744 ppd->record.type = type;
ef416fc2 1745
1746 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
1747 strlcpy(ppd->record.make, manufacturer, sizeof(ppd->record.make));
1748 strlcpy(ppd->record.make_and_model, make_model,
1749 sizeof(ppd->record.make_and_model));
b94498cf 1750 strlcpy(ppd->record.languages[0], lang_version,
1751 sizeof(ppd->record.languages[0]));
1752 strlcpy(ppd->record.products[0], (char *)cupsArrayFirst(products),
1753 sizeof(ppd->record.products[0]));
1754 strlcpy(ppd->record.psversions[0], (char *)cupsArrayFirst(psversions),
1755 sizeof(ppd->record.psversions[0]));
bd7854cb 1756 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
ef416fc2 1757 }
1758
f899b121 1759 /*
b94498cf 1760 * Add remaining products, versions, and languages...
f899b121 1761 */
1762
b94498cf 1763 for (i = 1;
1764 i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL;
1765 i ++)
1766 strlcpy(ppd->record.products[i], ptr,
1767 sizeof(ppd->record.products[0]));
1768
1769 for (i = 1;
1770 i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL;
1771 i ++)
1772 strlcpy(ppd->record.psversions[i], ptr,
1773 sizeof(ppd->record.psversions[0]));
f899b121 1774
b94498cf 1775 for (i = 1, ptr = (char *)cupsArrayFirst(cups_languages);
1776 i < PPD_MAX_LANG && ptr;
1777 i ++, ptr = (char *)cupsArrayNext(cups_languages))
1778 strlcpy(ppd->record.languages[i], ptr,
1779 sizeof(ppd->record.languages[0]));
1780
1781 /*
1782 * Free products, versions, and languages...
1783 */
1784
1785 free_array(cups_languages);
1786 free_array(products);
1787 free_array(psversions);
f899b121 1788
ef416fc2 1789 ChangedPPD = 1;
1790 }
1791
1792 cupsDirClose(dir);
1793
1794 return (1);
1795}
1796
1797
4509bb49
MS
1798/*
1799 * 'load_drv()' - Load the PPDs from a driver information file.
1800 */
1801
1802static int /* O - 1 on success, 0 on failure */
1803load_drv(const char *filename, /* I - Actual filename */
1804 const char *name, /* I - Name to the rest of the world */
1805 cups_file_t *fp, /* I - File to read from */
1806 time_t mtime, /* I - Mod time of driver info file */
1807 off_t size) /* I - Size of driver info file */
1808{
1809 ppdcSource *src; // Driver information file
1810 ppdcDriver *d; // Current driver
1811 ppdcAttr *device_id, // 1284DeviceID attribute
1812 *product, // Current product value
1813 *ps_version, // PSVersion attribute
1814 *cups_fax, // cupsFax attribute
1815 *nick_name; // NickName attribute
1816 ppdcFilter *filter; // Current filter
1817 bool product_found; // Found product?
1818 char uri[1024], // Driver URI
1819 make_model[1024]; // Make and model
1820 int type; // Driver type
1821
1822
66ab9486
MS
1823 /*
1824 * Load the driver info file...
1825 */
1826
4509bb49
MS
1827 src = new ppdcSource(filename, fp);
1828
1829 if (src->drivers->count == 0)
1830 {
1831 fprintf(stderr,
1832 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1833 filename);
e4572d57 1834 src->release();
4509bb49
MS
1835 return (0);
1836 }
1837
66ab9486
MS
1838 /*
1839 * Add a dummy entry for the file...
1840 */
1841
1842 add_ppd(filename, filename, "", "", "", "", "", "", mtime, size, 0,
ed6e7faf 1843 PPD_TYPE_DRV, "drv");
66ab9486
MS
1844
1845 /*
1846 * Then the drivers in the file...
1847 */
1848
4509bb49
MS
1849 for (d = (ppdcDriver *)src->drivers->first();
1850 d;
1851 d = (ppdcDriver *)src->drivers->next())
1852 {
1853 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0,
1854 "/%s/%s", name,
1855 d->file_name ? d->file_name->value :
1856 d->pc_file_name->value);
1857
1858 device_id = d->find_attr("1284DeviceID", NULL);
1859 ps_version = d->find_attr("PSVersion", NULL);
1860 nick_name = d->find_attr("NickName", NULL);
1861
1862 if (nick_name)
1863 strlcpy(make_model, nick_name->value->value, sizeof(make_model));
1864 else if (strncasecmp(d->model_name->value, d->manufacturer->value,
1865 strlen(d->manufacturer->value)))
1866 snprintf(make_model, sizeof(make_model), "%s %s, %s",
1867 d->manufacturer->value, d->model_name->value,
1868 d->version->value);
1869 else
1870 snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value,
1871 d->version->value);
1872
1873 if ((cups_fax = d->find_attr("cupsFax", NULL)) != NULL &&
1874 !strcasecmp(cups_fax->value->value, "true"))
1875 type = PPD_TYPE_FAX;
1876 else if (d->type == PPDC_DRIVER_PS)
1877 type = PPD_TYPE_POSTSCRIPT;
1878 else if (d->type != PPDC_DRIVER_CUSTOM)
1879 type = PPD_TYPE_RASTER;
1880 else
1881 {
1882 for (filter = (ppdcFilter *)d->filters->first(),
1883 type = PPD_TYPE_POSTSCRIPT;
1884 filter;
1885 filter = (ppdcFilter *)d->filters->next())
1886 if (strcasecmp(filter->mime_type->value, "application/vnd.cups-raster"))
1887 type = PPD_TYPE_RASTER;
1888 else if (strcasecmp(filter->mime_type->value,
1889 "application/vnd.cups-pdf"))
1890 type = PPD_TYPE_PDF;
1891 }
1892
1893 for (product = (ppdcAttr *)d->attrs->first(), product_found = false;
1894 product;
1895 product = (ppdcAttr *)d->attrs->next())
1896 if (!strcmp(product->name->value, "Product"))
1897 {
1898 product_found = true;
1899
66ab9486 1900 add_ppd(filename, uri, "en", d->manufacturer->value, make_model,
4509bb49
MS
1901 device_id ? device_id->value->value : "",
1902 product->value->value,
1903 ps_version ? ps_version->value->value : "(3010) 0",
ed6e7faf 1904 mtime, size, d->model_number, type, "drv");
4509bb49
MS
1905 }
1906
1907 if (!product_found)
66ab9486 1908 add_ppd(filename, uri, "en", d->manufacturer->value, make_model,
4509bb49
MS
1909 device_id ? device_id->value->value : "",
1910 d->model_name->value,
1911 ps_version ? ps_version->value->value : "(3010) 0",
ed6e7faf 1912 mtime, size, d->model_number, type, "drv");
4509bb49
MS
1913 }
1914
e4572d57 1915 src->release();
4509bb49
MS
1916
1917 return (1);
1918}
1919
1920
ef416fc2 1921/*
1922 * 'load_drivers()' - Load driver-generated PPD files.
1923 */
1924
bd7854cb 1925static int /* O - 1 on success, 0 on failure */
ed6e7faf
MS
1926load_drivers(cups_array_t *include, /* I - Drivers to include */
1927 cups_array_t *exclude) /* I - Drivers to exclude */
ef416fc2 1928{
b94498cf 1929 int i; /* Looping var */
1930 char *start, /* Start of value */
1931 *ptr; /* Pointer into string */
1932 const char *server_bin; /* CUPS_SERVERBIN env variable */
ef416fc2 1933 char drivers[1024]; /* Location of driver programs */
c934a06c
MS
1934 int pid; /* Process ID for driver program */
1935 cups_file_t *fp; /* Pipe to driver program */
ef416fc2 1936 cups_dir_t *dir; /* Directory pointer */
1937 cups_dentry_t *dent; /* Directory entry */
c934a06c
MS
1938 char *argv[3], /* Arguments for command */
1939 filename[1024], /* Name of driver */
ef416fc2 1940 line[2048], /* Line from driver */
1941 name[512], /* ppd-name */
ef416fc2 1942 make[128], /* ppd-make */
b94498cf 1943 make_and_model[128], /* ppd-make-and-model */
1944 device_id[128], /* ppd-device-id */
1945 languages[128], /* ppd-natural-language */
1946 product[128], /* ppd-product */
3d8365b8 1947 psversion[128], /* ppd-psversion */
1948 type_str[128]; /* ppd-type */
1949 int type; /* PPD type */
b94498cf 1950 ppd_info_t *ppd; /* Newly added PPD */
ef416fc2 1951
1952
1953 /*
1954 * Try opening the driver directory...
1955 */
1956
1957 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
1958 server_bin = CUPS_SERVERBIN;
1959
1960 snprintf(drivers, sizeof(drivers), "%s/driver", server_bin);
1961
1962 if ((dir = cupsDirOpen(drivers)) == NULL)
1963 {
1964 fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory "
839a51c8
MS
1965 "\"%s\": %s\n",
1966 drivers, strerror(errno));
ef416fc2 1967 return (0);
1968 }
1969
1970 /*
1971 * Loop through all of the device drivers...
1972 */
1973
c934a06c
MS
1974 argv[1] = (char *)"list";
1975 argv[2] = NULL;
1976
ef416fc2 1977 while ((dent = cupsDirRead(dir)) != NULL)
1978 {
1979 /*
1980 * Only look at executable files...
1981 */
1982
1983 if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode))
1984 continue;
1985
ed6e7faf
MS
1986 /*
1987 * Include/exclude specific drivers...
1988 */
1989
1990 if (cupsArrayFind(exclude, dent->filename) ||
1991 (include && !cupsArrayFind(include, dent->filename)))
1992 continue;
1993
ef416fc2 1994 /*
1995 * Run the driver with no arguments and collect the output...
1996 */
1997
c934a06c
MS
1998 argv[0] = dent->filename;
1999 snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename);
2000
2001 if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL)
ef416fc2 2002 {
c934a06c 2003 while (cupsFileGets(fp, line, sizeof(line)))
ef416fc2 2004 {
2005 /*
2006 * Each line is of the form:
2007 *
f899b121 2008 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
b94498cf 2009 * "ppd-device-id" "ppd-product" "ppd-psversion"
ef416fc2 2010 */
2011
bd7854cb 2012 device_id[0] = '\0';
f899b121 2013 product[0] = '\0';
b94498cf 2014 psversion[0] = '\0';
3d8365b8 2015 strcpy(type_str, "postscript");
bd7854cb 2016
2017 if (sscanf(line, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
b94498cf 2018 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
3d8365b8 2019 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
2020 "%*[ \t]\"%127[^\"]\"",
b94498cf 2021 name, languages, make, make_and_model,
3d8365b8 2022 device_id, product, psversion, type_str) < 4)
ef416fc2 2023 {
2024 /*
2025 * Bad format; strip trailing newline and write an error message.
2026 */
2027
2028 if (line[strlen(line) - 1] == '\n')
2029 line[strlen(line) - 1] = '\0';
2030
2031 fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
2032 dent->filename, line);
2033 break;
2034 }
2035 else
2036 {
2037 /*
2038 * Add the device to the array of available devices...
2039 */
2040
b94498cf 2041 if ((start = strchr(languages, ',')) != NULL)
2042 *start++ = '\0';
2043
3d8365b8 2044 for (type = 0;
2045 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
2046 type ++)
2047 if (!strcmp(type_str, ppd_types[type]))
2048 break;
2049
2050 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
2051 {
c934a06c
MS
2052 fprintf(stderr,
2053 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
3d8365b8 2054 type_str);
2055 type = PPD_TYPE_UNKNOWN;
2056 }
2057
66ab9486 2058 ppd = add_ppd("", name, languages, make, make_and_model, device_id,
ed6e7faf 2059 product, psversion, 0, 0, 0, type, dent->filename);
b94498cf 2060
2061 if (!ppd)
ef416fc2 2062 {
2063 cupsDirClose(dir);
c934a06c 2064 cupsFileClose(fp);
ef416fc2 2065 return (0);
2066 }
2067
b94498cf 2068 if (start && *start)
2069 {
2070 for (i = 1; i < PPD_MAX_LANG && *start; i ++)
2071 {
2072 if ((ptr = strchr(start, ',')) != NULL)
2073 *ptr++ = '\0';
2074 else
2075 ptr = start + strlen(start);
2076
2077 strlcpy(ppd->record.languages[i], start,
2078 sizeof(ppd->record.languages[0]));
2079
2080 start = ptr;
2081 }
2082 }
2083
ef416fc2 2084 fprintf(stderr, "DEBUG: [cups-driverd] Added dynamic PPD \"%s\"...\n",
2085 name);
2086 }
2087 }
2088
c934a06c 2089 cupsFileClose(fp);
ef416fc2 2090 }
2091 else
2092 fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
2093 filename, strerror(errno));
2094 }
2095
2096 cupsDirClose(dir);
2097
2098 return (1);
2099}
2100
2101
58dc1933
MS
2102/*
2103 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2104 * ID.
2105 */
2106
2107static regex_t * /* O - Regular expression */
2108regex_device_id(const char *device_id) /* I - IEEE-1284 device ID */
2109{
2110 char res[2048], /* Regular expression string */
2111 *ptr; /* Pointer into string */
2112 regex_t *re; /* Regular expression */
2113 int cmd; /* Command set string? */
2114
2115
2116 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id);
2117
2118 /*
2119 * Scan the device ID string and insert class, command set, manufacturer, and
2120 * model attributes to match. We assume that the device ID in the PPD and the
2121 * device ID reported by the device itself use the same attribute names and
2122 * order of attributes.
2123 */
2124
2125 ptr = res;
2126
2127 while (*device_id && ptr < (res + sizeof(res) - 6))
2128 {
2129 cmd = !strncasecmp(device_id, "COMMAND SET:", 12) ||
2130 !strncasecmp(device_id, "CMD:", 4);
2131
2132 if (cmd || !strncasecmp(device_id, "MANUFACTURER:", 13) ||
2133 !strncasecmp(device_id, "MFG:", 4) ||
2134 !strncasecmp(device_id, "MFR:", 4) ||
2135 !strncasecmp(device_id, "MODEL:", 6) ||
2136 !strncasecmp(device_id, "MDL:", 4))
2137 {
2138 if (ptr > res)
2139 {
2140 *ptr++ = '.';
2141 *ptr++ = '*';
2142 }
2143
2144 *ptr++ = '(';
2145
2146 while (*device_id && *device_id != ';' && ptr < (res + sizeof(res) - 4))
2147 {
2148 if (strchr("[]{}().*\\|", *device_id))
2149 *ptr++ = '\\';
2150 *ptr++ = *device_id++;
2151 }
2152
2153 if (*device_id == ';' || !*device_id)
2154 *ptr++ = ';';
2155 *ptr++ = ')';
2156 if (cmd)
2157 *ptr++ = '?';
2158 }
2159 else if ((device_id = strchr(device_id, ';')) == NULL)
2160 break;
2161 else
2162 device_id ++;
2163 }
2164
2165 *ptr = '\0';
2166
2167 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res);
2168
2169 /*
2170 * Compile the regular expression and return...
2171 */
2172
2173 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2174 {
2175 if (!regcomp(re, res, REG_EXTENDED | REG_ICASE))
2176 {
2177 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr);
2178 return (re);
2179 }
2180
2181 free(re);
2182 }
2183
2184 return (NULL);
2185}
2186
2187
2188/*
2189 * 'regex_string()' - Construct a regular expression to compare a simple string.
2190 */
2191
2192static regex_t * /* O - Regular expression */
2193regex_string(const char *s) /* I - String to compare */
2194{
2195 char res[2048], /* Regular expression string */
2196 *ptr; /* Pointer into string */
2197 regex_t *re; /* Regular expression */
2198
2199
2200 fprintf(stderr, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s);
2201
2202 /*
2203 * Convert the string to a regular expression, escaping special characters
2204 * as needed.
2205 */
2206
2207 ptr = res;
2208
2209 while (*s && ptr < (res + sizeof(res) - 2))
2210 {
2211 if (strchr("[]{}().*\\", *s))
2212 *ptr++ = '\\';
2213
2214 *ptr++ = *s++;
2215 }
2216
2217 *ptr = '\0';
2218
2219 fprintf(stderr, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res);
2220
2221 /*
2222 * Create a case-insensitive regular expression...
2223 */
2224
2225 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2226 {
2227 if (!regcomp(re, res, REG_ICASE))
2228 {
2229 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr);
2230 return (re);
2231 }
2232
2233 free(re);
2234 }
2235
2236 return (NULL);
2237}
2238
2239
ef416fc2 2240/*
4509bb49 2241 * End of "$Id$".
ef416fc2 2242 */