]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-driverd.cxx
Merge changes from CUPS 1.6svn-r10267.
[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(const char *filename);
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 || argc == 3) && !strcmp(argv[1], "dump"))
178 return (dump_ppds_dat(argv[2]));
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(const char *filename) /* I - Filename */
767 {
768 char temp[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 if (filename)
777 strlcpy(temp, filename, sizeof(temp));
778 else
779 temp[0] = '\0';
780
781 load_ppds_dat(temp, sizeof(temp), 0);
782
783 puts("mtime,size,model_number,type,filename,name,languages0,products0,"
784 "psversions0,make,make_and_model,device_id,scheme");
785 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
786 ppd;
787 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
788 printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
789 "\"%s\",\"%s\"\n",
790 (int)ppd->record.mtime, (long)ppd->record.size,
791 ppd->record.model_number, ppd->record.type, ppd->record.filename,
792 ppd->record.name, ppd->record.languages[0], ppd->record.products[0],
793 ppd->record.psversions[0], ppd->record.make,
794 ppd->record.make_and_model, ppd->record.device_id,
795 ppd->record.scheme);
796
797 return (0);
798 }
799
800
801 /*
802 * 'free_array()' - Free an array of strings.
803 */
804
805 static void
806 free_array(cups_array_t *a) /* I - Array to free */
807 {
808 char *ptr; /* Pointer to string */
809
810
811 for (ptr = (char *)cupsArrayFirst(a);
812 ptr;
813 ptr = (char *)cupsArrayNext(a))
814 free(ptr);
815
816 cupsArrayDelete(a);
817 }
818
819
820 /*
821 * 'list_ppds()' - List PPD files.
822 */
823
824 static int /* O - Exit code */
825 list_ppds(int request_id, /* I - Request ID */
826 int limit, /* I - Limit */
827 const char *opt) /* I - Option argument */
828 {
829 int i; /* Looping vars */
830 int count; /* Number of PPDs to send */
831 ppd_info_t *ppd; /* Current PPD file */
832 cups_file_t *fp; /* ppds.dat file */
833 char filename[1024], /* ppds.dat filename */
834 model[1024]; /* Model directory */
835 const char *cups_datadir; /* CUPS_DATADIR environment variable */
836 int num_options; /* Number of options */
837 cups_option_t *options; /* Options */
838 cups_array_t *requested, /* requested-attributes values */
839 *include, /* PPD schemes to include */
840 *exclude; /* PPD schemes to exclude */
841 const char *device_id, /* ppd-device-id option */
842 *language, /* ppd-natural-language option */
843 *make, /* ppd-make option */
844 *make_and_model, /* ppd-make-and-model option */
845 *model_number_str, /* ppd-model-number option */
846 *product, /* ppd-product option */
847 *psversion, /* ppd-psversion option */
848 *type_str; /* ppd-type option */
849 int model_number, /* ppd-model-number value */
850 type, /* ppd-type value */
851 make_and_model_len, /* Length of ppd-make-and-model */
852 product_len, /* Length of ppd-product */
853 send_device_id, /* Send ppd-device-id? */
854 send_make, /* Send ppd-make? */
855 send_make_and_model, /* Send ppd-make-and-model? */
856 send_model_number, /* Send ppd-model-number? */
857 send_name, /* Send ppd-name? */
858 send_natural_language, /* Send ppd-natural-language? */
859 send_product, /* Send ppd-product? */
860 send_psversion, /* Send ppd-psversion? */
861 send_type, /* Send ppd-type? */
862 sent_header; /* Sent the IPP header? */
863 regex_t *device_id_re, /* Regular expression for matching device ID */
864 *make_and_model_re; /* Regular expression for matching make and model */
865 regmatch_t re_matches[6]; /* Regular expression matches */
866 cups_array_t *matches; /* Matching PPDs */
867
868
869 fprintf(stderr,
870 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
871 "opt=\"%s\"\n", request_id, limit, opt);
872
873 /*
874 * See if we a PPD database file...
875 */
876
877 filename[0] = '\0';
878 load_ppds_dat(filename, sizeof(filename), 1);
879
880 /*
881 * Load all PPDs in the specified directory and below...
882 */
883
884 if ((cups_datadir = getenv("CUPS_DATADIR")) == NULL)
885 cups_datadir = CUPS_DATADIR;
886
887 Inodes = cupsArrayNew((cups_array_func_t)compare_inodes, NULL);
888
889 snprintf(model, sizeof(model), "%s/model", cups_datadir);
890 load_ppds(model, "", 1);
891
892 snprintf(model, sizeof(model), "%s/drv", cups_datadir);
893 load_ppds(model, "", 1);
894
895 #ifdef __APPLE__
896 /*
897 * Load PPDs from standard Mac OS X locations...
898 */
899
900 load_ppds("/Library/Printers",
901 "Library/Printers", 0);
902 load_ppds("/Library/Printers/PPDs/Contents/Resources",
903 "Library/Printers/PPDs/Contents/Resources", 0);
904 load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
905 "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
906 load_ppds("/System/Library/Printers",
907 "System/Library/Printers", 0);
908 load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
909 "System/Library/Printers/PPDs/Contents/Resources", 0);
910 load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
911 "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
912
913 #elif defined(__linux)
914 /*
915 * Load PPDs from LSB-defined locations...
916 */
917
918 if (!access("/usr/local/share/ppd", 0))
919 load_ppds("/usr/local/share/ppd", "lsb/local", 1);
920 if (!access("/usr/share/ppd", 0))
921 load_ppds("/usr/share/ppd", "lsb/usr", 1);
922 if (!access("/opt/share/ppd", 0))
923 load_ppds("/opt/share/ppd", "lsb/opt", 1);
924 #endif /* __APPLE__ */
925
926 /*
927 * Cull PPD files that are no longer present...
928 */
929
930 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
931 ppd;
932 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
933 if (!ppd->found)
934 {
935 /*
936 * Remove this PPD file from the list...
937 */
938
939 cupsArrayRemove(PPDsByName, ppd);
940 cupsArrayRemove(PPDsByMakeModel, ppd);
941 free(ppd);
942
943 ChangedPPD = 1;
944 }
945
946 /*
947 * Write the new ppds.dat file...
948 */
949
950 fprintf(stderr, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD);
951
952 if (ChangedPPD)
953 {
954 char newname[1024]; /* New filename */
955
956 snprintf(newname, sizeof(newname), "%s.%d", filename, (int)getpid());
957
958 if ((fp = cupsFileOpen(newname, "w")) != NULL)
959 {
960 unsigned ppdsync = PPD_SYNC; /* Sync word */
961
962 cupsFileWrite(fp, (char *)&ppdsync, sizeof(ppdsync));
963
964 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
965 ppd;
966 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
967 cupsFileWrite(fp, (char *)&(ppd->record), sizeof(ppd_rec_t));
968
969 cupsFileClose(fp);
970
971 if (rename(newname, filename))
972 fprintf(stderr, "ERROR: [cups-driverd] Unable to rename \"%s\" - %s\n",
973 newname, strerror(errno));
974 else
975 fprintf(stderr, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
976 filename, cupsArrayCount(PPDsByName));
977 }
978 else
979 fprintf(stderr, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
980 filename, strerror(errno));
981 }
982 else
983 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr);
984
985 /*
986 * Scan for dynamic PPD files...
987 */
988
989 num_options = cupsParseOptions(opt, 0, &options);
990 exclude = cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
991 num_options, options));
992 include = cupsdCreateStringsArray(cupsGetOption("include-schemes",
993 num_options, options));
994
995 load_drivers(include, exclude);
996
997 /*
998 * Add the raw driver...
999 */
1000
1001 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
1002 PPD_TYPE_UNKNOWN, "raw");
1003
1004 /*
1005 * Send IPP attributes...
1006 */
1007
1008 requested = cupsdCreateStringsArray(
1009 cupsGetOption("requested-attributes", num_options,
1010 options));
1011 device_id = cupsGetOption("ppd-device-id", num_options, options);
1012 language = cupsGetOption("ppd-natural-language", num_options, options);
1013 make = cupsGetOption("ppd-make", num_options, options);
1014 make_and_model = cupsGetOption("ppd-make-and-model", num_options, options);
1015 model_number_str = cupsGetOption("ppd-model-number", num_options, options);
1016 product = cupsGetOption("ppd-product", num_options, options);
1017 psversion = cupsGetOption("ppd-psversion", num_options, options);
1018 type_str = cupsGetOption("ppd-type", num_options, options);
1019
1020 if (make_and_model)
1021 make_and_model_len = strlen(make_and_model);
1022 else
1023 make_and_model_len = 0;
1024
1025 if (product)
1026 product_len = strlen(product);
1027 else
1028 product_len = 0;
1029
1030 if (model_number_str)
1031 model_number = atoi(model_number_str);
1032 else
1033 model_number = 0;
1034
1035 if (type_str)
1036 {
1037 for (type = 0;
1038 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
1039 type ++)
1040 if (!strcmp(type_str, ppd_types[type]))
1041 break;
1042
1043 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
1044 {
1045 fprintf(stderr, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
1046 type_str);
1047 type_str = NULL;
1048 }
1049 }
1050 else
1051 type = 0;
1052
1053 for (i = 0; i < num_options; i ++)
1054 fprintf(stderr, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options[i].name,
1055 options[i].value);
1056
1057 if (!requested || cupsArrayFind(requested, (void *)"all") != NULL)
1058 {
1059 send_name = 1;
1060 send_make = 1;
1061 send_make_and_model = 1;
1062 send_model_number = 1;
1063 send_natural_language = 1;
1064 send_device_id = 1;
1065 send_product = 1;
1066 send_psversion = 1;
1067 send_type = 1;
1068 }
1069 else
1070 {
1071 send_name = cupsArrayFind(requested,
1072 (void *)"ppd-name") != NULL;
1073 send_make = cupsArrayFind(requested,
1074 (void *)"ppd-make") != NULL;
1075 send_make_and_model = cupsArrayFind(requested,
1076 (void *)"ppd-make-and-model") != NULL;
1077 send_model_number = cupsArrayFind(requested,
1078 (void *)"ppd-model-number") != NULL;
1079 send_natural_language = cupsArrayFind(requested,
1080 (void *)"ppd-natural-language") != NULL;
1081 send_device_id = cupsArrayFind(requested,
1082 (void *)"ppd-device-id") != NULL;
1083 send_product = cupsArrayFind(requested,
1084 (void *)"ppd-product") != NULL;
1085 send_psversion = cupsArrayFind(requested,
1086 (void *)"ppd-psversion") != NULL;
1087 send_type = cupsArrayFind(requested,
1088 (void *)"ppd-type") != NULL;
1089 }
1090
1091 /*
1092 * Send the content type header to the scheduler; request_id can only be
1093 * 0 when run manually since the scheduler enforces the IPP requirement for
1094 * a request ID from 1 to 2^31-1...
1095 */
1096
1097 if (request_id > 0)
1098 puts("Content-Type: application/ipp\n");
1099
1100 sent_header = 0;
1101
1102 if (limit <= 0 || limit > cupsArrayCount(PPDsByMakeModel))
1103 count = cupsArrayCount(PPDsByMakeModel);
1104 else
1105 count = limit;
1106
1107 if (device_id || language || make || make_and_model || model_number_str ||
1108 product)
1109 {
1110 matches = cupsArrayNew((cups_array_func_t)compare_matches, NULL);
1111
1112 if (device_id)
1113 device_id_re = regex_device_id(device_id);
1114 else
1115 device_id_re = NULL;
1116
1117 if (make_and_model)
1118 make_and_model_re = regex_string(make_and_model);
1119 else
1120 make_and_model_re = NULL;
1121
1122 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1123 ppd;
1124 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
1125 {
1126 /*
1127 * Filter PPDs based on make, model, product, language, model number,
1128 * and/or device ID using the "matches" score value. An exact match
1129 * for product, make-and-model, or device-id adds 3 to the score.
1130 * Partial matches for make-and-model yield 1 or 2 points, and matches
1131 * for the make and language add a single point. Results are then sorted
1132 * by score, highest score first.
1133 */
1134
1135 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1136 ppd->record.type >= PPD_TYPE_DRV)
1137 continue;
1138
1139 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1140 (include && !cupsArrayFind(include, ppd->record.scheme)))
1141 continue;
1142
1143 ppd->matches = 0;
1144
1145 if (device_id_re &&
1146 !regexec(device_id_re, ppd->record.device_id,
1147 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1148 re_matches, 0))
1149 {
1150 /*
1151 * Add the number of matching values from the device ID - it will be
1152 * at least 2 (manufacturer and model), and as much as 3 (command set).
1153 */
1154
1155 for (i = 1; i < (int)(sizeof(re_matches) / sizeof(re_matches[0])); i ++)
1156 if (re_matches[i].rm_so >= 0)
1157 ppd->matches ++;
1158 }
1159
1160 if (language)
1161 {
1162 for (i = 0; i < PPD_MAX_LANG; i ++)
1163 if (!ppd->record.languages[i][0])
1164 break;
1165 else if (!strcmp(ppd->record.languages[i], language))
1166 {
1167 ppd->matches ++;
1168 break;
1169 }
1170 }
1171
1172 if (make && !_cups_strcasecmp(ppd->record.make, make))
1173 ppd->matches ++;
1174
1175 if (make_and_model_re &&
1176 !regexec(make_and_model_re, ppd->record.make_and_model,
1177 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1178 re_matches, 0))
1179 {
1180 // See how much of the make-and-model string we matched...
1181 if (re_matches[0].rm_so == 0)
1182 {
1183 if (re_matches[0].rm_eo == make_and_model_len)
1184 ppd->matches += 3; // Exact match
1185 else
1186 ppd->matches += 2; // Prefix match
1187 }
1188 else
1189 ppd->matches ++; // Infix match
1190 }
1191
1192 if (model_number_str && ppd->record.model_number == model_number)
1193 ppd->matches ++;
1194
1195 if (product)
1196 {
1197 for (i = 0; i < PPD_MAX_PROD; i ++)
1198 if (!ppd->record.products[i][0])
1199 break;
1200 else if (!_cups_strcasecmp(ppd->record.products[i], product))
1201 {
1202 ppd->matches += 3;
1203 break;
1204 }
1205 else if (!_cups_strncasecmp(ppd->record.products[i], product,
1206 product_len))
1207 {
1208 ppd->matches += 2;
1209 break;
1210 }
1211 }
1212
1213 if (psversion)
1214 {
1215 for (i = 0; i < PPD_MAX_VERS; i ++)
1216 if (!ppd->record.psversions[i][0])
1217 break;
1218 else if (!_cups_strcasecmp(ppd->record.psversions[i], psversion))
1219 {
1220 ppd->matches ++;
1221 break;
1222 }
1223 }
1224
1225 if (type_str && ppd->record.type == type)
1226 ppd->matches ++;
1227
1228 if (ppd->matches)
1229 {
1230 fprintf(stderr, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
1231 ppd->record.name, ppd->matches);
1232 cupsArrayAdd(matches, ppd);
1233 }
1234 }
1235 }
1236 else if (include || exclude)
1237 {
1238 matches = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
1239
1240 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1241 ppd;
1242 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
1243 {
1244 /*
1245 * Filter PPDs based on the include/exclude lists.
1246 */
1247
1248 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1249 ppd->record.type >= PPD_TYPE_DRV)
1250 continue;
1251
1252 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1253 (include && !cupsArrayFind(include, ppd->record.scheme)))
1254 continue;
1255
1256 cupsArrayAdd(matches, ppd);
1257 }
1258 }
1259 else
1260 matches = PPDsByMakeModel;
1261
1262 for (ppd = (ppd_info_t *)cupsArrayFirst(matches);
1263 count > 0 && ppd;
1264 ppd = (ppd_info_t *)cupsArrayNext(matches))
1265 {
1266 /*
1267 * Skip invalid PPDs...
1268 */
1269
1270 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1271 ppd->record.type >= PPD_TYPE_DRV)
1272 continue;
1273
1274 /*
1275 * Send this PPD...
1276 */
1277
1278 if (!sent_header)
1279 {
1280 sent_header = 1;
1281
1282 cupsdSendIPPHeader(IPP_OK, request_id);
1283 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1284 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
1285 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
1286 "en-US");
1287 }
1288
1289 fprintf(stderr, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
1290 ppd->record.name, ppd->record.make_and_model);
1291
1292 count --;
1293
1294 cupsdSendIPPGroup(IPP_TAG_PRINTER);
1295
1296 if (send_name)
1297 cupsdSendIPPString(IPP_TAG_NAME, "ppd-name", ppd->record.name);
1298
1299 if (send_natural_language)
1300 {
1301 cupsdSendIPPString(IPP_TAG_LANGUAGE, "ppd-natural-language",
1302 ppd->record.languages[0]);
1303
1304 for (i = 1; i < PPD_MAX_LANG && ppd->record.languages[i][0]; i ++)
1305 cupsdSendIPPString(IPP_TAG_LANGUAGE, "", ppd->record.languages[i]);
1306 }
1307
1308 if (send_make)
1309 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make", ppd->record.make);
1310
1311 if (send_make_and_model)
1312 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make-and-model",
1313 ppd->record.make_and_model);
1314
1315 if (send_device_id)
1316 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-device-id",
1317 ppd->record.device_id);
1318
1319 if (send_product)
1320 {
1321 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-product",
1322 ppd->record.products[0]);
1323
1324 for (i = 1; i < PPD_MAX_PROD && ppd->record.products[i][0]; i ++)
1325 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.products[i]);
1326 }
1327
1328 if (send_psversion)
1329 {
1330 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-psversion",
1331 ppd->record.psversions[0]);
1332
1333 for (i = 1; i < PPD_MAX_VERS && ppd->record.psversions[i][0]; i ++)
1334 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.psversions[i]);
1335 }
1336
1337 if (send_type)
1338 cupsdSendIPPString(IPP_TAG_KEYWORD, "ppd-type",
1339 ppd_types[ppd->record.type]);
1340
1341 if (send_model_number)
1342 cupsdSendIPPInteger(IPP_TAG_INTEGER, "ppd-model-number",
1343 ppd->record.model_number);
1344
1345 /*
1346 * If we have only requested the ppd-make attribute, then skip
1347 * the remaining PPDs with this make...
1348 */
1349
1350 if (cupsArrayFind(requested, (void *)"ppd-make") &&
1351 cupsArrayCount(requested) == 1)
1352 {
1353 const char *this_make; /* This ppd-make */
1354
1355
1356 for (this_make = ppd->record.make,
1357 ppd = (ppd_info_t *)cupsArrayNext(matches);
1358 ppd;
1359 ppd = (ppd_info_t *)cupsArrayNext(matches))
1360 if (_cups_strcasecmp(this_make, ppd->record.make))
1361 break;
1362
1363 cupsArrayPrev(matches);
1364 }
1365 }
1366
1367 if (!sent_header)
1368 {
1369 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
1370 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1371 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
1372 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
1373 }
1374
1375 cupsdSendIPPTrailer();
1376
1377 return (0);
1378 }
1379
1380
1381 /*
1382 * 'load_drv()' - Load the PPDs from a driver information file.
1383 */
1384
1385 static int /* O - 1 on success, 0 on failure */
1386 load_drv(const char *filename, /* I - Actual filename */
1387 const char *name, /* I - Name to the rest of the world */
1388 cups_file_t *fp, /* I - File to read from */
1389 time_t mtime, /* I - Mod time of driver info file */
1390 off_t size) /* I - Size of driver info file */
1391 {
1392 ppdcSource *src; // Driver information file
1393 ppdcDriver *d; // Current driver
1394 ppdcAttr *device_id, // 1284DeviceID attribute
1395 *product, // Current product value
1396 *ps_version, // PSVersion attribute
1397 *cups_fax, // cupsFax attribute
1398 *nick_name; // NickName attribute
1399 ppdcFilter *filter; // Current filter
1400 ppd_info_t *ppd; // Current PPD
1401 int products_found; // Number of products found
1402 char uri[1024], // Driver URI
1403 make_model[1024]; // Make and model
1404 int type; // Driver type
1405
1406
1407 /*
1408 * Load the driver info file...
1409 */
1410
1411 src = new ppdcSource(filename, fp);
1412
1413 if (src->drivers->count == 0)
1414 {
1415 fprintf(stderr,
1416 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1417 filename);
1418 src->release();
1419 return (0);
1420 }
1421
1422 /*
1423 * Add a dummy entry for the file...
1424 */
1425
1426 add_ppd(name, name, "", "", "", "", "", "", mtime, size, 0,
1427 PPD_TYPE_DRV, "drv");
1428 ChangedPPD = 1;
1429
1430 /*
1431 * Then the drivers in the file...
1432 */
1433
1434 for (d = (ppdcDriver *)src->drivers->first();
1435 d;
1436 d = (ppdcDriver *)src->drivers->next())
1437 {
1438 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0,
1439 "/%s/%s", name,
1440 d->file_name ? d->file_name->value :
1441 d->pc_file_name->value);
1442
1443 device_id = d->find_attr("1284DeviceID", NULL);
1444 ps_version = d->find_attr("PSVersion", NULL);
1445 nick_name = d->find_attr("NickName", NULL);
1446
1447 if (nick_name)
1448 strlcpy(make_model, nick_name->value->value, sizeof(make_model));
1449 else if (_cups_strncasecmp(d->model_name->value, d->manufacturer->value,
1450 strlen(d->manufacturer->value)))
1451 snprintf(make_model, sizeof(make_model), "%s %s, %s",
1452 d->manufacturer->value, d->model_name->value,
1453 d->version->value);
1454 else
1455 snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value,
1456 d->version->value);
1457
1458 if ((cups_fax = d->find_attr("cupsFax", NULL)) != NULL &&
1459 !_cups_strcasecmp(cups_fax->value->value, "true"))
1460 type = PPD_TYPE_FAX;
1461 else if (d->type == PPDC_DRIVER_PS)
1462 type = PPD_TYPE_POSTSCRIPT;
1463 else if (d->type != PPDC_DRIVER_CUSTOM)
1464 type = PPD_TYPE_RASTER;
1465 else
1466 {
1467 for (filter = (ppdcFilter *)d->filters->first(),
1468 type = PPD_TYPE_POSTSCRIPT;
1469 filter;
1470 filter = (ppdcFilter *)d->filters->next())
1471 if (_cups_strcasecmp(filter->mime_type->value, "application/vnd.cups-raster"))
1472 type = PPD_TYPE_RASTER;
1473 else if (_cups_strcasecmp(filter->mime_type->value,
1474 "application/vnd.cups-pdf"))
1475 type = PPD_TYPE_PDF;
1476 }
1477
1478 for (product = (ppdcAttr *)d->attrs->first(), products_found = 0,
1479 ppd = NULL;
1480 product;
1481 product = (ppdcAttr *)d->attrs->next())
1482 if (!strcmp(product->name->value, "Product"))
1483 {
1484 if (!products_found)
1485 ppd = add_ppd(name, uri, "en", d->manufacturer->value, make_model,
1486 device_id ? device_id->value->value : "",
1487 product->value->value,
1488 ps_version ? ps_version->value->value : "(3010) 0",
1489 mtime, size, d->model_number, type, "drv");
1490 else if (products_found < PPD_MAX_PROD)
1491 strlcpy(ppd->record.products[products_found], product->value->value,
1492 sizeof(ppd->record.products[0]));
1493 else
1494 break;
1495
1496 products_found ++;
1497 }
1498
1499 if (!products_found)
1500 add_ppd(name, uri, "en", d->manufacturer->value, make_model,
1501 device_id ? device_id->value->value : "",
1502 d->model_name->value,
1503 ps_version ? ps_version->value->value : "(3010) 0",
1504 mtime, size, d->model_number, type, "drv");
1505 }
1506
1507 src->release();
1508
1509 return (1);
1510 }
1511
1512
1513 /*
1514 * 'load_drivers()' - Load driver-generated PPD files.
1515 */
1516
1517 static int /* O - 1 on success, 0 on failure */
1518 load_drivers(cups_array_t *include, /* I - Drivers to include */
1519 cups_array_t *exclude) /* I - Drivers to exclude */
1520 {
1521 int i; /* Looping var */
1522 char *start, /* Start of value */
1523 *ptr; /* Pointer into string */
1524 const char *server_bin, /* CUPS_SERVERBIN env variable */
1525 *scheme, /* Scheme for this driver */
1526 *scheme_end; /* Pointer to end of scheme */
1527 char drivers[1024]; /* Location of driver programs */
1528 int pid; /* Process ID for driver program */
1529 cups_file_t *fp; /* Pipe to driver program */
1530 cups_dir_t *dir; /* Directory pointer */
1531 cups_dentry_t *dent; /* Directory entry */
1532 char *argv[3], /* Arguments for command */
1533 filename[1024], /* Name of driver */
1534 line[2048], /* Line from driver */
1535 name[512], /* ppd-name */
1536 make[128], /* ppd-make */
1537 make_and_model[128], /* ppd-make-and-model */
1538 device_id[256], /* ppd-device-id */
1539 languages[128], /* ppd-natural-language */
1540 product[128], /* ppd-product */
1541 psversion[128], /* ppd-psversion */
1542 type_str[128]; /* ppd-type */
1543 int type; /* PPD type */
1544 ppd_info_t *ppd; /* Newly added PPD */
1545
1546
1547 /*
1548 * Try opening the driver directory...
1549 */
1550
1551 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
1552 server_bin = CUPS_SERVERBIN;
1553
1554 snprintf(drivers, sizeof(drivers), "%s/driver", server_bin);
1555
1556 if ((dir = cupsDirOpen(drivers)) == NULL)
1557 {
1558 fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory "
1559 "\"%s\": %s\n",
1560 drivers, strerror(errno));
1561 return (0);
1562 }
1563
1564 /*
1565 * Loop through all of the device drivers...
1566 */
1567
1568 argv[1] = (char *)"list";
1569 argv[2] = NULL;
1570
1571 while ((dent = cupsDirRead(dir)) != NULL)
1572 {
1573 /*
1574 * Only look at executable files...
1575 */
1576
1577 if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode))
1578 continue;
1579
1580 /*
1581 * Include/exclude specific drivers...
1582 */
1583
1584 if (exclude)
1585 {
1586 /*
1587 * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
1588 */
1589
1590 for (scheme = (char *)cupsArrayFirst(exclude);
1591 scheme;
1592 scheme = (char *)cupsArrayNext(exclude))
1593 {
1594 fprintf(stderr, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
1595 dent->filename, scheme);
1596 scheme_end = scheme + strlen(scheme) - 1;
1597
1598 if ((scheme_end > scheme && *scheme_end == '*' &&
1599 !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
1600 !strcmp(scheme, dent->filename))
1601 {
1602 fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr);
1603 break;
1604 }
1605 }
1606
1607 if (scheme)
1608 continue;
1609 }
1610
1611 if (include)
1612 {
1613 /*
1614 * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
1615 */
1616
1617 for (scheme = (char *)cupsArrayFirst(include);
1618 scheme;
1619 scheme = (char *)cupsArrayNext(include))
1620 {
1621 fprintf(stderr, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
1622 dent->filename, scheme);
1623 scheme_end = scheme + strlen(scheme) - 1;
1624
1625 if ((scheme_end > scheme && *scheme_end == '*' &&
1626 !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
1627 !strcmp(scheme, dent->filename))
1628 {
1629 fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr);
1630 break;
1631 }
1632 }
1633
1634 if (!scheme)
1635 continue;
1636 }
1637 else
1638 scheme = dent->filename;
1639
1640 /*
1641 * Run the driver with no arguments and collect the output...
1642 */
1643
1644 snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename);
1645
1646 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_PROGRAM, !geteuid(),
1647 _cupsFileCheckFilter, NULL))
1648 continue;
1649
1650 argv[0] = dent->filename;
1651
1652 if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL)
1653 {
1654 while (cupsFileGets(fp, line, sizeof(line)))
1655 {
1656 /*
1657 * Each line is of the form:
1658 *
1659 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
1660 * "ppd-device-id" "ppd-product" "ppd-psversion"
1661 */
1662
1663 device_id[0] = '\0';
1664 product[0] = '\0';
1665 psversion[0] = '\0';
1666 strcpy(type_str, "postscript");
1667
1668 if (sscanf(line, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
1669 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%255[^\"]\""
1670 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1671 "%*[ \t]\"%127[^\"]\"",
1672 name, languages, make, make_and_model,
1673 device_id, product, psversion, type_str) < 4)
1674 {
1675 /*
1676 * Bad format; strip trailing newline and write an error message.
1677 */
1678
1679 if (line[strlen(line) - 1] == '\n')
1680 line[strlen(line) - 1] = '\0';
1681
1682 fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1683 dent->filename, line);
1684 break;
1685 }
1686 else
1687 {
1688 /*
1689 * Add the device to the array of available devices...
1690 */
1691
1692 if ((start = strchr(languages, ',')) != NULL)
1693 *start++ = '\0';
1694
1695 for (type = 0;
1696 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
1697 type ++)
1698 if (!strcmp(type_str, ppd_types[type]))
1699 break;
1700
1701 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
1702 {
1703 fprintf(stderr,
1704 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
1705 type_str);
1706 type = PPD_TYPE_UNKNOWN;
1707 }
1708
1709 ppd = add_ppd(filename, name, languages, make, make_and_model,
1710 device_id, product, psversion, 0, 0, 0, type, scheme);
1711
1712 if (!ppd)
1713 {
1714 cupsDirClose(dir);
1715 cupsFileClose(fp);
1716 return (0);
1717 }
1718
1719 if (start && *start)
1720 {
1721 for (i = 1; i < PPD_MAX_LANG && *start; i ++)
1722 {
1723 if ((ptr = strchr(start, ',')) != NULL)
1724 *ptr++ = '\0';
1725 else
1726 ptr = start + strlen(start);
1727
1728 strlcpy(ppd->record.languages[i], start,
1729 sizeof(ppd->record.languages[0]));
1730
1731 start = ptr;
1732 }
1733 }
1734
1735 fprintf(stderr, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1736 name);
1737 }
1738 }
1739
1740 cupsFileClose(fp);
1741 }
1742 else
1743 fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1744 filename, strerror(errno));
1745 }
1746
1747 cupsDirClose(dir);
1748
1749 return (1);
1750 }
1751
1752
1753 /*
1754 * 'load_ppds()' - Load PPD files recursively.
1755 */
1756
1757 static int /* O - 1 on success, 0 on failure */
1758 load_ppds(const char *d, /* I - Actual directory */
1759 const char *p, /* I - Virtual path in name */
1760 int descend) /* I - Descend into directories? */
1761 {
1762 struct stat dinfo, /* Directory information */
1763 *dinfoptr; /* Pointer to match */
1764 int i; /* Looping var */
1765 cups_file_t *fp; /* Pointer to file */
1766 cups_dir_t *dir; /* Directory pointer */
1767 cups_dentry_t *dent; /* Directory entry */
1768 char filename[1024], /* Name of PPD or directory */
1769 line[256], /* Line from backend */
1770 *ptr, /* Pointer into name */
1771 name[128], /* Name of PPD file */
1772 lang_version[64], /* PPD LanguageVersion */
1773 lang_encoding[64], /* PPD LanguageEncoding */
1774 country[64], /* Country code */
1775 manufacturer[256], /* Manufacturer */
1776 make_model[256], /* Make and Model */
1777 model_name[256], /* ModelName */
1778 nick_name[256], /* NickName */
1779 device_id[256], /* 1284DeviceID */
1780 product[256], /* Product */
1781 psversion[256], /* PSVersion */
1782 temp[512]; /* Temporary make and model */
1783 int install_group, /* In the installable options group? */
1784 model_number, /* cupsModelNumber */
1785 type; /* ppd-type */
1786 cups_array_t *products, /* Product array */
1787 *psversions, /* PSVersion array */
1788 *cups_languages; /* cupsLanguages array */
1789 ppd_info_t *ppd, /* New PPD file */
1790 key; /* Search key */
1791 int new_ppd; /* Is this a new PPD? */
1792 struct /* LanguageVersion translation table */
1793 {
1794 const char *version, /* LanguageVersion string */
1795 *language; /* Language code */
1796 } languages[] =
1797 {
1798 { "chinese", "zh" },
1799 { "czech", "cs" },
1800 { "danish", "da" },
1801 { "dutch", "nl" },
1802 { "english", "en" },
1803 { "finnish", "fi" },
1804 { "french", "fr" },
1805 { "german", "de" },
1806 { "greek", "el" },
1807 { "hungarian", "hu" },
1808 { "italian", "it" },
1809 { "japanese", "ja" },
1810 { "korean", "ko" },
1811 { "norwegian", "no" },
1812 { "polish", "pl" },
1813 { "portuguese", "pt" },
1814 { "russian", "ru" },
1815 { "simplified chinese", "zh_CN" },
1816 { "slovak", "sk" },
1817 { "spanish", "es" },
1818 { "swedish", "sv" },
1819 { "traditional chinese", "zh_TW" },
1820 { "turkish", "tr" }
1821 };
1822
1823
1824 /*
1825 * See if we've loaded this directory before...
1826 */
1827
1828 if (stat(d, &dinfo))
1829 {
1830 if (errno != ENOENT)
1831 fprintf(stderr, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d,
1832 strerror(errno));
1833
1834 return (0);
1835 }
1836 else if (cupsArrayFind(Inodes, &dinfo))
1837 {
1838 fprintf(stderr, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
1839 d);
1840 return (0);
1841 }
1842
1843 /*
1844 * Nope, add it to the Inodes array and continue...
1845 */
1846
1847 dinfoptr = (struct stat *)malloc(sizeof(struct stat));
1848 memcpy(dinfoptr, &dinfo, sizeof(struct stat));
1849 cupsArrayAdd(Inodes, dinfoptr);
1850
1851 /*
1852 * Check permissions...
1853 */
1854
1855 if (_cupsFileCheck(d, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
1856 _cupsFileCheckFilter, NULL))
1857 return (0);
1858
1859 if ((dir = cupsDirOpen(d)) == NULL)
1860 {
1861 if (errno != ENOENT)
1862 fprintf(stderr,
1863 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1864 d, strerror(errno));
1865
1866 return (0);
1867 }
1868
1869 fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d);
1870
1871 while ((dent = cupsDirRead(dir)) != NULL)
1872 {
1873 /*
1874 * Skip files/directories starting with "."...
1875 */
1876
1877 if (dent->filename[0] == '.')
1878 continue;
1879
1880 /*
1881 * See if this is a file...
1882 */
1883
1884 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
1885
1886 if (p[0])
1887 snprintf(name, sizeof(name), "%s/%s", p, dent->filename);
1888 else
1889 strlcpy(name, dent->filename, sizeof(name));
1890
1891 if (S_ISDIR(dent->fileinfo.st_mode))
1892 {
1893 /*
1894 * Do subdirectory...
1895 */
1896
1897 if (descend)
1898 {
1899 if (!load_ppds(filename, name, 1))
1900 {
1901 cupsDirClose(dir);
1902 return (1);
1903 }
1904 }
1905 else if ((ptr = filename + strlen(filename) - 14) > filename &&
1906 !strcmp(ptr, ".printerDriver"))
1907 {
1908 /*
1909 * Load PPDs in a printer driver bundle.
1910 */
1911
1912 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
1913 _cupsFileCheckFilter, NULL))
1914 continue;
1915
1916 strlcat(filename, "/Contents/Resources/PPDs", sizeof(filename));
1917 strlcat(name, "/Contents/Resources/PPDs", sizeof(name));
1918
1919 load_ppds(filename, name, 0);
1920 }
1921
1922 continue;
1923 }
1924 else if (strstr(filename, ".plist"))
1925 {
1926 /*
1927 * Skip plist files in the PPDs directory...
1928 */
1929
1930 continue;
1931 }
1932 else if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_FILE_ONLY, !geteuid(),
1933 _cupsFileCheckFilter, NULL))
1934 continue;
1935
1936 /*
1937 * See if this file has been scanned before...
1938 */
1939
1940 strcpy(key.record.filename, name);
1941 strcpy(key.record.name, name);
1942
1943 ppd = (ppd_info_t *)cupsArrayFind(PPDsByName, &key);
1944
1945 if (ppd &&
1946 ppd->record.size == dent->fileinfo.st_size &&
1947 ppd->record.mtime == dent->fileinfo.st_mtime)
1948 {
1949 /*
1950 * Rewind to the first entry for this file...
1951 */
1952
1953 while ((ppd = (ppd_info_t *)cupsArrayPrev(PPDsByName)) != NULL &&
1954 !strcmp(ppd->record.filename, name));
1955
1956 /*
1957 * Then mark all of the matches for this file as found...
1958 */
1959
1960 while ((ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) != NULL &&
1961 !strcmp(ppd->record.filename, name))
1962 ppd->found = 1;
1963
1964 continue;
1965 }
1966
1967 /*
1968 * No, file is new/changed, so re-scan it...
1969 */
1970
1971 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1972 continue;
1973
1974 /*
1975 * Now see if this is a PPD file...
1976 */
1977
1978 line[0] = '\0';
1979 cupsFileGets(fp, line, sizeof(line));
1980
1981 if (strncmp(line, "*PPD-Adobe:", 11))
1982 {
1983 /*
1984 * Nope, treat it as a driver information file...
1985 */
1986
1987 load_drv(filename, name, fp, dent->fileinfo.st_mtime,
1988 dent->fileinfo.st_size);
1989 continue;
1990 }
1991
1992 /*
1993 * Now read until we get the NickName field...
1994 */
1995
1996 cups_languages = cupsArrayNew(NULL, NULL);
1997 products = cupsArrayNew(NULL, NULL);
1998 psversions = cupsArrayNew(NULL, NULL);
1999
2000 model_name[0] = '\0';
2001 nick_name[0] = '\0';
2002 manufacturer[0] = '\0';
2003 device_id[0] = '\0';
2004 lang_encoding[0] = '\0';
2005 strcpy(lang_version, "en");
2006 model_number = 0;
2007 install_group = 0;
2008 type = PPD_TYPE_POSTSCRIPT;
2009
2010 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
2011 {
2012 if (!strncmp(line, "*Manufacturer:", 14))
2013 sscanf(line, "%*[^\"]\"%255[^\"]", manufacturer);
2014 else if (!strncmp(line, "*ModelName:", 11))
2015 sscanf(line, "%*[^\"]\"%127[^\"]", model_name);
2016 else if (!strncmp(line, "*LanguageEncoding:", 18))
2017 sscanf(line, "%*[^:]:%63s", lang_encoding);
2018 else if (!strncmp(line, "*LanguageVersion:", 17))
2019 sscanf(line, "%*[^:]:%63s", lang_version);
2020 else if (!strncmp(line, "*NickName:", 10))
2021 sscanf(line, "%*[^\"]\"%255[^\"]", nick_name);
2022 else if (!_cups_strncasecmp(line, "*1284DeviceID:", 14))
2023 {
2024 sscanf(line, "%*[^\"]\"%255[^\"]", device_id);
2025
2026 // Make sure device ID ends with a semicolon...
2027 if (device_id[0] && device_id[strlen(device_id) - 1] != ';')
2028 strlcat(device_id, ";", sizeof(device_id));
2029 }
2030 else if (!strncmp(line, "*Product:", 9))
2031 {
2032 if (sscanf(line, "%*[^\"]\"(%255[^\"]", product) == 1)
2033 {
2034 /*
2035 * Make sure the value ends with a right parenthesis - can't stop at
2036 * the first right paren since the product name may contain escaped
2037 * parenthesis...
2038 */
2039
2040 ptr = product + strlen(product) - 1;
2041 if (ptr > product && *ptr == ')')
2042 {
2043 /*
2044 * Yes, ends with a parenthesis, so remove it from the end and
2045 * add the product to the list...
2046 */
2047
2048 *ptr = '\0';
2049 cupsArrayAdd(products, strdup(product));
2050 }
2051 }
2052 }
2053 else if (!strncmp(line, "*PSVersion:", 11))
2054 {
2055 sscanf(line, "%*[^\"]\"%255[^\"]", psversion);
2056 cupsArrayAdd(psversions, strdup(psversion));
2057 }
2058 else if (!strncmp(line, "*cupsLanguages:", 15))
2059 {
2060 char *start; /* Start of language */
2061
2062
2063 for (start = line + 15; *start && isspace(*start & 255); start ++);
2064
2065 if (*start++ == '\"')
2066 {
2067 while (*start)
2068 {
2069 for (ptr = start + 1;
2070 *ptr && *ptr != '\"' && !isspace(*ptr & 255);
2071 ptr ++);
2072
2073 if (*ptr)
2074 {
2075 *ptr++ = '\0';
2076
2077 while (isspace(*ptr & 255))
2078 *ptr++ = '\0';
2079 }
2080
2081 cupsArrayAdd(cups_languages, strdup(start));
2082 start = ptr;
2083 }
2084 }
2085 }
2086 else if (!strncmp(line, "*cupsFax:", 9))
2087 {
2088 for (ptr = line + 9; isspace(*ptr & 255); ptr ++);
2089
2090 if (!_cups_strncasecmp(ptr, "true", 4))
2091 type = PPD_TYPE_FAX;
2092 }
2093 else if (!strncmp(line, "*cupsFilter:", 12) && type == PPD_TYPE_POSTSCRIPT)
2094 {
2095 if (strstr(line + 12, "application/vnd.cups-raster"))
2096 type = PPD_TYPE_RASTER;
2097 else if (strstr(line + 12, "application/vnd.cups-pdf"))
2098 type = PPD_TYPE_PDF;
2099 }
2100 else if (!strncmp(line, "*cupsModelNumber:", 17))
2101 sscanf(line, "*cupsModelNumber:%d", &model_number);
2102 else if (!strncmp(line, "*OpenGroup: Installable", 23))
2103 install_group = 1;
2104 else if (!strncmp(line, "*CloseGroup:", 12))
2105 install_group = 0;
2106 else if (!strncmp(line, "*OpenUI", 7))
2107 {
2108 /*
2109 * Stop early if we have a NickName or ModelName attributes
2110 * before the first non-installable OpenUI...
2111 */
2112
2113 if (!install_group && (model_name[0] || nick_name[0]) &&
2114 cupsArrayCount(products) > 0 && cupsArrayCount(psversions) > 0)
2115 break;
2116 }
2117 }
2118
2119 /*
2120 * Close the file...
2121 */
2122
2123 cupsFileClose(fp);
2124
2125 /*
2126 * See if we got all of the required info...
2127 */
2128
2129 if (nick_name[0])
2130 cupsCharsetToUTF8((cups_utf8_t *)make_model, nick_name,
2131 sizeof(make_model), _ppdGetEncoding(lang_encoding));
2132 else
2133 strcpy(make_model, model_name);
2134
2135 while (isspace(make_model[0] & 255))
2136 _cups_strcpy(make_model, make_model + 1);
2137
2138 if (!make_model[0] || cupsArrayCount(products) == 0 ||
2139 cupsArrayCount(psversions) == 0)
2140 {
2141 /*
2142 * We don't have all the info needed, so skip this file...
2143 */
2144
2145 if (!make_model[0])
2146 fprintf(stderr, "WARNING: Missing NickName and ModelName in %s!\n",
2147 filename);
2148
2149 if (cupsArrayCount(products) == 0)
2150 fprintf(stderr, "WARNING: Missing Product in %s!\n", filename);
2151
2152 if (cupsArrayCount(psversions) == 0)
2153 fprintf(stderr, "WARNING: Missing PSVersion in %s!\n", filename);
2154
2155 free_array(products);
2156 free_array(psversions);
2157 free_array(cups_languages);
2158
2159 continue;
2160 }
2161
2162 if (model_name[0])
2163 cupsArrayAdd(products, strdup(model_name));
2164
2165 /*
2166 * Normalize the make and model string...
2167 */
2168
2169 while (isspace(manufacturer[0] & 255))
2170 _cups_strcpy(manufacturer, manufacturer + 1);
2171
2172 if (!_cups_strncasecmp(make_model, manufacturer, strlen(manufacturer)))
2173 strlcpy(temp, make_model, sizeof(temp));
2174 else
2175 snprintf(temp, sizeof(temp), "%s %s", manufacturer, make_model);
2176
2177 _ppdNormalizeMakeAndModel(temp, make_model, sizeof(make_model));
2178
2179 /*
2180 * See if we got a manufacturer...
2181 */
2182
2183 if (!manufacturer[0] || !strcmp(manufacturer, "ESP"))
2184 {
2185 /*
2186 * Nope, copy the first part of the make and model then...
2187 */
2188
2189 strlcpy(manufacturer, make_model, sizeof(manufacturer));
2190
2191 /*
2192 * Truncate at the first space, dash, or slash, or make the
2193 * manufacturer "Other"...
2194 */
2195
2196 for (ptr = manufacturer; *ptr; ptr ++)
2197 if (*ptr == ' ' || *ptr == '-' || *ptr == '/')
2198 break;
2199
2200 if (*ptr && ptr > manufacturer)
2201 *ptr = '\0';
2202 else
2203 strcpy(manufacturer, "Other");
2204 }
2205 else if (!_cups_strncasecmp(manufacturer, "LHAG", 4) ||
2206 !_cups_strncasecmp(manufacturer, "linotype", 8))
2207 strcpy(manufacturer, "LHAG");
2208 else if (!_cups_strncasecmp(manufacturer, "Hewlett", 7))
2209 strcpy(manufacturer, "HP");
2210
2211 /*
2212 * Fix the lang_version as needed...
2213 */
2214
2215 if ((ptr = strchr(lang_version, '-')) != NULL)
2216 *ptr++ = '\0';
2217 else if ((ptr = strchr(lang_version, '_')) != NULL)
2218 *ptr++ = '\0';
2219
2220 if (ptr)
2221 {
2222 /*
2223 * Setup the country suffix...
2224 */
2225
2226 country[0] = '_';
2227 _cups_strcpy(country + 1, ptr);
2228 }
2229 else
2230 {
2231 /*
2232 * No country suffix...
2233 */
2234
2235 country[0] = '\0';
2236 }
2237
2238 for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++)
2239 if (!_cups_strcasecmp(languages[i].version, lang_version))
2240 break;
2241
2242 if (i < (int)(sizeof(languages) / sizeof(languages[0])))
2243 {
2244 /*
2245 * Found a known language...
2246 */
2247
2248 snprintf(lang_version, sizeof(lang_version), "%s%s",
2249 languages[i].language, country);
2250 }
2251 else
2252 {
2253 /*
2254 * Unknown language; use "xx"...
2255 */
2256
2257 strcpy(lang_version, "xx");
2258 }
2259
2260 /*
2261 * Record the PPD file...
2262 */
2263
2264 new_ppd = !ppd;
2265
2266 if (new_ppd)
2267 {
2268 /*
2269 * Add new PPD file...
2270 */
2271
2272 fprintf(stderr, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name);
2273
2274 ppd = add_ppd(name, name, lang_version, manufacturer, make_model,
2275 device_id, (char *)cupsArrayFirst(products),
2276 (char *)cupsArrayFirst(psversions),
2277 dent->fileinfo.st_mtime, dent->fileinfo.st_size,
2278 model_number, type, "file");
2279
2280 if (!ppd)
2281 {
2282 cupsDirClose(dir);
2283 return (0);
2284 }
2285 }
2286 else
2287 {
2288 /*
2289 * Update existing record...
2290 */
2291
2292 fprintf(stderr, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name);
2293
2294 memset(ppd, 0, sizeof(ppd_info_t));
2295
2296 ppd->found = 1;
2297 ppd->record.mtime = dent->fileinfo.st_mtime;
2298 ppd->record.size = dent->fileinfo.st_size;
2299 ppd->record.model_number = model_number;
2300 ppd->record.type = type;
2301
2302 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
2303 strlcpy(ppd->record.make, manufacturer, sizeof(ppd->record.make));
2304 strlcpy(ppd->record.make_and_model, make_model,
2305 sizeof(ppd->record.make_and_model));
2306 strlcpy(ppd->record.languages[0], lang_version,
2307 sizeof(ppd->record.languages[0]));
2308 strlcpy(ppd->record.products[0], (char *)cupsArrayFirst(products),
2309 sizeof(ppd->record.products[0]));
2310 strlcpy(ppd->record.psversions[0], (char *)cupsArrayFirst(psversions),
2311 sizeof(ppd->record.psversions[0]));
2312 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
2313 }
2314
2315 /*
2316 * Add remaining products, versions, and languages...
2317 */
2318
2319 for (i = 1;
2320 i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL;
2321 i ++)
2322 strlcpy(ppd->record.products[i], ptr,
2323 sizeof(ppd->record.products[0]));
2324
2325 for (i = 1;
2326 i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL;
2327 i ++)
2328 strlcpy(ppd->record.psversions[i], ptr,
2329 sizeof(ppd->record.psversions[0]));
2330
2331 for (i = 1, ptr = (char *)cupsArrayFirst(cups_languages);
2332 i < PPD_MAX_LANG && ptr;
2333 i ++, ptr = (char *)cupsArrayNext(cups_languages))
2334 strlcpy(ppd->record.languages[i], ptr,
2335 sizeof(ppd->record.languages[0]));
2336
2337 /*
2338 * Free products, versions, and languages...
2339 */
2340
2341 free_array(cups_languages);
2342 free_array(products);
2343 free_array(psversions);
2344
2345 ChangedPPD = 1;
2346 }
2347
2348 cupsDirClose(dir);
2349
2350 return (1);
2351 }
2352
2353
2354 /*
2355 * 'load_ppds_dat()' - Load the ppds.dat file.
2356 */
2357
2358 static void
2359 load_ppds_dat(char *filename, /* I - Filename buffer */
2360 size_t filesize, /* I - Size of filename buffer */
2361 int verbose) /* I - Be verbose? */
2362 {
2363 ppd_info_t *ppd; /* Current PPD file */
2364 cups_file_t *fp; /* ppds.dat file */
2365 struct stat fileinfo; /* ppds.dat information */
2366 const char *cups_cachedir; /* CUPS_CACHEDIR environment variable */
2367
2368
2369 PPDsByName = cupsArrayNew((cups_array_func_t)compare_names, NULL);
2370 PPDsByMakeModel = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
2371 ChangedPPD = 0;
2372
2373 if (!filename[0])
2374 {
2375 if ((cups_cachedir = getenv("CUPS_CACHEDIR")) == NULL)
2376 cups_cachedir = CUPS_CACHEDIR;
2377
2378 snprintf(filename, filesize, "%s/ppds.dat", cups_cachedir);
2379 }
2380
2381 if ((fp = cupsFileOpen(filename, "r")) != NULL)
2382 {
2383 /*
2384 * See if we have the right sync word...
2385 */
2386
2387 unsigned ppdsync; /* Sync word */
2388 int num_ppds; /* Number of PPDs */
2389
2390 if (cupsFileRead(fp, (char *)&ppdsync, sizeof(ppdsync))
2391 == sizeof(ppdsync) &&
2392 ppdsync == PPD_SYNC &&
2393 !stat(filename, &fileinfo) &&
2394 ((fileinfo.st_size - sizeof(ppdsync)) % sizeof(ppd_rec_t)) == 0 &&
2395 (num_ppds = (fileinfo.st_size - sizeof(ppdsync)) /
2396 sizeof(ppd_rec_t)) > 0)
2397 {
2398 /*
2399 * We have a ppds.dat file, so read it!
2400 */
2401
2402 for (; num_ppds > 0; num_ppds --)
2403 {
2404 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
2405 {
2406 if (verbose)
2407 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
2408 stderr);
2409 exit(1);
2410 }
2411
2412 if (cupsFileRead(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)) > 0)
2413 {
2414 cupsArrayAdd(PPDsByName, ppd);
2415 cupsArrayAdd(PPDsByMakeModel, ppd);
2416 }
2417 else
2418 {
2419 free(ppd);
2420 break;
2421 }
2422 }
2423
2424 if (verbose)
2425 fprintf(stderr, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
2426 filename, cupsArrayCount(PPDsByName));
2427 }
2428
2429 cupsFileClose(fp);
2430 }
2431 }
2432
2433
2434 /*
2435 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2436 * ID.
2437 */
2438
2439 static regex_t * /* O - Regular expression */
2440 regex_device_id(const char *device_id) /* I - IEEE-1284 device ID */
2441 {
2442 char res[2048], /* Regular expression string */
2443 *ptr; /* Pointer into string */
2444 regex_t *re; /* Regular expression */
2445 int cmd; /* Command set string? */
2446
2447
2448 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id);
2449
2450 /*
2451 * Scan the device ID string and insert class, command set, manufacturer, and
2452 * model attributes to match. We assume that the device ID in the PPD and the
2453 * device ID reported by the device itself use the same attribute names and
2454 * order of attributes.
2455 */
2456
2457 ptr = res;
2458
2459 while (*device_id && ptr < (res + sizeof(res) - 6))
2460 {
2461 cmd = !_cups_strncasecmp(device_id, "COMMAND SET:", 12) ||
2462 !_cups_strncasecmp(device_id, "CMD:", 4);
2463
2464 if (cmd || !_cups_strncasecmp(device_id, "MANUFACTURER:", 13) ||
2465 !_cups_strncasecmp(device_id, "MFG:", 4) ||
2466 !_cups_strncasecmp(device_id, "MFR:", 4) ||
2467 !_cups_strncasecmp(device_id, "MODEL:", 6) ||
2468 !_cups_strncasecmp(device_id, "MDL:", 4))
2469 {
2470 if (ptr > res)
2471 {
2472 *ptr++ = '.';
2473 *ptr++ = '*';
2474 }
2475
2476 *ptr++ = '(';
2477
2478 while (*device_id && *device_id != ';' && ptr < (res + sizeof(res) - 8))
2479 {
2480 if (strchr("[]{}().*\\|", *device_id))
2481 *ptr++ = '\\';
2482 if (*device_id == ':')
2483 {
2484 /*
2485 * KEY:.*value
2486 */
2487
2488 *ptr++ = *device_id++;
2489 *ptr++ = '.';
2490 *ptr++ = '*';
2491 }
2492 else
2493 *ptr++ = *device_id++;
2494 }
2495
2496 if (*device_id == ';' || !*device_id)
2497 {
2498 /*
2499 * KEY:.*value.*;
2500 */
2501
2502 *ptr++ = '.';
2503 *ptr++ = '*';
2504 *ptr++ = ';';
2505 }
2506 *ptr++ = ')';
2507 if (cmd)
2508 *ptr++ = '?';
2509 }
2510 else if ((device_id = strchr(device_id, ';')) == NULL)
2511 break;
2512 else
2513 device_id ++;
2514 }
2515
2516 *ptr = '\0';
2517
2518 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res);
2519
2520 /*
2521 * Compile the regular expression and return...
2522 */
2523
2524 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2525 {
2526 if (!regcomp(re, res, REG_EXTENDED | REG_ICASE))
2527 {
2528 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr);
2529 return (re);
2530 }
2531
2532 free(re);
2533 }
2534
2535 return (NULL);
2536 }
2537
2538
2539 /*
2540 * 'regex_string()' - Construct a regular expression to compare a simple string.
2541 */
2542
2543 static regex_t * /* O - Regular expression */
2544 regex_string(const char *s) /* I - String to compare */
2545 {
2546 char res[2048], /* Regular expression string */
2547 *ptr; /* Pointer into string */
2548 regex_t *re; /* Regular expression */
2549
2550
2551 fprintf(stderr, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s);
2552
2553 /*
2554 * Convert the string to a regular expression, escaping special characters
2555 * as needed.
2556 */
2557
2558 ptr = res;
2559
2560 while (*s && ptr < (res + sizeof(res) - 2))
2561 {
2562 if (strchr("[]{}().*\\", *s))
2563 *ptr++ = '\\';
2564
2565 *ptr++ = *s++;
2566 }
2567
2568 *ptr = '\0';
2569
2570 fprintf(stderr, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res);
2571
2572 /*
2573 * Create a case-insensitive regular expression...
2574 */
2575
2576 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2577 {
2578 if (!regcomp(re, res, REG_ICASE))
2579 {
2580 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr);
2581 return (re);
2582 }
2583
2584 free(re);
2585 }
2586
2587 return (NULL);
2588 }
2589
2590
2591 /*
2592 * End of "$Id$".
2593 */