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