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