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