]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-driverd.cxx
Merge changes from CUPS 1.5svn-r9675.
[thirdparty/cups.git] / scheduler / cups-driverd.cxx
1 /*
2 * "$Id$"
3 *
4 * PPD/driver support for CUPS.
5 *
6 * This program handles listing and installing static PPD files, PPD files
7 * created from driver information files, and dynamically generated PPD files
8 * using driver helper programs.
9 *
10 * Copyright 2007-2011 by Apple Inc.
11 * Copyright 1997-2007 by Easy Software Products.
12 *
13 * These coded instructions, statements, and computer programs are the
14 * property of Apple Inc. and are protected by Federal copyright
15 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
16 * which should have been included with this file. If this file is
17 * file is missing or damaged, see the license at "http://www.cups.org/".
18 *
19 * Contents:
20 *
21 * main() - Scan for drivers and return an IPP response.
22 * add_ppd() - Add a PPD file.
23 * cat_drv() - Generate a PPD from a driver info file.
24 * cat_ppd() - Copy a PPD file to stdout.
25 * copy_static() - Copy a static PPD file to stdout.
26 * compare_inodes() - Compare two inodes.
27 * compare_matches() - Compare PPD match scores for sorting.
28 * compare_names() - Compare PPD filenames for sorting.
29 * compare_ppds() - Compare PPD file make and model names for sorting.
30 * dump_ppds_dat() - Dump the contents of the ppds.dat file.
31 * free_array() - Free an array of strings.
32 * list_ppds() - List PPD files.
33 * load_drv() - Load the PPDs from a driver information file.
34 * load_drivers() - Load driver-generated PPD files.
35 * load_ppds() - Load PPD files recursively.
36 * load_ppds_dat() - Load the ppds.dat file.
37 * regex_device_id() - Compile a regular expression based on the 1284 device
38 * ID.
39 * regex_string() - Construct a regular expression to compare a simple
40 * string.
41 */
42
43 /*
44 * Include necessary headers...
45 */
46
47 #include "util.h"
48 #include <cups/dir.h>
49 #include <cups/transcode.h>
50 #include <cups/ppd-private.h>
51 #include <ppdc/ppdc.h>
52 #include <regex.h>
53
54
55 /*
56 * Constants...
57 */
58
59 #define PPD_SYNC 0x50504437 /* Sync word for ppds.dat (PPD7) */
60 #define PPD_MAX_LANG 32 /* Maximum languages */
61 #define PPD_MAX_PROD 32 /* Maximum products */
62 #define PPD_MAX_VERS 32 /* Maximum versions */
63
64 #define PPD_TYPE_POSTSCRIPT 0 /* PostScript PPD */
65 #define PPD_TYPE_PDF 1 /* PDF PPD */
66 #define PPD_TYPE_RASTER 2 /* CUPS raster PPD */
67 #define PPD_TYPE_FAX 3 /* Facsimile/MFD PPD */
68 #define PPD_TYPE_UNKNOWN 4 /* Other/hybrid PPD */
69 #define PPD_TYPE_DRV 5 /* Driver info file */
70
71 static const char * const ppd_types[] = /* ppd-type values */
72 {
73 "postscript",
74 "pdf",
75 "raster",
76 "fax",
77 "unknown",
78 "drv"
79 };
80
81
82 /*
83 * PPD information structures...
84 */
85
86 typedef struct /**** PPD record ****/
87 {
88 time_t mtime; /* Modification time */
89 off_t size; /* Size in bytes */
90 int model_number; /* cupsModelNumber */
91 int type; /* ppd-type */
92 char filename[512], /* Filename */
93 name[512], /* PPD name */
94 languages[PPD_MAX_LANG][6],
95 /* LanguageVersion/cupsLanguages */
96 products[PPD_MAX_PROD][128],
97 /* Product strings */
98 psversions[PPD_MAX_VERS][32],
99 /* PSVersion strings */
100 make[128], /* Manufacturer */
101 make_and_model[128], /* NickName/ModelName */
102 device_id[256], /* IEEE 1284 Device ID */
103 scheme[128]; /* PPD scheme */
104 } ppd_rec_t;
105
106 typedef struct /**** In-memory record ****/
107 {
108 int found; /* 1 if PPD is found */
109 int matches; /* Match count */
110 ppd_rec_t record; /* PPDs.dat record */
111 } ppd_info_t;
112
113
114 /*
115 * Globals...
116 */
117
118 cups_array_t *Inodes = NULL, /* Inodes of directories we've visited */
119 *PPDsByName = NULL, /* PPD files sorted by filename and name */
120 *PPDsByMakeModel = NULL;/* PPD files sorted by make and model */
121 int ChangedPPD; /* Did we change the PPD database? */
122
123
124 /*
125 * Local functions...
126 */
127
128 static ppd_info_t *add_ppd(const char *filename, const char *name,
129 const char *language, const char *make,
130 const char *make_and_model,
131 const char *device_id, const char *product,
132 const char *psversion, time_t mtime,
133 size_t size, int model_number, int type,
134 const char *scheme);
135 static int cat_drv(const char *name, int request_id);
136 static int cat_ppd(const char *name, int request_id);
137 static int cat_static(const char *name, int request_id);
138 static int compare_inodes(struct stat *a, struct stat *b);
139 static int compare_matches(const ppd_info_t *p0,
140 const ppd_info_t *p1);
141 static int compare_names(const ppd_info_t *p0,
142 const ppd_info_t *p1);
143 static int compare_ppds(const ppd_info_t *p0,
144 const ppd_info_t *p1);
145 static int dump_ppds_dat(void);
146 static void free_array(cups_array_t *a);
147 static int list_ppds(int request_id, int limit, const char *opt);
148 static int load_drivers(cups_array_t *include,
149 cups_array_t *exclude);
150 static int load_drv(const char *filename, const char *name,
151 cups_file_t *fp, time_t mtime, off_t size);
152 static int load_ppds(const char *d, const char *p, int descend);
153 static void load_ppds_dat(char *filename, size_t filesize,
154 int verbose);
155 static regex_t *regex_device_id(const char *device_id);
156 static regex_t *regex_string(const char *s);
157
158
159 /*
160 * 'main()' - Scan for drivers and return an IPP response.
161 *
162 * Usage:
163 *
164 * cups-driverd request_id limit options
165 */
166
167 int /* O - Exit code */
168 main(int argc, /* I - Number of command-line args */
169 char *argv[]) /* I - Command-line arguments */
170 {
171 /*
172 * Install or list PPDs...
173 */
174
175 if (argc == 3 && !strcmp(argv[1], "cat"))
176 return (cat_ppd(argv[2], 0));
177 else if (argc == 2 && !strcmp(argv[1], "dump"))
178 return (dump_ppds_dat());
179 else if (argc == 4 && !strcmp(argv[1], "get"))
180 return (cat_ppd(argv[3], atoi(argv[2])));
181 else if (argc == 5 && !strcmp(argv[1], "list"))
182 return (list_ppds(atoi(argv[2]), atoi(argv[3]), argv[4]));
183 else
184 {
185 fputs("Usage: cups-driverd cat ppd-name\n", stderr);
186 fputs("Usage: cups-driverd dump\n", stderr);
187 fputs("Usage: cups-driverd get request_id ppd-name\n", stderr);
188 fputs("Usage: cups-driverd list request_id limit options\n", stderr);
189 return (1);
190 }
191 }
192
193
194 /*
195 * 'add_ppd()' - Add a PPD file.
196 */
197
198 static ppd_info_t * /* O - PPD */
199 add_ppd(const char *filename, /* I - PPD filename */
200 const char *name, /* I - PPD name */
201 const char *language, /* I - LanguageVersion */
202 const char *make, /* I - Manufacturer */
203 const char *make_and_model, /* I - NickName/ModelName */
204 const char *device_id, /* I - 1284DeviceID */
205 const char *product, /* I - Product */
206 const char *psversion, /* I - PSVersion */
207 time_t mtime, /* I - Modification time */
208 size_t size, /* I - File size */
209 int model_number, /* I - Model number */
210 int type, /* I - Driver type */
211 const char *scheme) /* I - PPD scheme */
212 {
213 ppd_info_t *ppd; /* PPD */
214 char *recommended; /* Foomatic driver string */
215
216
217 /*
218 * Add a new PPD file...
219 */
220
221 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
222 {
223 fprintf(stderr,
224 "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n",
225 cupsArrayCount(PPDsByName));
226 return (NULL);
227 }
228
229 /*
230 * Zero-out the PPD data and copy the values over...
231 */
232
233 ppd->found = 1;
234 ppd->record.mtime = mtime;
235 ppd->record.size = size;
236 ppd->record.model_number = model_number;
237 ppd->record.type = type;
238
239 strlcpy(ppd->record.filename, filename, sizeof(ppd->record.filename));
240 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
241 strlcpy(ppd->record.languages[0], language,
242 sizeof(ppd->record.languages[0]));
243 strlcpy(ppd->record.products[0], product, sizeof(ppd->record.products[0]));
244 strlcpy(ppd->record.psversions[0], psversion,
245 sizeof(ppd->record.psversions[0]));
246 strlcpy(ppd->record.make, make, sizeof(ppd->record.make));
247 strlcpy(ppd->record.make_and_model, make_and_model,
248 sizeof(ppd->record.make_and_model));
249 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
250 strlcpy(ppd->record.scheme, scheme, sizeof(ppd->record.scheme));
251
252 /*
253 * Strip confusing (and often wrong) "recommended" suffix added by
254 * Foomatic drivers...
255 */
256
257 if ((recommended = strstr(ppd->record.make_and_model,
258 " (recommended)")) != NULL)
259 *recommended = '\0';
260
261 /*
262 * Add the PPD to the PPD arrays...
263 */
264
265 cupsArrayAdd(PPDsByName, ppd);
266 cupsArrayAdd(PPDsByMakeModel, ppd);
267
268 /*
269 * Return the new PPD pointer...
270 */
271
272 return (ppd);
273 }
274
275
276 /*
277 * 'cat_drv()' - Generate a PPD from a driver info file.
278 */
279
280 static int /* O - Exit code */
281 cat_drv(const char *name, /* I - PPD name */
282 int request_id) /* I - Request ID for response? */
283 {
284 const char *datadir; // CUPS_DATADIR env var
285 ppdcSource *src; // PPD source file data
286 ppdcDriver *d; // Current driver
287 cups_file_t *out; // Stdout via CUPS file API
288 char message[2048], // status-message
289 filename[1024], // Full path to .drv file(s)
290 scheme[32], // URI scheme ("drv")
291 userpass[256], // User/password info (unused)
292 host[2], // Hostname (unused)
293 resource[1024], // Resource path (/dir/to/filename.drv)
294 *pc_file_name; // Filename portion of URI
295 int port; // Port number (unused)
296
297
298 // Determine where CUPS has installed the data files...
299 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
300 datadir = CUPS_DATADIR;
301
302 // Pull out the
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 char newname[1024]; /* New filename */
926
927 snprintf(newname, sizeof(newname), "%s.%d", filename, (int)getpid());
928
929 if ((fp = cupsFileOpen(newname, "w")) != NULL)
930 {
931 unsigned ppdsync = PPD_SYNC; /* Sync word */
932
933 cupsFileWrite(fp, (char *)&ppdsync, sizeof(ppdsync));
934
935 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
936 ppd;
937 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
938 cupsFileWrite(fp, (char *)&(ppd->record), sizeof(ppd_rec_t));
939
940 cupsFileClose(fp);
941
942 if (rename(newname, filename))
943 fprintf(stderr, "ERROR: [cups-driverd] Unable to rename \"%s\" - %s\n",
944 newname, strerror(errno));
945 else
946 fprintf(stderr, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
947 filename, cupsArrayCount(PPDsByName));
948 }
949 else
950 fprintf(stderr, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
951 filename, strerror(errno));
952 }
953 else
954 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr);
955
956 /*
957 * Scan for dynamic PPD files...
958 */
959
960 num_options = cupsParseOptions(opt, 0, &options);
961 exclude = cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
962 num_options, options));
963 include = cupsdCreateStringsArray(cupsGetOption("include-schemes",
964 num_options, options));
965
966 load_drivers(include, exclude);
967
968 /*
969 * Add the raw driver...
970 */
971
972 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
973 PPD_TYPE_UNKNOWN, "raw");
974
975 /*
976 * Send IPP attributes...
977 */
978
979 requested = cupsdCreateStringsArray(
980 cupsGetOption("requested-attributes", num_options,
981 options));
982 device_id = cupsGetOption("ppd-device-id", num_options, options);
983 language = cupsGetOption("ppd-natural-language", num_options, options);
984 make = cupsGetOption("ppd-make", num_options, options);
985 make_and_model = cupsGetOption("ppd-make-and-model", num_options, options);
986 model_number_str = cupsGetOption("ppd-model-number", num_options, options);
987 product = cupsGetOption("ppd-product", num_options, options);
988 psversion = cupsGetOption("ppd-psversion", num_options, options);
989 type_str = cupsGetOption("ppd-type", num_options, options);
990
991 if (make_and_model)
992 make_and_model_len = strlen(make_and_model);
993 else
994 make_and_model_len = 0;
995
996 if (product)
997 product_len = strlen(product);
998 else
999 product_len = 0;
1000
1001 if (model_number_str)
1002 model_number = atoi(model_number_str);
1003 else
1004 model_number = 0;
1005
1006 if (type_str)
1007 {
1008 for (type = 0;
1009 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
1010 type ++)
1011 if (!strcmp(type_str, ppd_types[type]))
1012 break;
1013
1014 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
1015 {
1016 fprintf(stderr, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
1017 type_str);
1018 type_str = NULL;
1019 }
1020 }
1021 else
1022 type = 0;
1023
1024 for (i = 0; i < num_options; i ++)
1025 fprintf(stderr, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options[i].name,
1026 options[i].value);
1027
1028 if (!requested || cupsArrayFind(requested, (void *)"all") != NULL)
1029 {
1030 send_name = 1;
1031 send_make = 1;
1032 send_make_and_model = 1;
1033 send_model_number = 1;
1034 send_natural_language = 1;
1035 send_device_id = 1;
1036 send_product = 1;
1037 send_psversion = 1;
1038 send_type = 1;
1039 }
1040 else
1041 {
1042 send_name = cupsArrayFind(requested,
1043 (void *)"ppd-name") != NULL;
1044 send_make = cupsArrayFind(requested,
1045 (void *)"ppd-make") != NULL;
1046 send_make_and_model = cupsArrayFind(requested,
1047 (void *)"ppd-make-and-model") != NULL;
1048 send_model_number = cupsArrayFind(requested,
1049 (void *)"ppd-model-number") != NULL;
1050 send_natural_language = cupsArrayFind(requested,
1051 (void *)"ppd-natural-language") != NULL;
1052 send_device_id = cupsArrayFind(requested,
1053 (void *)"ppd-device-id") != NULL;
1054 send_product = cupsArrayFind(requested,
1055 (void *)"ppd-product") != NULL;
1056 send_psversion = cupsArrayFind(requested,
1057 (void *)"ppd-psversion") != NULL;
1058 send_type = cupsArrayFind(requested,
1059 (void *)"ppd-type") != NULL;
1060 }
1061
1062 /*
1063 * Send the content type header to the scheduler; request_id can only be
1064 * 0 when run manually since the scheduler enforces the IPP requirement for
1065 * a request ID from 1 to 2^31-1...
1066 */
1067
1068 if (request_id > 0)
1069 puts("Content-Type: application/ipp\n");
1070
1071 sent_header = 0;
1072
1073 if (limit <= 0 || limit > cupsArrayCount(PPDsByMakeModel))
1074 count = cupsArrayCount(PPDsByMakeModel);
1075 else
1076 count = limit;
1077
1078 if (device_id || language || make || make_and_model || model_number_str ||
1079 product)
1080 {
1081 matches = cupsArrayNew((cups_array_func_t)compare_matches, NULL);
1082
1083 if (device_id)
1084 device_id_re = regex_device_id(device_id);
1085 else
1086 device_id_re = NULL;
1087
1088 if (make_and_model)
1089 make_and_model_re = regex_string(make_and_model);
1090 else
1091 make_and_model_re = NULL;
1092
1093 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1094 ppd;
1095 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
1096 {
1097 /*
1098 * Filter PPDs based on make, model, product, language, model number,
1099 * and/or device ID using the "matches" score value. An exact match
1100 * for product, make-and-model, or device-id adds 3 to the score.
1101 * Partial matches for make-and-model yield 1 or 2 points, and matches
1102 * for the make and language add a single point. Results are then sorted
1103 * by score, highest score first.
1104 */
1105
1106 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1107 ppd->record.type >= PPD_TYPE_DRV)
1108 continue;
1109
1110 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1111 (include && !cupsArrayFind(include, ppd->record.scheme)))
1112 continue;
1113
1114 ppd->matches = 0;
1115
1116 if (device_id_re &&
1117 !regexec(device_id_re, ppd->record.device_id,
1118 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1119 re_matches, 0))
1120 {
1121 /*
1122 * Add the number of matching values from the device ID - it will be
1123 * at least 2 (manufacturer and model), and as much as 3 (command set).
1124 */
1125
1126 for (i = 1; i < (int)(sizeof(re_matches) / sizeof(re_matches[0])); i ++)
1127 if (re_matches[i].rm_so >= 0)
1128 ppd->matches ++;
1129 }
1130
1131 if (language)
1132 {
1133 for (i = 0; i < PPD_MAX_LANG; i ++)
1134 if (!ppd->record.languages[i][0])
1135 break;
1136 else if (!strcmp(ppd->record.languages[i], language))
1137 {
1138 ppd->matches ++;
1139 break;
1140 }
1141 }
1142
1143 if (make && !strcasecmp(ppd->record.make, make))
1144 ppd->matches ++;
1145
1146 if (make_and_model_re &&
1147 !regexec(make_and_model_re, ppd->record.make_and_model,
1148 (int)(sizeof(re_matches) / sizeof(re_matches[0])),
1149 re_matches, 0))
1150 {
1151 // See how much of the make-and-model string we matched...
1152 if (re_matches[0].rm_so == 0)
1153 {
1154 if (re_matches[0].rm_eo == make_and_model_len)
1155 ppd->matches += 3; // Exact match
1156 else
1157 ppd->matches += 2; // Prefix match
1158 }
1159 else
1160 ppd->matches ++; // Infix match
1161 }
1162
1163 if (model_number_str && ppd->record.model_number == model_number)
1164 ppd->matches ++;
1165
1166 if (product)
1167 {
1168 for (i = 0; i < PPD_MAX_PROD; i ++)
1169 if (!ppd->record.products[i][0])
1170 break;
1171 else if (!strcasecmp(ppd->record.products[i], product))
1172 {
1173 ppd->matches += 3;
1174 break;
1175 }
1176 }
1177
1178 if (psversion)
1179 {
1180 for (i = 0; i < PPD_MAX_VERS; i ++)
1181 if (!ppd->record.psversions[i][0])
1182 break;
1183 else if (!strcasecmp(ppd->record.psversions[i], psversion))
1184 {
1185 ppd->matches ++;
1186 break;
1187 }
1188 }
1189
1190 if (type_str && ppd->record.type == type)
1191 ppd->matches ++;
1192
1193 if (ppd->matches)
1194 {
1195 fprintf(stderr, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
1196 ppd->record.name, ppd->matches);
1197 cupsArrayAdd(matches, ppd);
1198 }
1199 }
1200 }
1201 else if (include || exclude)
1202 {
1203 matches = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
1204
1205 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1206 ppd;
1207 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
1208 {
1209 /*
1210 * Filter PPDs based on the include/exclude lists.
1211 */
1212
1213 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1214 ppd->record.type >= PPD_TYPE_DRV)
1215 continue;
1216
1217 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1218 (include && !cupsArrayFind(include, ppd->record.scheme)))
1219 continue;
1220
1221 cupsArrayAdd(matches, ppd);
1222 }
1223 }
1224 else
1225 matches = PPDsByMakeModel;
1226
1227 for (ppd = (ppd_info_t *)cupsArrayFirst(matches);
1228 count > 0 && ppd;
1229 ppd = (ppd_info_t *)cupsArrayNext(matches))
1230 {
1231 /*
1232 * Skip invalid PPDs...
1233 */
1234
1235 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1236 ppd->record.type >= PPD_TYPE_DRV)
1237 continue;
1238
1239 /*
1240 * Send this PPD...
1241 */
1242
1243 if (!sent_header)
1244 {
1245 sent_header = 1;
1246
1247 cupsdSendIPPHeader(IPP_OK, request_id);
1248 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1249 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
1250 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
1251 "en-US");
1252 }
1253
1254 fprintf(stderr, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
1255 ppd->record.name, ppd->record.make_and_model);
1256
1257 count --;
1258
1259 cupsdSendIPPGroup(IPP_TAG_PRINTER);
1260
1261 if (send_name)
1262 cupsdSendIPPString(IPP_TAG_NAME, "ppd-name", ppd->record.name);
1263
1264 if (send_natural_language)
1265 {
1266 cupsdSendIPPString(IPP_TAG_LANGUAGE, "ppd-natural-language",
1267 ppd->record.languages[0]);
1268
1269 for (i = 1; i < PPD_MAX_LANG && ppd->record.languages[i][0]; i ++)
1270 cupsdSendIPPString(IPP_TAG_LANGUAGE, "", ppd->record.languages[i]);
1271 }
1272
1273 if (send_make)
1274 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make", ppd->record.make);
1275
1276 if (send_make_and_model)
1277 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make-and-model",
1278 ppd->record.make_and_model);
1279
1280 if (send_device_id)
1281 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-device-id",
1282 ppd->record.device_id);
1283
1284 if (send_product)
1285 {
1286 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-product",
1287 ppd->record.products[0]);
1288
1289 for (i = 1; i < PPD_MAX_PROD && ppd->record.products[i][0]; i ++)
1290 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.products[i]);
1291 }
1292
1293 if (send_psversion)
1294 {
1295 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-psversion",
1296 ppd->record.psversions[0]);
1297
1298 for (i = 1; i < PPD_MAX_VERS && ppd->record.psversions[i][0]; i ++)
1299 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.psversions[i]);
1300 }
1301
1302 if (send_type)
1303 cupsdSendIPPString(IPP_TAG_KEYWORD, "ppd-type",
1304 ppd_types[ppd->record.type]);
1305
1306 if (send_model_number)
1307 cupsdSendIPPInteger(IPP_TAG_INTEGER, "ppd-model-number",
1308 ppd->record.model_number);
1309
1310 /*
1311 * If we have only requested the ppd-make attribute, then skip
1312 * the remaining PPDs with this make...
1313 */
1314
1315 if (cupsArrayFind(requested, (void *)"ppd-make") &&
1316 cupsArrayCount(requested) == 1)
1317 {
1318 const char *this_make; /* This ppd-make */
1319
1320
1321 for (this_make = ppd->record.make,
1322 ppd = (ppd_info_t *)cupsArrayNext(matches);
1323 ppd;
1324 ppd = (ppd_info_t *)cupsArrayNext(matches))
1325 if (strcasecmp(this_make, ppd->record.make))
1326 break;
1327
1328 cupsArrayPrev(matches);
1329 }
1330 }
1331
1332 if (!sent_header)
1333 {
1334 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
1335 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1336 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
1337 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
1338 }
1339
1340 cupsdSendIPPTrailer();
1341
1342 return (0);
1343 }
1344
1345
1346 /*
1347 * 'load_drv()' - Load the PPDs from a driver information file.
1348 */
1349
1350 static int /* O - 1 on success, 0 on failure */
1351 load_drv(const char *filename, /* I - Actual filename */
1352 const char *name, /* I - Name to the rest of the world */
1353 cups_file_t *fp, /* I - File to read from */
1354 time_t mtime, /* I - Mod time of driver info file */
1355 off_t size) /* I - Size of driver info file */
1356 {
1357 ppdcSource *src; // Driver information file
1358 ppdcDriver *d; // Current driver
1359 ppdcAttr *device_id, // 1284DeviceID attribute
1360 *product, // Current product value
1361 *ps_version, // PSVersion attribute
1362 *cups_fax, // cupsFax attribute
1363 *nick_name; // NickName attribute
1364 ppdcFilter *filter; // Current filter
1365 ppd_info_t *ppd; // Current PPD
1366 int products_found; // Number of products found
1367 char uri[1024], // Driver URI
1368 make_model[1024]; // Make and model
1369 int type; // Driver type
1370
1371
1372 /*
1373 * Load the driver info file...
1374 */
1375
1376 src = new ppdcSource(filename, fp);
1377
1378 if (src->drivers->count == 0)
1379 {
1380 fprintf(stderr,
1381 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1382 filename);
1383 src->release();
1384 return (0);
1385 }
1386
1387 /*
1388 * Add a dummy entry for the file...
1389 */
1390
1391 add_ppd(name, name, "", "", "", "", "", "", mtime, size, 0,
1392 PPD_TYPE_DRV, "drv");
1393 ChangedPPD = 1;
1394
1395 /*
1396 * Then the drivers in the file...
1397 */
1398
1399 for (d = (ppdcDriver *)src->drivers->first();
1400 d;
1401 d = (ppdcDriver *)src->drivers->next())
1402 {
1403 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0,
1404 "/%s/%s", name,
1405 d->file_name ? d->file_name->value :
1406 d->pc_file_name->value);
1407
1408 device_id = d->find_attr("1284DeviceID", NULL);
1409 ps_version = d->find_attr("PSVersion", NULL);
1410 nick_name = d->find_attr("NickName", NULL);
1411
1412 if (nick_name)
1413 strlcpy(make_model, nick_name->value->value, sizeof(make_model));
1414 else if (strncasecmp(d->model_name->value, d->manufacturer->value,
1415 strlen(d->manufacturer->value)))
1416 snprintf(make_model, sizeof(make_model), "%s %s, %s",
1417 d->manufacturer->value, d->model_name->value,
1418 d->version->value);
1419 else
1420 snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value,
1421 d->version->value);
1422
1423 if ((cups_fax = d->find_attr("cupsFax", NULL)) != NULL &&
1424 !strcasecmp(cups_fax->value->value, "true"))
1425 type = PPD_TYPE_FAX;
1426 else if (d->type == PPDC_DRIVER_PS)
1427 type = PPD_TYPE_POSTSCRIPT;
1428 else if (d->type != PPDC_DRIVER_CUSTOM)
1429 type = PPD_TYPE_RASTER;
1430 else
1431 {
1432 for (filter = (ppdcFilter *)d->filters->first(),
1433 type = PPD_TYPE_POSTSCRIPT;
1434 filter;
1435 filter = (ppdcFilter *)d->filters->next())
1436 if (strcasecmp(filter->mime_type->value, "application/vnd.cups-raster"))
1437 type = PPD_TYPE_RASTER;
1438 else if (strcasecmp(filter->mime_type->value,
1439 "application/vnd.cups-pdf"))
1440 type = PPD_TYPE_PDF;
1441 }
1442
1443 for (product = (ppdcAttr *)d->attrs->first(), products_found = 0,
1444 ppd = NULL;
1445 product;
1446 product = (ppdcAttr *)d->attrs->next())
1447 if (!strcmp(product->name->value, "Product"))
1448 {
1449 if (!products_found)
1450 ppd = add_ppd(name, uri, "en", d->manufacturer->value, make_model,
1451 device_id ? device_id->value->value : "",
1452 product->value->value,
1453 ps_version ? ps_version->value->value : "(3010) 0",
1454 mtime, size, d->model_number, type, "drv");
1455 else if (products_found < PPD_MAX_PROD)
1456 strlcpy(ppd->record.products[products_found], product->value->value,
1457 sizeof(ppd->record.products[0]));
1458 else
1459 break;
1460
1461 products_found ++;
1462 }
1463
1464 if (!products_found)
1465 add_ppd(name, uri, "en", d->manufacturer->value, make_model,
1466 device_id ? device_id->value->value : "",
1467 d->model_name->value,
1468 ps_version ? ps_version->value->value : "(3010) 0",
1469 mtime, size, d->model_number, type, "drv");
1470 }
1471
1472 src->release();
1473
1474 return (1);
1475 }
1476
1477
1478 /*
1479 * 'load_drivers()' - Load driver-generated PPD files.
1480 */
1481
1482 static int /* O - 1 on success, 0 on failure */
1483 load_drivers(cups_array_t *include, /* I - Drivers to include */
1484 cups_array_t *exclude) /* I - Drivers to exclude */
1485 {
1486 int i; /* Looping var */
1487 char *start, /* Start of value */
1488 *ptr; /* Pointer into string */
1489 const char *server_bin, /* CUPS_SERVERBIN env variable */
1490 *scheme, /* Scheme for this driver */
1491 *scheme_end; /* Pointer to end of scheme */
1492 char drivers[1024]; /* Location of driver programs */
1493 int pid; /* Process ID for driver program */
1494 cups_file_t *fp; /* Pipe to driver program */
1495 cups_dir_t *dir; /* Directory pointer */
1496 cups_dentry_t *dent; /* Directory entry */
1497 char *argv[3], /* Arguments for command */
1498 filename[1024], /* Name of driver */
1499 line[2048], /* Line from driver */
1500 name[512], /* ppd-name */
1501 make[128], /* ppd-make */
1502 make_and_model[128], /* ppd-make-and-model */
1503 device_id[128], /* ppd-device-id */
1504 languages[128], /* ppd-natural-language */
1505 product[128], /* ppd-product */
1506 psversion[128], /* ppd-psversion */
1507 type_str[128]; /* ppd-type */
1508 int type; /* PPD type */
1509 ppd_info_t *ppd; /* Newly added PPD */
1510
1511
1512 /*
1513 * Try opening the driver directory...
1514 */
1515
1516 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
1517 server_bin = CUPS_SERVERBIN;
1518
1519 snprintf(drivers, sizeof(drivers), "%s/driver", server_bin);
1520
1521 if ((dir = cupsDirOpen(drivers)) == NULL)
1522 {
1523 fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory "
1524 "\"%s\": %s\n",
1525 drivers, strerror(errno));
1526 return (0);
1527 }
1528
1529 /*
1530 * Loop through all of the device drivers...
1531 */
1532
1533 argv[1] = (char *)"list";
1534 argv[2] = NULL;
1535
1536 while ((dent = cupsDirRead(dir)) != NULL)
1537 {
1538 /*
1539 * Only look at executable files...
1540 */
1541
1542 if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode))
1543 continue;
1544
1545 /*
1546 * Include/exclude specific drivers...
1547 */
1548
1549 if (exclude)
1550 {
1551 /*
1552 * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
1553 */
1554
1555 for (scheme = (char *)cupsArrayFirst(exclude);
1556 scheme;
1557 scheme = (char *)cupsArrayNext(exclude))
1558 {
1559 fprintf(stderr, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
1560 dent->filename, scheme);
1561 scheme_end = scheme + strlen(scheme) - 1;
1562
1563 if ((scheme_end > scheme && *scheme_end == '*' &&
1564 !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
1565 !strcmp(scheme, dent->filename))
1566 {
1567 fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr);
1568 break;
1569 }
1570 }
1571
1572 if (scheme)
1573 continue;
1574 }
1575
1576 if (include)
1577 {
1578 /*
1579 * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
1580 */
1581
1582 for (scheme = (char *)cupsArrayFirst(include);
1583 scheme;
1584 scheme = (char *)cupsArrayNext(include))
1585 {
1586 fprintf(stderr, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
1587 dent->filename, scheme);
1588 scheme_end = scheme + strlen(scheme) - 1;
1589
1590 if ((scheme_end > scheme && *scheme_end == '*' &&
1591 !strncmp(scheme, dent->filename, scheme_end - scheme)) ||
1592 !strcmp(scheme, dent->filename))
1593 {
1594 fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr);
1595 break;
1596 }
1597 }
1598
1599 if (!scheme)
1600 continue;
1601 }
1602 else
1603 scheme = dent->filename;
1604
1605 /*
1606 * Run the driver with no arguments and collect the output...
1607 */
1608
1609 argv[0] = dent->filename;
1610 snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename);
1611
1612 if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL)
1613 {
1614 while (cupsFileGets(fp, line, sizeof(line)))
1615 {
1616 /*
1617 * Each line is of the form:
1618 *
1619 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
1620 * "ppd-device-id" "ppd-product" "ppd-psversion"
1621 */
1622
1623 device_id[0] = '\0';
1624 product[0] = '\0';
1625 psversion[0] = '\0';
1626 strcpy(type_str, "postscript");
1627
1628 if (sscanf(line, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
1629 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1630 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1631 "%*[ \t]\"%127[^\"]\"",
1632 name, languages, make, make_and_model,
1633 device_id, product, psversion, type_str) < 4)
1634 {
1635 /*
1636 * Bad format; strip trailing newline and write an error message.
1637 */
1638
1639 if (line[strlen(line) - 1] == '\n')
1640 line[strlen(line) - 1] = '\0';
1641
1642 fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1643 dent->filename, line);
1644 break;
1645 }
1646 else
1647 {
1648 /*
1649 * Add the device to the array of available devices...
1650 */
1651
1652 if ((start = strchr(languages, ',')) != NULL)
1653 *start++ = '\0';
1654
1655 for (type = 0;
1656 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
1657 type ++)
1658 if (!strcmp(type_str, ppd_types[type]))
1659 break;
1660
1661 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
1662 {
1663 fprintf(stderr,
1664 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
1665 type_str);
1666 type = PPD_TYPE_UNKNOWN;
1667 }
1668
1669 ppd = add_ppd(filename, name, languages, make, make_and_model,
1670 device_id, product, psversion, 0, 0, 0, type, scheme);
1671
1672 if (!ppd)
1673 {
1674 cupsDirClose(dir);
1675 cupsFileClose(fp);
1676 return (0);
1677 }
1678
1679 if (start && *start)
1680 {
1681 for (i = 1; i < PPD_MAX_LANG && *start; i ++)
1682 {
1683 if ((ptr = strchr(start, ',')) != NULL)
1684 *ptr++ = '\0';
1685 else
1686 ptr = start + strlen(start);
1687
1688 strlcpy(ppd->record.languages[i], start,
1689 sizeof(ppd->record.languages[0]));
1690
1691 start = ptr;
1692 }
1693 }
1694
1695 fprintf(stderr, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1696 name);
1697 }
1698 }
1699
1700 cupsFileClose(fp);
1701 }
1702 else
1703 fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1704 filename, strerror(errno));
1705 }
1706
1707 cupsDirClose(dir);
1708
1709 return (1);
1710 }
1711
1712
1713 /*
1714 * 'load_ppds()' - Load PPD files recursively.
1715 */
1716
1717 static int /* O - 1 on success, 0 on failure */
1718 load_ppds(const char *d, /* I - Actual directory */
1719 const char *p, /* I - Virtual path in name */
1720 int descend) /* I - Descend into directories? */
1721 {
1722 struct stat dinfo, /* Directory information */
1723 *dinfoptr; /* Pointer to match */
1724 int i; /* Looping var */
1725 cups_file_t *fp; /* Pointer to file */
1726 cups_dir_t *dir; /* Directory pointer */
1727 cups_dentry_t *dent; /* Directory entry */
1728 char filename[1024], /* Name of PPD or directory */
1729 line[256], /* Line from backend */
1730 *ptr, /* Pointer into name */
1731 name[128], /* Name of PPD file */
1732 lang_version[64], /* PPD LanguageVersion */
1733 lang_encoding[64], /* PPD LanguageEncoding */
1734 country[64], /* Country code */
1735 manufacturer[256], /* Manufacturer */
1736 make_model[256], /* Make and Model */
1737 model_name[256], /* ModelName */
1738 nick_name[256], /* NickName */
1739 device_id[256], /* 1284DeviceID */
1740 product[256], /* Product */
1741 psversion[256], /* PSVersion */
1742 temp[512]; /* Temporary make and model */
1743 int model_number, /* cupsModelNumber */
1744 type; /* ppd-type */
1745 cups_array_t *products, /* Product array */
1746 *psversions, /* PSVersion array */
1747 *cups_languages; /* cupsLanguages array */
1748 ppd_info_t *ppd, /* New PPD file */
1749 key; /* Search key */
1750 int new_ppd; /* Is this a new PPD? */
1751 struct /* LanguageVersion translation table */
1752 {
1753 const char *version, /* LanguageVersion string */
1754 *language; /* Language code */
1755 } languages[] =
1756 {
1757 { "chinese", "zh" },
1758 { "czech", "cs" },
1759 { "danish", "da" },
1760 { "dutch", "nl" },
1761 { "english", "en" },
1762 { "finnish", "fi" },
1763 { "french", "fr" },
1764 { "german", "de" },
1765 { "greek", "el" },
1766 { "hungarian", "hu" },
1767 { "italian", "it" },
1768 { "japanese", "ja" },
1769 { "korean", "ko" },
1770 { "norwegian", "no" },
1771 { "polish", "pl" },
1772 { "portuguese", "pt" },
1773 { "russian", "ru" },
1774 { "simplified chinese", "zh_CN" },
1775 { "slovak", "sk" },
1776 { "spanish", "es" },
1777 { "swedish", "sv" },
1778 { "traditional chinese", "zh_TW" },
1779 { "turkish", "tr" }
1780 };
1781
1782
1783 /*
1784 * See if we've loaded this directory before...
1785 */
1786
1787 if (stat(d, &dinfo))
1788 {
1789 if (errno != ENOENT)
1790 fprintf(stderr, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d,
1791 strerror(errno));
1792
1793 return (0);
1794 }
1795 else if (cupsArrayFind(Inodes, &dinfo))
1796 {
1797 fprintf(stderr, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
1798 d);
1799 return (0);
1800 }
1801
1802 /*
1803 * Nope, add it to the Inodes array and continue...
1804 */
1805
1806 dinfoptr = (struct stat *)malloc(sizeof(struct stat));
1807 memcpy(dinfoptr, &dinfo, sizeof(struct stat));
1808 cupsArrayAdd(Inodes, dinfoptr);
1809
1810 if ((dir = cupsDirOpen(d)) == NULL)
1811 {
1812 if (errno != ENOENT)
1813 fprintf(stderr,
1814 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1815 d, strerror(errno));
1816
1817 return (0);
1818 }
1819
1820 fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d);
1821
1822 while ((dent = cupsDirRead(dir)) != NULL)
1823 {
1824 /*
1825 * Skip files/directories starting with "."...
1826 */
1827
1828 if (dent->filename[0] == '.')
1829 continue;
1830
1831 /*
1832 * See if this is a file...
1833 */
1834
1835 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
1836
1837 if (p[0])
1838 snprintf(name, sizeof(name), "%s/%s", p, dent->filename);
1839 else
1840 strlcpy(name, dent->filename, sizeof(name));
1841
1842 if (S_ISDIR(dent->fileinfo.st_mode))
1843 {
1844 /*
1845 * Do subdirectory...
1846 */
1847
1848 if (descend)
1849 if (!load_ppds(filename, name, 1))
1850 {
1851 cupsDirClose(dir);
1852 return (1);
1853 }
1854
1855 continue;
1856 }
1857 else if ((ptr = filename + strlen(filename) - 6) > filename &&
1858 !strcmp(ptr, ".plist"))
1859 {
1860 /*
1861 * Skip plist files in the PPDs directory...
1862 */
1863
1864 continue;
1865 }
1866
1867 /*
1868 * See if this file has been scanned before...
1869 */
1870
1871 strcpy(key.record.filename, name);
1872 strcpy(key.record.name, name);
1873
1874 ppd = (ppd_info_t *)cupsArrayFind(PPDsByName, &key);
1875
1876 if (ppd &&
1877 ppd->record.size == dent->fileinfo.st_size &&
1878 ppd->record.mtime == dent->fileinfo.st_mtime)
1879 {
1880 /*
1881 * Rewind to the first entry for this file...
1882 */
1883
1884 while ((ppd = (ppd_info_t *)cupsArrayPrev(PPDsByName)) != NULL &&
1885 !strcmp(ppd->record.filename, name));
1886
1887 /*
1888 * Then mark all of the matches for this file as found...
1889 */
1890
1891 while ((ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) != NULL &&
1892 !strcmp(ppd->record.filename, name))
1893 ppd->found = 1;
1894
1895 continue;
1896 }
1897
1898 /*
1899 * No, file is new/changed, so re-scan it...
1900 */
1901
1902 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1903 continue;
1904
1905 /*
1906 * Now see if this is a PPD file...
1907 */
1908
1909 line[0] = '\0';
1910 cupsFileGets(fp, line, sizeof(line));
1911
1912 if (strncmp(line, "*PPD-Adobe:", 11))
1913 {
1914 /*
1915 * Nope, treat it as a driver information file...
1916 */
1917
1918 load_drv(filename, name, fp, dent->fileinfo.st_mtime,
1919 dent->fileinfo.st_size);
1920 continue;
1921 }
1922
1923 /*
1924 * Now read until we get the NickName field...
1925 */
1926
1927 cups_languages = cupsArrayNew(NULL, NULL);
1928 products = cupsArrayNew(NULL, NULL);
1929 psversions = cupsArrayNew(NULL, NULL);
1930
1931 model_name[0] = '\0';
1932 nick_name[0] = '\0';
1933 manufacturer[0] = '\0';
1934 device_id[0] = '\0';
1935 lang_encoding[0] = '\0';
1936 strcpy(lang_version, "en");
1937 model_number = 0;
1938 type = PPD_TYPE_POSTSCRIPT;
1939
1940 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
1941 {
1942 if (!strncmp(line, "*Manufacturer:", 14))
1943 sscanf(line, "%*[^\"]\"%255[^\"]", manufacturer);
1944 else if (!strncmp(line, "*ModelName:", 11))
1945 sscanf(line, "%*[^\"]\"%127[^\"]", model_name);
1946 else if (!strncmp(line, "*LanguageEncoding:", 18))
1947 sscanf(line, "%*[^:]:%63s", lang_encoding);
1948 else if (!strncmp(line, "*LanguageVersion:", 17))
1949 sscanf(line, "%*[^:]:%63s", lang_version);
1950 else if (!strncmp(line, "*NickName:", 10))
1951 sscanf(line, "%*[^\"]\"%255[^\"]", nick_name);
1952 else if (!strncasecmp(line, "*1284DeviceID:", 14))
1953 {
1954 sscanf(line, "%*[^\"]\"%255[^\"]", device_id);
1955
1956 // Make sure device ID ends with a semicolon...
1957 if (device_id[0] && device_id[strlen(device_id) - 1] != ';')
1958 strlcat(device_id, ";", sizeof(device_id));
1959 }
1960 else if (!strncmp(line, "*Product:", 9))
1961 {
1962 if (sscanf(line, "%*[^\"]\"(%255[^\"]", product) == 1)
1963 {
1964 /*
1965 * Make sure the value ends with a right parenthesis - can't stop at
1966 * the first right paren since the product name may contain escaped
1967 * parenthesis...
1968 */
1969
1970 ptr = product + strlen(product) - 1;
1971 if (ptr > product && *ptr == ')')
1972 {
1973 /*
1974 * Yes, ends with a parenthesis, so remove it from the end and
1975 * add the product to the list...
1976 */
1977
1978 *ptr = '\0';
1979 cupsArrayAdd(products, strdup(product));
1980 }
1981 }
1982 }
1983 else if (!strncmp(line, "*PSVersion:", 11))
1984 {
1985 sscanf(line, "%*[^\"]\"%255[^\"]", psversion);
1986 cupsArrayAdd(psversions, strdup(psversion));
1987 }
1988 else if (!strncmp(line, "*cupsLanguages:", 15))
1989 {
1990 char *start; /* Start of language */
1991
1992
1993 for (start = line + 15; *start && isspace(*start & 255); start ++);
1994
1995 if (*start++ == '\"')
1996 {
1997 while (*start)
1998 {
1999 for (ptr = start + 1;
2000 *ptr && *ptr != '\"' && !isspace(*ptr & 255);
2001 ptr ++);
2002
2003 if (*ptr)
2004 {
2005 *ptr++ = '\0';
2006
2007 while (isspace(*ptr & 255))
2008 *ptr++ = '\0';
2009 }
2010
2011 cupsArrayAdd(cups_languages, strdup(start));
2012 start = ptr;
2013 }
2014 }
2015 }
2016 else if (!strncmp(line, "*cupsFax:", 9))
2017 {
2018 for (ptr = line + 9; isspace(*ptr & 255); ptr ++);
2019
2020 if (!strncasecmp(ptr, "true", 4))
2021 type = PPD_TYPE_FAX;
2022 }
2023 else if (!strncmp(line, "*cupsFilter:", 12) && type == PPD_TYPE_POSTSCRIPT)
2024 {
2025 if (strstr(line + 12, "application/vnd.cups-raster"))
2026 type = PPD_TYPE_RASTER;
2027 else if (strstr(line + 12, "application/vnd.cups-pdf"))
2028 type = PPD_TYPE_PDF;
2029 }
2030 else if (!strncmp(line, "*cupsModelNumber:", 17))
2031 sscanf(line, "*cupsModelNumber:%d", &model_number);
2032 else if (!strncmp(line, "*OpenUI", 7))
2033 {
2034 /*
2035 * Stop early if we have a NickName or ModelName attributes
2036 * before the first OpenUI...
2037 */
2038
2039 if ((model_name[0] || nick_name[0]) && cupsArrayCount(products) > 0 &&
2040 cupsArrayCount(psversions) > 0)
2041 break;
2042 }
2043 }
2044
2045 /*
2046 * Close the file...
2047 */
2048
2049 cupsFileClose(fp);
2050
2051 /*
2052 * See if we got all of the required info...
2053 */
2054
2055 if (nick_name[0])
2056 cupsCharsetToUTF8((cups_utf8_t *)make_model, nick_name,
2057 sizeof(make_model), _ppdGetEncoding(lang_encoding));
2058 else
2059 strcpy(make_model, model_name);
2060
2061 while (isspace(make_model[0] & 255))
2062 _cups_strcpy(make_model, make_model + 1);
2063
2064 if (!make_model[0] || cupsArrayCount(products) == 0 ||
2065 cupsArrayCount(psversions) == 0)
2066 {
2067 /*
2068 * We don't have all the info needed, so skip this file...
2069 */
2070
2071 if (!make_model[0])
2072 fprintf(stderr, "WARNING: Missing NickName and ModelName in %s!\n",
2073 filename);
2074
2075 if (cupsArrayCount(products) == 0)
2076 fprintf(stderr, "WARNING: Missing Product in %s!\n", filename);
2077
2078 if (cupsArrayCount(psversions) == 0)
2079 fprintf(stderr, "WARNING: Missing PSVersion in %s!\n", filename);
2080
2081 free_array(products);
2082 free_array(psversions);
2083 free_array(cups_languages);
2084
2085 continue;
2086 }
2087
2088 if (model_name[0])
2089 cupsArrayAdd(products, strdup(model_name));
2090
2091 /*
2092 * Normalize the make and model string...
2093 */
2094
2095 while (isspace(manufacturer[0] & 255))
2096 _cups_strcpy(manufacturer, manufacturer + 1);
2097
2098 if (!strncasecmp(make_model, manufacturer, strlen(manufacturer)))
2099 strlcpy(temp, make_model, sizeof(temp));
2100 else
2101 snprintf(temp, sizeof(temp), "%s %s", manufacturer, make_model);
2102
2103 _ppdNormalizeMakeAndModel(temp, make_model, sizeof(make_model));
2104
2105 /*
2106 * See if we got a manufacturer...
2107 */
2108
2109 if (!manufacturer[0] || !strcmp(manufacturer, "ESP"))
2110 {
2111 /*
2112 * Nope, copy the first part of the make and model then...
2113 */
2114
2115 strlcpy(manufacturer, make_model, sizeof(manufacturer));
2116
2117 /*
2118 * Truncate at the first space, dash, or slash, or make the
2119 * manufacturer "Other"...
2120 */
2121
2122 for (ptr = manufacturer; *ptr; ptr ++)
2123 if (*ptr == ' ' || *ptr == '-' || *ptr == '/')
2124 break;
2125
2126 if (*ptr && ptr > manufacturer)
2127 *ptr = '\0';
2128 else
2129 strcpy(manufacturer, "Other");
2130 }
2131 else if (!strncasecmp(manufacturer, "LHAG", 4) ||
2132 !strncasecmp(manufacturer, "linotype", 8))
2133 strcpy(manufacturer, "LHAG");
2134 else if (!strncasecmp(manufacturer, "Hewlett", 7))
2135 strcpy(manufacturer, "HP");
2136
2137 /*
2138 * Fix the lang_version as needed...
2139 */
2140
2141 if ((ptr = strchr(lang_version, '-')) != NULL)
2142 *ptr++ = '\0';
2143 else if ((ptr = strchr(lang_version, '_')) != NULL)
2144 *ptr++ = '\0';
2145
2146 if (ptr)
2147 {
2148 /*
2149 * Setup the country suffix...
2150 */
2151
2152 country[0] = '_';
2153 _cups_strcpy(country + 1, ptr);
2154 }
2155 else
2156 {
2157 /*
2158 * No country suffix...
2159 */
2160
2161 country[0] = '\0';
2162 }
2163
2164 for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++)
2165 if (!strcasecmp(languages[i].version, lang_version))
2166 break;
2167
2168 if (i < (int)(sizeof(languages) / sizeof(languages[0])))
2169 {
2170 /*
2171 * Found a known language...
2172 */
2173
2174 snprintf(lang_version, sizeof(lang_version), "%s%s",
2175 languages[i].language, country);
2176 }
2177 else
2178 {
2179 /*
2180 * Unknown language; use "xx"...
2181 */
2182
2183 strcpy(lang_version, "xx");
2184 }
2185
2186 /*
2187 * Record the PPD file...
2188 */
2189
2190 new_ppd = !ppd;
2191
2192 if (new_ppd)
2193 {
2194 /*
2195 * Add new PPD file...
2196 */
2197
2198 fprintf(stderr, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name);
2199
2200 ppd = add_ppd(name, name, lang_version, manufacturer, make_model,
2201 device_id, (char *)cupsArrayFirst(products),
2202 (char *)cupsArrayFirst(psversions),
2203 dent->fileinfo.st_mtime, dent->fileinfo.st_size,
2204 model_number, type, "file");
2205
2206 if (!ppd)
2207 {
2208 cupsDirClose(dir);
2209 return (0);
2210 }
2211 }
2212 else
2213 {
2214 /*
2215 * Update existing record...
2216 */
2217
2218 fprintf(stderr, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name);
2219
2220 memset(ppd, 0, sizeof(ppd_info_t));
2221
2222 ppd->found = 1;
2223 ppd->record.mtime = dent->fileinfo.st_mtime;
2224 ppd->record.size = dent->fileinfo.st_size;
2225 ppd->record.model_number = model_number;
2226 ppd->record.type = type;
2227
2228 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
2229 strlcpy(ppd->record.make, manufacturer, sizeof(ppd->record.make));
2230 strlcpy(ppd->record.make_and_model, make_model,
2231 sizeof(ppd->record.make_and_model));
2232 strlcpy(ppd->record.languages[0], lang_version,
2233 sizeof(ppd->record.languages[0]));
2234 strlcpy(ppd->record.products[0], (char *)cupsArrayFirst(products),
2235 sizeof(ppd->record.products[0]));
2236 strlcpy(ppd->record.psversions[0], (char *)cupsArrayFirst(psversions),
2237 sizeof(ppd->record.psversions[0]));
2238 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
2239 }
2240
2241 /*
2242 * Add remaining products, versions, and languages...
2243 */
2244
2245 for (i = 1;
2246 i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL;
2247 i ++)
2248 strlcpy(ppd->record.products[i], ptr,
2249 sizeof(ppd->record.products[0]));
2250
2251 for (i = 1;
2252 i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL;
2253 i ++)
2254 strlcpy(ppd->record.psversions[i], ptr,
2255 sizeof(ppd->record.psversions[0]));
2256
2257 for (i = 1, ptr = (char *)cupsArrayFirst(cups_languages);
2258 i < PPD_MAX_LANG && ptr;
2259 i ++, ptr = (char *)cupsArrayNext(cups_languages))
2260 strlcpy(ppd->record.languages[i], ptr,
2261 sizeof(ppd->record.languages[0]));
2262
2263 /*
2264 * Free products, versions, and languages...
2265 */
2266
2267 free_array(cups_languages);
2268 free_array(products);
2269 free_array(psversions);
2270
2271 ChangedPPD = 1;
2272 }
2273
2274 cupsDirClose(dir);
2275
2276 return (1);
2277 }
2278
2279
2280 /*
2281 * 'load_ppds_dat()' - Load the ppds.dat file.
2282 */
2283
2284 static void
2285 load_ppds_dat(char *filename, /* I - Filename buffer */
2286 size_t filesize, /* I - Size of filename buffer */
2287 int verbose) /* I - Be verbose? */
2288 {
2289 ppd_info_t *ppd; /* Current PPD file */
2290 cups_file_t *fp; /* ppds.dat file */
2291 struct stat fileinfo; /* ppds.dat information */
2292 const char *cups_cachedir; /* CUPS_CACHEDIR environment variable */
2293
2294
2295 PPDsByName = cupsArrayNew((cups_array_func_t)compare_names, NULL);
2296 PPDsByMakeModel = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
2297 ChangedPPD = 0;
2298
2299 if ((cups_cachedir = getenv("CUPS_CACHEDIR")) == NULL)
2300 cups_cachedir = CUPS_CACHEDIR;
2301
2302 snprintf(filename, filesize, "%s/ppds.dat", cups_cachedir);
2303 if ((fp = cupsFileOpen(filename, "r")) != NULL)
2304 {
2305 /*
2306 * See if we have the right sync word...
2307 */
2308
2309 unsigned ppdsync; /* Sync word */
2310 int num_ppds; /* Number of PPDs */
2311
2312 if (cupsFileRead(fp, (char *)&ppdsync, sizeof(ppdsync))
2313 == sizeof(ppdsync) &&
2314 ppdsync == PPD_SYNC &&
2315 !stat(filename, &fileinfo) &&
2316 ((fileinfo.st_size - sizeof(ppdsync)) % sizeof(ppd_rec_t)) == 0 &&
2317 (num_ppds = (fileinfo.st_size - sizeof(ppdsync)) /
2318 sizeof(ppd_rec_t)) > 0)
2319 {
2320 /*
2321 * We have a ppds.dat file, so read it!
2322 */
2323
2324 for (; num_ppds > 0; num_ppds --)
2325 {
2326 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
2327 {
2328 if (verbose)
2329 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
2330 stderr);
2331 exit(1);
2332 }
2333
2334 if (cupsFileRead(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)) > 0)
2335 {
2336 cupsArrayAdd(PPDsByName, ppd);
2337 cupsArrayAdd(PPDsByMakeModel, ppd);
2338 }
2339 else
2340 {
2341 free(ppd);
2342 break;
2343 }
2344 }
2345
2346 if (verbose)
2347 fprintf(stderr, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
2348 filename, cupsArrayCount(PPDsByName));
2349 }
2350
2351 cupsFileClose(fp);
2352 }
2353 }
2354
2355
2356 /*
2357 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2358 * ID.
2359 */
2360
2361 static regex_t * /* O - Regular expression */
2362 regex_device_id(const char *device_id) /* I - IEEE-1284 device ID */
2363 {
2364 char res[2048], /* Regular expression string */
2365 *ptr; /* Pointer into string */
2366 regex_t *re; /* Regular expression */
2367 int cmd; /* Command set string? */
2368
2369
2370 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id);
2371
2372 /*
2373 * Scan the device ID string and insert class, command set, manufacturer, and
2374 * model attributes to match. We assume that the device ID in the PPD and the
2375 * device ID reported by the device itself use the same attribute names and
2376 * order of attributes.
2377 */
2378
2379 ptr = res;
2380
2381 while (*device_id && ptr < (res + sizeof(res) - 6))
2382 {
2383 cmd = !strncasecmp(device_id, "COMMAND SET:", 12) ||
2384 !strncasecmp(device_id, "CMD:", 4);
2385
2386 if (cmd || !strncasecmp(device_id, "MANUFACTURER:", 13) ||
2387 !strncasecmp(device_id, "MFG:", 4) ||
2388 !strncasecmp(device_id, "MFR:", 4) ||
2389 !strncasecmp(device_id, "MODEL:", 6) ||
2390 !strncasecmp(device_id, "MDL:", 4))
2391 {
2392 if (ptr > res)
2393 {
2394 *ptr++ = '.';
2395 *ptr++ = '*';
2396 }
2397
2398 *ptr++ = '(';
2399
2400 while (*device_id && *device_id != ';' && ptr < (res + sizeof(res) - 8))
2401 {
2402 if (strchr("[]{}().*\\|", *device_id))
2403 *ptr++ = '\\';
2404 if (*device_id == ':')
2405 {
2406 /*
2407 * KEY:.*value
2408 */
2409
2410 *ptr++ = *device_id++;
2411 *ptr++ = '.';
2412 *ptr++ = '*';
2413 }
2414 else
2415 *ptr++ = *device_id++;
2416 }
2417
2418 if (*device_id == ';' || !*device_id)
2419 {
2420 /*
2421 * KEY:.*value.*;
2422 */
2423
2424 *ptr++ = '.';
2425 *ptr++ = '*';
2426 *ptr++ = ';';
2427 }
2428 *ptr++ = ')';
2429 if (cmd)
2430 *ptr++ = '?';
2431 }
2432 else if ((device_id = strchr(device_id, ';')) == NULL)
2433 break;
2434 else
2435 device_id ++;
2436 }
2437
2438 *ptr = '\0';
2439
2440 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res);
2441
2442 /*
2443 * Compile the regular expression and return...
2444 */
2445
2446 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2447 {
2448 if (!regcomp(re, res, REG_EXTENDED | REG_ICASE))
2449 {
2450 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr);
2451 return (re);
2452 }
2453
2454 free(re);
2455 }
2456
2457 return (NULL);
2458 }
2459
2460
2461 /*
2462 * 'regex_string()' - Construct a regular expression to compare a simple string.
2463 */
2464
2465 static regex_t * /* O - Regular expression */
2466 regex_string(const char *s) /* I - String to compare */
2467 {
2468 char res[2048], /* Regular expression string */
2469 *ptr; /* Pointer into string */
2470 regex_t *re; /* Regular expression */
2471
2472
2473 fprintf(stderr, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s);
2474
2475 /*
2476 * Convert the string to a regular expression, escaping special characters
2477 * as needed.
2478 */
2479
2480 ptr = res;
2481
2482 while (*s && ptr < (res + sizeof(res) - 2))
2483 {
2484 if (strchr("[]{}().*\\", *s))
2485 *ptr++ = '\\';
2486
2487 *ptr++ = *s++;
2488 }
2489
2490 *ptr = '\0';
2491
2492 fprintf(stderr, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res);
2493
2494 /*
2495 * Create a case-insensitive regular expression...
2496 */
2497
2498 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2499 {
2500 if (!regcomp(re, res, REG_ICASE))
2501 {
2502 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr);
2503 return (re);
2504 }
2505
2506 free(re);
2507 }
2508
2509 return (NULL);
2510 }
2511
2512
2513 /*
2514 * End of "$Id$".
2515 */