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