]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-driverd.cxx
4 * PPD/driver support for the Common UNIX Printing System (CUPS).
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.
10 * Copyright 2007-2009 by Apple Inc.
11 * Copyright 1997-2007 by Easy Software Products.
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/".
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
39 * regex_string() - Construct a regular expression to compare a simple
44 * Include necessary headers...
49 #include <cups/transcode.h>
50 #include <cups/ppd-private.h>
51 #include <ppdc/ppdc.h>
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 */
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 */
71 static const char * const ppd_types
[] = /* ppd-type values */
83 * PPD information structures...
86 typedef struct /**** PPD record ****/
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],
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 */
106 typedef struct /**** In-memory record ****/
108 int found
; /* 1 if PPD is found */
109 int matches
; /* Match count */
110 ppd_rec_t record
; /* PPDs.dat record */
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? */
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
,
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
,
155 static regex_t
*regex_device_id(const char *device_id
);
156 static regex_t
*regex_string(const char *s
);
160 * 'main()' - Scan for drivers and return an IPP response.
164 * cups-driverd request_id limit options
167 int /* O - Exit code */
168 main(int argc
, /* I - Number of command-line args */
169 char *argv
[]) /* I - Command-line arguments */
172 * Install or list PPDs...
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]));
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
);
195 * 'add_ppd()' - Add a PPD file.
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 */
213 ppd_info_t
*ppd
; /* PPD */
214 char *recommended
; /* Foomatic driver string */
218 * Add a new PPD file...
221 if ((ppd
= (ppd_info_t
*)calloc(1, sizeof(ppd_info_t
))) == NULL
)
224 "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n",
225 cupsArrayCount(PPDsByName
));
230 * Zero-out the PPD data and copy the values over...
234 ppd
->record
.mtime
= mtime
;
235 ppd
->record
.size
= size
;
236 ppd
->record
.model_number
= model_number
;
237 ppd
->record
.type
= type
;
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
));
253 * Strip confusing (and often wrong) "recommended" suffix added by
254 * Foomatic drivers...
257 if ((recommended
= strstr(ppd
->record
.make_and_model
,
258 " (recommended)")) != NULL
)
262 * Add the PPD to the PPD arrays...
265 cupsArrayAdd(PPDsByName
, ppd
);
266 cupsArrayAdd(PPDsByMakeModel
, ppd
);
269 * Return the new PPD pointer...
277 * 'cat_drv()' - Generate a PPD from a driver info file.
280 static int /* O - Exit code */
281 cat_drv(const char *name
, /* I - PPD name */
282 int request_id
) /* I - Request ID for response? */
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)
298 // Determine where CUPS has installed the data files...
299 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
300 datadir
= CUPS_DATADIR
;
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
)
310 fprintf(stderr
, "ERROR: Bad PPD name \"%s\"!\n", name
);
314 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\"!", name
);
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",
321 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
322 cupsdSendIPPTrailer();
328 *pc_file_name
++ = '\0';
331 if (!strncmp(resource
, "/Library/Printers/PPDs.drv/", 27))
332 strlcpy(filename
, resource
, sizeof(filename
));
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
);
341 src
= new ppdcSource(filename
);
343 for (d
= (ppdcDriver
*)src
->drivers
->first();
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
)))
352 ppdcArray
*locales
; // Locale names
353 ppdcCatalog
*catalog
; // Message catalog in .drv file
356 fprintf(stderr
, "DEBUG2: [cups-driverd] %d locales defined in \"%s\"...\n",
357 src
->po_files
->count
, filename
);
359 locales
= new ppdcArray();
360 for (catalog
= (ppdcCatalog
*)src
->po_files
->first();
362 catalog
= (ppdcCatalog
*)src
->po_files
->next())
364 fprintf(stderr
, "DEBUG2: [cups-driverd] Adding locale \"%s\"...\n",
365 catalog
->locale
->value
);
366 catalog
->locale
->retain();
367 locales
->add(catalog
->locale
);
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",
377 cupsdSendIPPTrailer();
381 out
= cupsFileStdout();
382 d
->write_ppd_file(out
, NULL
, locales
, src
, PPDC_LFONLY
);
389 fprintf(stderr
, "ERROR: PPD \"%s\" not found!\n", name
);
393 snprintf(message
, sizeof(message
), "PPD \"%s\" not found!", name
);
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",
400 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
401 cupsdSendIPPTrailer();
412 * 'cat_ppd()' - Copy a PPD file to stdout.
415 static int /* O - Exit code */
416 cat_ppd(const char *name
, /* I - PPD name */
417 int request_id
) /* I - Request ID for response? */
419 char scheme
[256], /* Scheme from PPD name */
420 *sptr
, /* Pointer into scheme */
421 line
[1024], /* Line/filename */
422 message
[2048]; /* status-message */
426 * Figure out if this is a static or dynamic PPD file...
429 strlcpy(scheme
, name
, sizeof(scheme
));
430 if ((sptr
= strchr(scheme
, ':')) != NULL
)
434 if (!strcmp(scheme
, "file"))
437 * "file:name" == "name"...
448 puts("Content-Type: application/ipp\n");
451 return (cat_static(name
, request_id
));
452 else if (!strcmp(scheme
, "drv"))
453 return (cat_drv(name
, request_id
));
457 * Dynamic PPD, see if we have a driver program to support it...
460 const char *serverbin
; /* CUPS_SERVERBIN env var */
461 char *argv
[4]; /* Arguments for program */
464 if ((serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
465 serverbin
= CUPS_SERVERBIN
;
467 snprintf(line
, sizeof(line
), "%s/driver/%s", serverbin
, scheme
);
468 if (access(line
, X_OK
))
471 * File does not exist or is not executable...
474 fprintf(stderr
, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n",
475 line
, strerror(errno
));
479 snprintf(message
, sizeof(message
), "Unable to access \"%s\" - %s",
480 line
, strerror(errno
));
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",
487 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
488 cupsdSendIPPTrailer();
495 * Yes, let it cat the PPD file...
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",
505 cupsdSendIPPTrailer();
509 argv
[1] = (char *)"cat";
510 argv
[2] = (char *)name
;
513 if (cupsdExec(line
, argv
))
516 * Unable to execute driver...
519 fprintf(stderr
, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n",
520 line
, strerror(errno
));
526 * Return with no errors...
534 * 'copy_static()' - Copy a static PPD file to stdout.
537 static int /* O - Exit code */
538 cat_static(const char *name
, /* I - PPD name */
539 int request_id
) /* I - Request ID for response? */
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 */
547 if (name
[0] == '/' || strstr(name
, "../") || strstr(name
, "/.."))
553 fprintf(stderr
, "ERROR: [cups-driverd] Bad PPD name \"%s\"!\n", name
);
557 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\"!", name
);
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",
564 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
565 cupsdSendIPPTrailer();
572 * Try opening the file...
576 if (!strncmp(name
, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
577 !strncmp(name
, "Library/Printers/PPDs/Contents/Resources/", 41))
580 * Map ppd-name to Mac OS X standard locations...
583 snprintf(line
, sizeof(line
), "/%s", name
);
587 #elif defined(__linux)
588 if (!strncmp(name
, "lsb/usr/", 8))
591 * Map ppd-name to LSB standard /usr/share/ppd location...
594 snprintf(line
, sizeof(line
), "/usr/share/ppd/%s", name
+ 8);
596 else if (!strncmp(name
, "lsb/opt/", 8))
599 * Map ppd-name to LSB standard /opt/share/ppd location...
602 snprintf(line
, sizeof(line
), "/opt/share/ppd/%s", name
+ 8);
604 else if (!strncmp(name
, "lsb/local/", 10))
607 * Map ppd-name to LSB standard /usr/local/share/ppd location...
610 snprintf(line
, sizeof(line
), "/usr/local/share/ppd/%s", name
+ 10);
614 #endif /* __APPLE__ */
616 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
617 datadir
= CUPS_DATADIR
;
619 snprintf(line
, sizeof(line
), "%s/model/%s", datadir
, name
);
622 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
624 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n",
625 line
, strerror(errno
));
629 snprintf(message
, sizeof(message
), "Unable to open \"%s\" - %s",
630 line
, strerror(errno
));
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",
637 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
638 cupsdSendIPPTrailer();
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",
651 cupsdSendIPPTrailer();
655 * Now copy the file to stdout...
658 while (cupsFileGets(fp
, line
, sizeof(line
)))
668 * 'compare_inodes()' - Compare two inodes.
671 static int /* O - Result of comparison */
672 compare_inodes(struct stat
*a
, /* I - First inode */
673 struct stat
*b
) /* I - Second inode */
675 if (a
->st_dev
!= b
->st_dev
)
676 return (a
->st_dev
- b
->st_dev
);
678 return (a
->st_ino
- b
->st_ino
);
683 * 'compare_matches()' - Compare PPD match scores for sorting.
687 compare_matches(const ppd_info_t
*p0
, /* I - First PPD */
688 const ppd_info_t
*p1
) /* I - Second PPD */
690 if (p1
->matches
!= p0
->matches
)
691 return (p1
->matches
- p0
->matches
);
693 return (cupsdCompareNames(p0
->record
.make_and_model
,
694 p1
->record
.make_and_model
));
699 * 'compare_names()' - Compare PPD filenames for sorting.
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 */
706 int diff
; /* Difference between strings */
709 if ((diff
= strcmp(p0
->record
.filename
, p1
->record
.filename
)) != 0)
712 return (strcmp(p0
->record
.name
, p1
->record
.name
));
717 * 'compare_ppds()' - Compare PPD file make and model names for sorting.
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 */
724 int diff
; /* Difference between strings */
728 * First compare manufacturers...
731 if ((diff
= strcasecmp(p0
->record
.make
, p1
->record
.make
)) != 0)
733 else if ((diff
= cupsdCompareNames(p0
->record
.make_and_model
,
734 p1
->record
.make_and_model
)) != 0)
737 return (strcmp(p0
->record
.languages
[0], p1
->record
.languages
[0]));
742 * 'dump_ppds_dat()' - Dump the contents of the ppds.dat file.
745 static int /* O - Exit status */
748 char filename
[1024]; /* ppds.dat filename */
749 ppd_info_t
*ppd
; /* Current PPD */
753 * See if we a PPD database file...
756 load_ppds_dat(filename
, sizeof(filename
), 0);
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
);
762 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
763 printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
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
,
777 * 'free_array()' - Free an array of strings.
781 free_array(cups_array_t
*a
) /* I - Array to free */
783 char *ptr
; /* Pointer to string */
786 for (ptr
= (char *)cupsArrayFirst(a
);
788 ptr
= (char *)cupsArrayNext(a
))
796 * 'list_ppds()' - List PPD files.
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 */
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 */
845 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
846 "opt=\"%s\"\n", request_id
, limit
, opt
);
849 * See if we a PPD database file...
852 load_ppds_dat(filename
, sizeof(filename
), 1);
855 * Load all PPDs in the specified directory and below...
858 if ((cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
859 cups_datadir
= CUPS_DATADIR
;
861 Inodes
= cupsArrayNew((cups_array_func_t
)compare_inodes
, NULL
);
863 snprintf(model
, sizeof(model
), "%s/model", cups_datadir
);
864 load_ppds(model
, "", 1);
866 snprintf(model
, sizeof(model
), "%s/drv", cups_datadir
);
867 load_ppds(model
, "", 1);
871 * Load PPDs from standard Mac OS X locations...
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);
883 #elif defined(__linux)
885 * Load PPDs from LSB-defined locations...
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__ */
897 * Cull PPD files that are no longer present...
900 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
902 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
906 * Remove this PPD file from the list...
909 cupsArrayRemove(PPDsByName
, ppd
);
910 cupsArrayRemove(PPDsByMakeModel
, ppd
);
917 * Write the new ppds.dat file...
920 fprintf(stderr
, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD
);
924 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
926 unsigned ppdsync
= PPD_SYNC
; /* Sync word */
929 cupsFileWrite(fp
, (char *)&ppdsync
, sizeof(ppdsync
));
931 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
933 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
934 cupsFileWrite(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
));
938 fprintf(stderr
, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
939 filename
, cupsArrayCount(PPDsByName
));
942 fprintf(stderr
, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
943 filename
, strerror(errno
));
946 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr
);
949 * Scan for dynamic PPD files...
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
));
958 load_drivers(include
, exclude
);
961 * Add the raw driver...
964 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
965 PPD_TYPE_UNKNOWN
, "raw");
968 * Send IPP attributes...
971 requested
= cupsdCreateStringsArray(
972 cupsGetOption("requested-attributes", num_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
);
984 make_and_model_len
= strlen(make_and_model
);
986 make_and_model_len
= 0;
989 product_len
= strlen(product
);
993 if (model_number_str
)
994 model_number
= atoi(model_number_str
);
1001 type
< (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0]));
1003 if (!strcmp(type_str
, ppd_types
[type
]))
1006 if (type
>= (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0])))
1008 fprintf(stderr
, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
1016 for (i
= 0; i
< num_options
; i
++)
1017 fprintf(stderr
, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options
[i
].name
,
1020 if (!requested
|| cupsArrayFind(requested
, (void *)"all") != NULL
)
1024 send_make_and_model
= 1;
1025 send_model_number
= 1;
1026 send_natural_language
= 1;
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
;
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...
1061 puts("Content-Type: application/ipp\n");
1065 if (limit
<= 0 || limit
> cupsArrayCount(PPDsByMakeModel
))
1066 count
= cupsArrayCount(PPDsByMakeModel
);
1070 if (device_id
|| language
|| make
|| make_and_model
|| model_number_str
||
1073 matches
= cupsArrayNew((cups_array_func_t
)compare_matches
, NULL
);
1076 device_id_re
= regex_device_id(device_id
);
1078 device_id_re
= NULL
;
1081 make_and_model_re
= regex_string(make_and_model
);
1083 make_and_model_re
= NULL
;
1085 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByMakeModel
);
1087 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
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.
1098 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1099 ppd
->record
.type
>= PPD_TYPE_DRV
)
1102 if (cupsArrayFind(exclude
, ppd
->record
.scheme
) ||
1103 (include
&& !cupsArrayFind(include
, ppd
->record
.scheme
)))
1109 !regexec(device_id_re
, ppd
->record
.device_id
,
1110 (int)(sizeof(re_matches
) / sizeof(re_matches
[0])),
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).
1118 for (i
= 1; i
< (int)(sizeof(re_matches
) / sizeof(re_matches
[0])); i
++)
1119 if (re_matches
[i
].rm_so
>= 0)
1125 for (i
= 0; i
< PPD_MAX_LANG
; i
++)
1126 if (!ppd
->record
.languages
[i
][0])
1128 else if (!strcmp(ppd
->record
.languages
[i
], language
))
1135 if (make
&& !strcasecmp(ppd
->record
.make
, make
))
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])),
1143 // See how much of the make-and-model string we matched...
1144 if (re_matches
[0].rm_so
== 0)
1146 if (re_matches
[0].rm_eo
== make_and_model_len
)
1147 ppd
->matches
+= 3; // Exact match
1149 ppd
->matches
+= 2; // Prefix match
1152 ppd
->matches
++; // Infix match
1155 if (model_number_str
&& ppd
->record
.model_number
== model_number
)
1160 for (i
= 0; i
< PPD_MAX_PROD
; i
++)
1161 if (!ppd
->record
.products
[i
][0])
1163 else if (!strcasecmp(ppd
->record
.products
[i
], product
))
1172 for (i
= 0; i
< PPD_MAX_VERS
; i
++)
1173 if (!ppd
->record
.psversions
[i
][0])
1175 else if (!strcasecmp(ppd
->record
.psversions
[i
], psversion
))
1182 if (type_str
&& ppd
->record
.type
== type
)
1187 fprintf(stderr
, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
1188 ppd
->record
.name
, ppd
->matches
);
1189 cupsArrayAdd(matches
, ppd
);
1193 else if (include
|| exclude
)
1195 matches
= cupsArrayNew((cups_array_func_t
)compare_ppds
, NULL
);
1197 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByMakeModel
);
1199 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
1202 * Filter PPDs based on the include/exclude lists.
1205 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1206 ppd
->record
.type
>= PPD_TYPE_DRV
)
1209 if (cupsArrayFind(exclude
, ppd
->record
.scheme
) ||
1210 (include
&& !cupsArrayFind(include
, ppd
->record
.scheme
)))
1213 cupsArrayAdd(matches
, ppd
);
1217 matches
= PPDsByMakeModel
;
1219 for (ppd
= (ppd_info_t
*)cupsArrayFirst(matches
);
1221 ppd
= (ppd_info_t
*)cupsArrayNext(matches
))
1224 * Skip invalid PPDs...
1227 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1228 ppd
->record
.type
>= PPD_TYPE_DRV
)
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",
1246 fprintf(stderr
, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
1247 ppd
->record
.name
, ppd
->record
.make_and_model
);
1251 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
1254 cupsdSendIPPString(IPP_TAG_NAME
, "ppd-name", ppd
->record
.name
);
1256 if (send_natural_language
)
1258 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "ppd-natural-language",
1259 ppd
->record
.languages
[0]);
1261 for (i
= 1; i
< PPD_MAX_LANG
&& ppd
->record
.languages
[i
][0]; i
++)
1262 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "", ppd
->record
.languages
[i
]);
1266 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make", ppd
->record
.make
);
1268 if (send_make_and_model
)
1269 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make-and-model",
1270 ppd
->record
.make_and_model
);
1273 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-device-id",
1274 ppd
->record
.device_id
);
1278 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-product",
1279 ppd
->record
.products
[0]);
1281 for (i
= 1; i
< PPD_MAX_PROD
&& ppd
->record
.products
[i
][0]; i
++)
1282 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.products
[i
]);
1287 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-psversion",
1288 ppd
->record
.psversions
[0]);
1290 for (i
= 1; i
< PPD_MAX_VERS
&& ppd
->record
.psversions
[i
][0]; i
++)
1291 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.psversions
[i
]);
1295 cupsdSendIPPString(IPP_TAG_KEYWORD
, "ppd-type",
1296 ppd_types
[ppd
->record
.type
]);
1298 if (send_model_number
)
1299 cupsdSendIPPInteger(IPP_TAG_INTEGER
, "ppd-model-number",
1300 ppd
->record
.model_number
);
1303 * If we have only requested the ppd-make attribute, then skip
1304 * the remaining PPDs with this make...
1307 if (cupsArrayFind(requested
, (void *)"ppd-make") &&
1308 cupsArrayCount(requested
) == 1)
1310 const char *this_make
; /* This ppd-make */
1313 for (this_make
= ppd
->record
.make
,
1314 ppd
= (ppd_info_t
*)cupsArrayNext(matches
);
1316 ppd
= (ppd_info_t
*)cupsArrayNext(matches
))
1317 if (strcasecmp(this_make
, ppd
->record
.make
))
1320 cupsArrayPrev(matches
);
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");
1332 cupsdSendIPPTrailer();
1339 * 'load_ppds()' - Load PPD files recursively.
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? */
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 */
1378 const char *version
, /* LanguageVersion string */
1379 *language
; /* Language code */
1382 { "chinese", "zh" },
1386 { "english", "en" },
1387 { "finnish", "fi" },
1391 { "hungarian", "hu" },
1392 { "italian", "it" },
1393 { "japanese", "ja" },
1395 { "norwegian", "no" },
1397 { "portuguese", "pt" },
1398 { "russian", "ru" },
1399 { "simplified chinese", "zh_CN" },
1401 { "spanish", "es" },
1402 { "swedish", "sv" },
1403 { "traditional chinese", "zh_TW" },
1409 * See if we've loaded this directory before...
1412 if (stat(d
, &dinfo
))
1414 if (errno
!= ENOENT
)
1415 fprintf(stderr
, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d
,
1420 else if (cupsArrayFind(Inodes
, &dinfo
))
1422 fprintf(stderr
, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
1428 * Nope, add it to the Inodes array and continue...
1431 dinfoptr
= (struct stat
*)malloc(sizeof(struct stat
));
1432 memcpy(dinfoptr
, &dinfo
, sizeof(struct stat
));
1433 cupsArrayAdd(Inodes
, dinfoptr
);
1435 if ((dir
= cupsDirOpen(d
)) == NULL
)
1437 if (errno
!= ENOENT
)
1439 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1440 d
, strerror(errno
));
1445 fprintf(stderr
, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d
);
1447 while ((dent
= cupsDirRead(dir
)) != NULL
)
1450 * Skip files/directories starting with "."...
1453 if (dent
->filename
[0] == '.')
1457 * See if this is a file...
1460 snprintf(filename
, sizeof(filename
), "%s/%s", d
, dent
->filename
);
1463 snprintf(name
, sizeof(name
), "%s/%s", p
, dent
->filename
);
1465 strlcpy(name
, dent
->filename
, sizeof(name
));
1467 if (S_ISDIR(dent
->fileinfo
.st_mode
))
1470 * Do subdirectory...
1474 if (!load_ppds(filename
, name
, 1))
1484 * See if this file has been scanned before...
1487 strcpy(key
.record
.filename
, name
);
1488 strcpy(key
.record
.name
, name
);
1490 ppd
= (ppd_info_t
*)cupsArrayFind(PPDsByName
, &key
);
1493 ppd
->record
.size
== dent
->fileinfo
.st_size
&&
1494 ppd
->record
.mtime
== dent
->fileinfo
.st_mtime
)
1500 while ((ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
)) != NULL
&&
1501 !strcmp(ppd
->record
.filename
, name
));
1507 * No, file is new/changed, so re-scan it...
1510 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1514 * Now see if this is a PPD file...
1518 cupsFileGets(fp
, line
, sizeof(line
));
1520 if (strncmp(line
, "*PPD-Adobe:", 11))
1523 * Nope, treat it as a driver information file...
1526 load_drv(filename
, name
, fp
, dent
->fileinfo
.st_mtime
,
1527 dent
->fileinfo
.st_size
);
1532 * Now read until we get the NickName field...
1535 cups_languages
= cupsArrayNew(NULL
, NULL
);
1536 products
= cupsArrayNew(NULL
, NULL
);
1537 psversions
= cupsArrayNew(NULL
, NULL
);
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");
1546 type
= PPD_TYPE_POSTSCRIPT
;
1548 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
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))
1562 sscanf(line
, "%*[^\"]\"%255[^\"]", device_id
);
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
));
1568 else if (!strncmp(line
, "*Product:", 9))
1570 if (sscanf(line
, "%*[^\"]\"(%255[^\"]", product
) == 1)
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
1578 ptr
= product
+ strlen(product
) - 1;
1579 if (ptr
> product
&& *ptr
== ')')
1582 * Yes, ends with a parenthesis, so remove it from the end and
1583 * add the product to the list...
1587 cupsArrayAdd(products
, strdup(product
));
1591 else if (!strncmp(line
, "*PSVersion:", 11))
1593 sscanf(line
, "%*[^\"]\"%255[^\"]", psversion
);
1594 cupsArrayAdd(psversions
, strdup(psversion
));
1596 else if (!strncmp(line
, "*cupsLanguages:", 15))
1598 char *start
; /* Start of language */
1601 for (start
= line
+ 15; *start
&& isspace(*start
& 255); start
++);
1603 if (*start
++ == '\"')
1607 for (ptr
= start
+ 1;
1608 *ptr
&& *ptr
!= '\"' && !isspace(*ptr
& 255);
1615 while (isspace(*ptr
& 255))
1619 cupsArrayAdd(cups_languages
, strdup(start
));
1624 else if (!strncmp(line
, "*cupsFax:", 9))
1626 for (ptr
= line
+ 9; isspace(*ptr
& 255); ptr
++);
1628 if (!strncasecmp(ptr
, "true", 4))
1629 type
= PPD_TYPE_FAX
;
1631 else if (!strncmp(line
, "*cupsFilter:", 12) && type
== PPD_TYPE_POSTSCRIPT
)
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
;
1638 else if (!strncmp(line
, "*cupsModelNumber:", 17))
1639 sscanf(line
, "*cupsModelNumber:%d", &model_number
);
1640 else if (!strncmp(line
, "*OpenUI", 7))
1643 * Stop early if we have a NickName or ModelName attributes
1644 * before the first OpenUI...
1647 if ((model_name
[0] || nick_name
[0]) && cupsArrayCount(products
) > 0 &&
1648 cupsArrayCount(psversions
) > 0)
1660 * See if we got all of the required info...
1664 cupsCharsetToUTF8((cups_utf8_t
*)make_model
, nick_name
,
1665 sizeof(make_model
), _ppdGetEncoding(lang_encoding
));
1667 strcpy(make_model
, model_name
);
1669 while (isspace(make_model
[0] & 255))
1670 _cups_strcpy(make_model
, make_model
+ 1);
1672 if (!make_model
[0] || cupsArrayCount(products
) == 0 ||
1673 cupsArrayCount(psversions
) == 0)
1676 * We don't have all the info needed, so skip this file...
1680 fprintf(stderr
, "WARNING: Missing NickName and ModelName in %s!\n",
1683 if (cupsArrayCount(products
) == 0)
1684 fprintf(stderr
, "WARNING: Missing Product in %s!\n", filename
);
1686 if (cupsArrayCount(psversions
) == 0)
1687 fprintf(stderr
, "WARNING: Missing PSVersion in %s!\n", filename
);
1689 free_array(products
);
1690 free_array(psversions
);
1691 free_array(cups_languages
);
1697 cupsArrayAdd(products
, strdup(model_name
));
1700 * Normalize the make and model string...
1703 while (isspace(manufacturer
[0] & 255))
1704 _cups_strcpy(manufacturer
, manufacturer
+ 1);
1706 if (!strncasecmp(make_model
, manufacturer
, strlen(manufacturer
)))
1707 strlcpy(temp
, make_model
, sizeof(temp
));
1709 snprintf(temp
, sizeof(temp
), "%s %s", manufacturer
, make_model
);
1711 _ppdNormalizeMakeAndModel(temp
, make_model
, sizeof(make_model
));
1714 * See if we got a manufacturer...
1717 if (!manufacturer
[0] || !strcmp(manufacturer
, "ESP"))
1720 * Nope, copy the first part of the make and model then...
1723 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
1726 * Truncate at the first space, dash, or slash, or make the
1727 * manufacturer "Other"...
1730 for (ptr
= manufacturer
; *ptr
; ptr
++)
1731 if (*ptr
== ' ' || *ptr
== '-' || *ptr
== '/')
1734 if (*ptr
&& ptr
> manufacturer
)
1737 strcpy(manufacturer
, "Other");
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");
1746 * Fix the lang_version as needed...
1749 if ((ptr
= strchr(lang_version
, '-')) != NULL
)
1751 else if ((ptr
= strchr(lang_version
, '_')) != NULL
)
1757 * Setup the country suffix...
1761 _cups_strcpy(country
+ 1, ptr
);
1766 * No country suffix...
1772 for (i
= 0; i
< (int)(sizeof(languages
) / sizeof(languages
[0])); i
++)
1773 if (!strcasecmp(languages
[i
].version
, lang_version
))
1776 if (i
< (int)(sizeof(languages
) / sizeof(languages
[0])))
1779 * Found a known language...
1782 snprintf(lang_version
, sizeof(lang_version
), "%s%s",
1783 languages
[i
].language
, country
);
1788 * Unknown language; use "xx"...
1791 strcpy(lang_version
, "xx");
1795 * Record the PPD file...
1803 * Add new PPD file...
1806 fprintf(stderr
, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name
);
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");
1823 * Update existing record...
1826 fprintf(stderr
, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name
);
1828 memset(ppd
, 0, sizeof(ppd_info_t
));
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
;
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
));
1850 * Add remaining products, versions, and languages...
1854 i
< PPD_MAX_PROD
&& (ptr
= (char *)cupsArrayNext(products
)) != NULL
;
1856 strlcpy(ppd
->record
.products
[i
], ptr
,
1857 sizeof(ppd
->record
.products
[0]));
1860 i
< PPD_MAX_VERS
&& (ptr
= (char *)cupsArrayNext(psversions
)) != NULL
;
1862 strlcpy(ppd
->record
.psversions
[i
], ptr
,
1863 sizeof(ppd
->record
.psversions
[0]));
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]));
1872 * Free products, versions, and languages...
1875 free_array(cups_languages
);
1876 free_array(products
);
1877 free_array(psversions
);
1889 * 'load_drv()' - Load the PPDs from a driver information file.
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 */
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
1914 * Load the driver info file...
1917 src
= new ppdcSource(filename
, fp
);
1919 if (src
->drivers
->count
== 0)
1922 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1929 * Add a dummy entry for the file...
1932 add_ppd(filename
, filename
, "", "", "", "", "", "", mtime
, size
, 0,
1933 PPD_TYPE_DRV
, "drv");
1937 * Then the drivers in the file...
1940 for (d
= (ppdcDriver
*)src
->drivers
->first();
1942 d
= (ppdcDriver
*)src
->drivers
->next())
1944 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "drv", "", "", 0,
1946 d
->file_name
? d
->file_name
->value
:
1947 d
->pc_file_name
->value
);
1949 device_id
= d
->find_attr("1284DeviceID", NULL
);
1950 ps_version
= d
->find_attr("PSVersion", NULL
);
1951 nick_name
= d
->find_attr("NickName", NULL
);
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
,
1961 snprintf(make_model
, sizeof(make_model
), "%s, %s", d
->model_name
->value
,
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
;
1973 for (filter
= (ppdcFilter
*)d
->filters
->first(),
1974 type
= PPD_TYPE_POSTSCRIPT
;
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
;
1984 for (product
= (ppdcAttr
*)d
->attrs
->first(), product_found
= false;
1986 product
= (ppdcAttr
*)d
->attrs
->next())
1987 if (!strcmp(product
->name
->value
, "Product"))
1989 product_found
= true;
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");
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");
2013 * 'load_drivers()' - Load driver-generated PPD files.
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 */
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 */
2047 * Try opening the driver directory...
2050 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
2051 server_bin
= CUPS_SERVERBIN
;
2053 snprintf(drivers
, sizeof(drivers
), "%s/driver", server_bin
);
2055 if ((dir
= cupsDirOpen(drivers
)) == NULL
)
2057 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open driver directory "
2059 drivers
, strerror(errno
));
2064 * Loop through all of the device drivers...
2067 argv
[1] = (char *)"list";
2070 while ((dent
= cupsDirRead(dir
)) != NULL
)
2073 * Only look at executable files...
2076 if (!(dent
->fileinfo
.st_mode
& 0111) || !S_ISREG(dent
->fileinfo
.st_mode
))
2080 * Include/exclude specific drivers...
2086 * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
2089 for (scheme
= (char *)cupsArrayFirst(exclude
);
2091 scheme
= (char *)cupsArrayNext(exclude
))
2093 fprintf(stderr
, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
2094 dent
->filename
, scheme
);
2095 scheme_end
= scheme
+ strlen(scheme
) - 1;
2097 if ((scheme_end
> scheme
&& *scheme_end
== '*' &&
2098 !strncmp(scheme
, dent
->filename
, scheme_end
- scheme
)) ||
2099 !strcmp(scheme
, dent
->filename
))
2101 fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr
);
2113 * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
2116 for (scheme
= (char *)cupsArrayFirst(include
);
2118 scheme
= (char *)cupsArrayNext(include
))
2120 fprintf(stderr
, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
2121 dent
->filename
, scheme
);
2122 scheme_end
= scheme
+ strlen(scheme
) - 1;
2124 if ((scheme_end
> scheme
&& *scheme_end
== '*' &&
2125 !strncmp(scheme
, dent
->filename
, scheme_end
- scheme
)) ||
2126 !strcmp(scheme
, dent
->filename
))
2128 fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr
);
2137 scheme
= dent
->filename
;
2140 * Run the driver with no arguments and collect the output...
2143 argv
[0] = dent
->filename
;
2144 snprintf(filename
, sizeof(filename
), "%s/%s", drivers
, dent
->filename
);
2146 if ((fp
= cupsdPipeCommand(&pid
, filename
, argv
, 0)) != NULL
)
2148 while (cupsFileGets(fp
, line
, sizeof(line
)))
2151 * Each line is of the form:
2153 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
2154 * "ppd-device-id" "ppd-product" "ppd-psversion"
2157 device_id
[0] = '\0';
2159 psversion
[0] = '\0';
2160 strcpy(type_str
, "postscript");
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)
2170 * Bad format; strip trailing newline and write an error message.
2173 if (line
[strlen(line
) - 1] == '\n')
2174 line
[strlen(line
) - 1] = '\0';
2176 fprintf(stderr
, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
2177 dent
->filename
, line
);
2183 * Add the device to the array of available devices...
2186 if ((start
= strchr(languages
, ',')) != NULL
)
2190 type
< (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0]));
2192 if (!strcmp(type_str
, ppd_types
[type
]))
2195 if (type
>= (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0])))
2198 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
2200 type
= PPD_TYPE_UNKNOWN
;
2203 ppd
= add_ppd(filename
, name
, languages
, make
, make_and_model
,
2204 device_id
, product
, psversion
, 0, 0, 0, type
, scheme
);
2213 if (start
&& *start
)
2215 for (i
= 1; i
< PPD_MAX_LANG
&& *start
; i
++)
2217 if ((ptr
= strchr(start
, ',')) != NULL
)
2220 ptr
= start
+ strlen(start
);
2222 strlcpy(ppd
->record
.languages
[i
], start
,
2223 sizeof(ppd
->record
.languages
[0]));
2229 fprintf(stderr
, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
2237 fprintf(stderr
, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
2238 filename
, strerror(errno
));
2248 * 'load_ppds_dat()' - Load the ppds.dat file.
2252 load_ppds_dat(char *filename
, /* I - Filename buffer */
2253 size_t filesize
, /* I - Size of filename buffer */
2254 int verbose
) /* I - Be verbose? */
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 */
2262 PPDsByName
= cupsArrayNew((cups_array_func_t
)compare_names
, NULL
);
2263 PPDsByMakeModel
= cupsArrayNew((cups_array_func_t
)compare_ppds
, NULL
);
2266 if ((cups_cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
2267 cups_cachedir
= CUPS_CACHEDIR
;
2269 snprintf(filename
, filesize
, "%s/ppds.dat", cups_cachedir
);
2270 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
2273 * See if we have the right sync word...
2276 unsigned ppdsync
; /* Sync word */
2277 int num_ppds
; /* Number of PPDs */
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)
2289 * We have a ppds.dat file, so read it!
2292 for (; num_ppds
> 0; num_ppds
--)
2294 if ((ppd
= (ppd_info_t
*)calloc(1, sizeof(ppd_info_t
))) == NULL
)
2297 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
2302 if (cupsFileRead(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
)) > 0)
2304 cupsArrayAdd(PPDsByName
, ppd
);
2305 cupsArrayAdd(PPDsByMakeModel
, ppd
);
2315 fprintf(stderr
, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
2316 filename
, cupsArrayCount(PPDsByName
));
2325 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2329 static regex_t
* /* O - Regular expression */
2330 regex_device_id(const char *device_id
) /* I - IEEE-1284 device ID */
2332 char res
[2048], /* Regular expression string */
2333 *ptr
; /* Pointer into string */
2334 regex_t
*re
; /* Regular expression */
2335 int cmd
; /* Command set string? */
2338 fprintf(stderr
, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id
);
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.
2349 while (*device_id
&& ptr
< (res
+ sizeof(res
) - 6))
2351 cmd
= !strncasecmp(device_id
, "COMMAND SET:", 12) ||
2352 !strncasecmp(device_id
, "CMD:", 4);
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))
2368 while (*device_id
&& *device_id
!= ';' && ptr
< (res
+ sizeof(res
) - 4))
2370 if (strchr("[]{}().*\\|", *device_id
))
2372 *ptr
++ = *device_id
++;
2375 if (*device_id
== ';' || !*device_id
)
2381 else if ((device_id
= strchr(device_id
, ';')) == NULL
)
2389 fprintf(stderr
, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res
);
2392 * Compile the regular expression and return...
2395 if (res
[0] && (re
= (regex_t
*)calloc(1, sizeof(regex_t
))) != NULL
)
2397 if (!regcomp(re
, res
, REG_EXTENDED
| REG_ICASE
))
2399 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr
);
2411 * 'regex_string()' - Construct a regular expression to compare a simple string.
2414 static regex_t
* /* O - Regular expression */
2415 regex_string(const char *s
) /* I - String to compare */
2417 char res
[2048], /* Regular expression string */
2418 *ptr
; /* Pointer into string */
2419 regex_t
*re
; /* Regular expression */
2422 fprintf(stderr
, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s
);
2425 * Convert the string to a regular expression, escaping special characters
2431 while (*s
&& ptr
< (res
+ sizeof(res
) - 2))
2433 if (strchr("[]{}().*\\", *s
))
2441 fprintf(stderr
, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res
);
2444 * Create a case-insensitive regular expression...
2447 if (res
[0] && (re
= (regex_t
*)calloc(1, sizeof(regex_t
))) != NULL
)
2449 if (!regcomp(re
, res
, REG_ICASE
))
2451 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr
);