]>
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 0x50504436 /* Sync word for ppds.dat (PPD6) */
60 #define PPD_MAX_LANG 32 /* Maximum languages */
61 #define PPD_MAX_PROD 8 /* Maximum products */
62 #define PPD_MAX_VERS 8 /* 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 locales
->add(catalog
->locale
);
371 cupsdSendIPPHeader(IPP_OK
, request_id
);
372 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
373 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
374 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
376 cupsdSendIPPTrailer();
380 out
= cupsFileStdout();
381 d
->write_ppd_file(out
, NULL
, locales
, src
, PPDC_LFONLY
);
388 fprintf(stderr
, "ERROR: PPD \"%s\" not found!\n", name
);
392 snprintf(message
, sizeof(message
), "PPD \"%s\" not found!", name
);
394 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
395 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
396 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
397 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
399 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
400 cupsdSendIPPTrailer();
411 * 'cat_ppd()' - Copy a PPD file to stdout.
414 static int /* O - Exit code */
415 cat_ppd(const char *name
, /* I - PPD name */
416 int request_id
) /* I - Request ID for response? */
418 char scheme
[256], /* Scheme from PPD name */
419 *sptr
, /* Pointer into scheme */
420 line
[1024], /* Line/filename */
421 message
[2048]; /* status-message */
425 * Figure out if this is a static or dynamic PPD file...
428 strlcpy(scheme
, name
, sizeof(scheme
));
429 if ((sptr
= strchr(scheme
, ':')) != NULL
)
433 if (!strcmp(scheme
, "file"))
436 * "file:name" == "name"...
447 puts("Content-Type: application/ipp\n");
450 return (cat_static(name
, request_id
));
451 else if (!strcmp(scheme
, "drv"))
452 return (cat_drv(name
, request_id
));
456 * Dynamic PPD, see if we have a driver program to support it...
459 const char *serverbin
; /* CUPS_SERVERBIN env var */
460 char *argv
[4]; /* Arguments for program */
463 if ((serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
464 serverbin
= CUPS_SERVERBIN
;
466 snprintf(line
, sizeof(line
), "%s/driver/%s", serverbin
, scheme
);
467 if (access(line
, X_OK
))
470 * File does not exist or is not executable...
473 fprintf(stderr
, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n",
474 line
, strerror(errno
));
478 snprintf(message
, sizeof(message
), "Unable to access \"%s\" - %s",
479 line
, strerror(errno
));
481 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
482 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
483 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
484 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
486 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
487 cupsdSendIPPTrailer();
494 * Yes, let it cat the PPD file...
499 cupsdSendIPPHeader(IPP_OK
, request_id
);
500 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
501 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
502 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
504 cupsdSendIPPTrailer();
508 argv
[1] = (char *)"cat";
509 argv
[2] = (char *)name
;
512 if (cupsdExec(line
, argv
))
515 * Unable to execute driver...
518 fprintf(stderr
, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n",
519 line
, strerror(errno
));
525 * Return with no errors...
533 * 'copy_static()' - Copy a static PPD file to stdout.
536 static int /* O - Exit code */
537 cat_static(const char *name
, /* I - PPD name */
538 int request_id
) /* I - Request ID for response? */
540 cups_file_t
*fp
; /* PPD file */
541 const char *datadir
; /* CUPS_DATADIR env var */
542 char line
[1024], /* Line/filename */
543 message
[2048]; /* status-message */
546 if (name
[0] == '/' || strstr(name
, "../") || strstr(name
, "/.."))
552 fprintf(stderr
, "ERROR: [cups-driverd] Bad PPD name \"%s\"!\n", name
);
556 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\"!", name
);
558 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
559 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
560 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
561 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
563 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
564 cupsdSendIPPTrailer();
571 * Try opening the file...
575 if (!strncmp(name
, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
576 !strncmp(name
, "Library/Printers/PPDs/Contents/Resources/", 41))
579 * Map ppd-name to Mac OS X standard locations...
582 snprintf(line
, sizeof(line
), "/%s", name
);
586 #elif defined(__linux)
587 if (!strncmp(name
, "lsb/usr/", 8))
590 * Map ppd-name to LSB standard /usr/share/ppd location...
593 snprintf(line
, sizeof(line
), "/usr/share/ppd/%s", name
+ 8);
595 else if (!strncmp(name
, "lsb/opt/", 8))
598 * Map ppd-name to LSB standard /opt/share/ppd location...
601 snprintf(line
, sizeof(line
), "/opt/share/ppd/%s", name
+ 8);
603 else if (!strncmp(name
, "lsb/local/", 10))
606 * Map ppd-name to LSB standard /usr/local/share/ppd location...
609 snprintf(line
, sizeof(line
), "/usr/local/share/ppd/%s", name
+ 10);
613 #endif /* __APPLE__ */
615 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
616 datadir
= CUPS_DATADIR
;
618 snprintf(line
, sizeof(line
), "%s/model/%s", datadir
, name
);
621 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
623 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n",
624 line
, strerror(errno
));
628 snprintf(message
, sizeof(message
), "Unable to open \"%s\" - %s",
629 line
, strerror(errno
));
631 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
632 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
633 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
634 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
636 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
637 cupsdSendIPPTrailer();
645 cupsdSendIPPHeader(IPP_OK
, request_id
);
646 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
647 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
648 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
650 cupsdSendIPPTrailer();
654 * Now copy the file to stdout...
657 while (cupsFileGets(fp
, line
, sizeof(line
)))
667 * 'compare_inodes()' - Compare two inodes.
670 static int /* O - Result of comparison */
671 compare_inodes(struct stat
*a
, /* I - First inode */
672 struct stat
*b
) /* I - Second inode */
674 if (a
->st_dev
!= b
->st_dev
)
675 return (a
->st_dev
- b
->st_dev
);
677 return (a
->st_ino
- b
->st_ino
);
682 * 'compare_matches()' - Compare PPD match scores for sorting.
686 compare_matches(const ppd_info_t
*p0
, /* I - First PPD */
687 const ppd_info_t
*p1
) /* I - Second PPD */
689 if (p1
->matches
!= p0
->matches
)
690 return (p1
->matches
- p0
->matches
);
692 return (cupsdCompareNames(p0
->record
.make_and_model
,
693 p1
->record
.make_and_model
));
698 * 'compare_names()' - Compare PPD filenames for sorting.
701 static int /* O - Result of comparison */
702 compare_names(const ppd_info_t
*p0
, /* I - First PPD file */
703 const ppd_info_t
*p1
) /* I - Second PPD file */
705 int diff
; /* Difference between strings */
708 if ((diff
= strcmp(p0
->record
.filename
, p1
->record
.filename
)) != 0)
711 return (strcmp(p0
->record
.name
, p1
->record
.name
));
716 * 'compare_ppds()' - Compare PPD file make and model names for sorting.
719 static int /* O - Result of comparison */
720 compare_ppds(const ppd_info_t
*p0
, /* I - First PPD file */
721 const ppd_info_t
*p1
) /* I - Second PPD file */
723 int diff
; /* Difference between strings */
727 * First compare manufacturers...
730 if ((diff
= strcasecmp(p0
->record
.make
, p1
->record
.make
)) != 0)
732 else if ((diff
= cupsdCompareNames(p0
->record
.make_and_model
,
733 p1
->record
.make_and_model
)) != 0)
736 return (strcmp(p0
->record
.languages
[0], p1
->record
.languages
[0]));
741 * 'dump_ppds_dat()' - Dump the contents of the ppds.dat file.
744 static int /* O - Exit status */
747 char filename
[1024]; /* ppds.dat filename */
748 ppd_info_t
*ppd
; /* Current PPD */
752 * See if we a PPD database file...
755 load_ppds_dat(filename
, sizeof(filename
), 0);
757 puts("mtime,size,model_number,type,filename,name,languages0,products0,"
758 "psversions0,make,make_and_model,device_id,scheme");
759 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
761 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
762 printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
764 (int)ppd
->record
.mtime
, (long)ppd
->record
.size
,
765 ppd
->record
.model_number
, ppd
->record
.type
, ppd
->record
.filename
,
766 ppd
->record
.name
, ppd
->record
.languages
[0], ppd
->record
.products
[0],
767 ppd
->record
.psversions
[0], ppd
->record
.make
,
768 ppd
->record
.make_and_model
, ppd
->record
.device_id
,
776 * 'free_array()' - Free an array of strings.
780 free_array(cups_array_t
*a
) /* I - Array to free */
782 char *ptr
; /* Pointer to string */
785 for (ptr
= (char *)cupsArrayFirst(a
);
787 ptr
= (char *)cupsArrayNext(a
))
795 * 'list_ppds()' - List PPD files.
798 static int /* O - Exit code */
799 list_ppds(int request_id
, /* I - Request ID */
800 int limit
, /* I - Limit */
801 const char *opt
) /* I - Option argument */
803 int i
; /* Looping vars */
804 int count
; /* Number of PPDs to send */
805 ppd_info_t
*ppd
; /* Current PPD file */
806 cups_file_t
*fp
; /* ppds.dat file */
807 char filename
[1024], /* ppds.dat filename */
808 model
[1024]; /* Model directory */
809 const char *cups_datadir
; /* CUPS_DATADIR environment variable */
810 int num_options
; /* Number of options */
811 cups_option_t
*options
; /* Options */
812 cups_array_t
*requested
, /* requested-attributes values */
813 *include
, /* PPD schemes to include */
814 *exclude
; /* PPD schemes to exclude */
815 const char *device_id
, /* ppd-device-id option */
816 *language
, /* ppd-natural-language option */
817 *make
, /* ppd-make option */
818 *make_and_model
, /* ppd-make-and-model option */
819 *model_number_str
, /* ppd-model-number option */
820 *product
, /* ppd-product option */
821 *psversion
, /* ppd-psversion option */
822 *type_str
; /* ppd-type option */
823 int model_number
, /* ppd-model-number value */
824 type
, /* ppd-type value */
825 make_and_model_len
, /* Length of ppd-make-and-model */
826 product_len
, /* Length of ppd-product */
827 send_device_id
, /* Send ppd-device-id? */
828 send_make
, /* Send ppd-make? */
829 send_make_and_model
, /* Send ppd-make-and-model? */
830 send_model_number
, /* Send ppd-model-number? */
831 send_name
, /* Send ppd-name? */
832 send_natural_language
, /* Send ppd-natural-language? */
833 send_product
, /* Send ppd-product? */
834 send_psversion
, /* Send ppd-psversion? */
835 send_type
, /* Send ppd-type? */
836 sent_header
; /* Sent the IPP header? */
837 regex_t
*device_id_re
, /* Regular expression for matching device ID */
838 *make_and_model_re
; /* Regular expression for matching make and model */
839 regmatch_t re_matches
[6]; /* Regular expression matches */
840 cups_array_t
*matches
; /* Matching PPDs */
844 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
845 "opt=\"%s\"\n", request_id
, limit
, opt
);
848 * See if we a PPD database file...
851 load_ppds_dat(filename
, sizeof(filename
), 1);
854 * Load all PPDs in the specified directory and below...
857 if ((cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
858 cups_datadir
= CUPS_DATADIR
;
860 Inodes
= cupsArrayNew((cups_array_func_t
)compare_inodes
, NULL
);
862 snprintf(model
, sizeof(model
), "%s/model", cups_datadir
);
863 load_ppds(model
, "", 1);
865 snprintf(model
, sizeof(model
), "%s/drv", cups_datadir
);
866 load_ppds(model
, "", 1);
870 * Load PPDs from standard Mac OS X locations...
873 load_ppds("/Library/Printers/PPDs/Contents/Resources",
874 "Library/Printers/PPDs/Contents/Resources", 0);
875 load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
876 "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
877 load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
878 "System/Library/Printers/PPDs/Contents/Resources", 0);
879 load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
880 "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
882 #elif defined(__linux)
884 * Load PPDs from LSB-defined locations...
887 if (!access("/usr/local/share/ppd", 0))
888 load_ppds("/usr/local/share/ppd", "lsb/local", 1);
889 if (!access("/usr/share/ppd", 0))
890 load_ppds("/usr/share/ppd", "lsb/usr", 1);
891 if (!access("/opt/share/ppd", 0))
892 load_ppds("/opt/share/ppd", "lsb/opt", 1);
893 #endif /* __APPLE__ */
896 * Cull PPD files that are no longer present...
899 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
901 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
905 * Remove this PPD file from the list...
908 cupsArrayRemove(PPDsByName
, ppd
);
909 cupsArrayRemove(PPDsByMakeModel
, ppd
);
916 * Write the new ppds.dat file...
919 fprintf(stderr
, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD
);
923 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
925 unsigned ppdsync
= PPD_SYNC
; /* Sync word */
928 cupsFileWrite(fp
, (char *)&ppdsync
, sizeof(ppdsync
));
930 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
932 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
933 cupsFileWrite(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
));
937 fprintf(stderr
, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
938 filename
, cupsArrayCount(PPDsByName
));
941 fprintf(stderr
, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
942 filename
, strerror(errno
));
945 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr
);
948 * Scan for dynamic PPD files...
951 num_options
= cupsParseOptions(opt
, 0, &options
);
952 exclude
= cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
953 num_options
, options
));
954 include
= cupsdCreateStringsArray(cupsGetOption("include-schemes",
955 num_options
, options
));
957 load_drivers(include
, exclude
);
960 * Add the raw driver...
963 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
964 PPD_TYPE_UNKNOWN
, "raw");
967 * Send IPP attributes...
970 requested
= cupsdCreateStringsArray(
971 cupsGetOption("requested-attributes", num_options
,
973 device_id
= cupsGetOption("ppd-device-id", num_options
, options
);
974 language
= cupsGetOption("ppd-natural-language", num_options
, options
);
975 make
= cupsGetOption("ppd-make", num_options
, options
);
976 make_and_model
= cupsGetOption("ppd-make-and-model", num_options
, options
);
977 model_number_str
= cupsGetOption("ppd-model-number", num_options
, options
);
978 product
= cupsGetOption("ppd-product", num_options
, options
);
979 psversion
= cupsGetOption("ppd-psversion", num_options
, options
);
980 type_str
= cupsGetOption("ppd-type", num_options
, options
);
983 make_and_model_len
= strlen(make_and_model
);
985 make_and_model_len
= 0;
988 product_len
= strlen(product
);
992 if (model_number_str
)
993 model_number
= atoi(model_number_str
);
1000 type
< (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0]));
1002 if (!strcmp(type_str
, ppd_types
[type
]))
1005 if (type
>= (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0])))
1007 fprintf(stderr
, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
1015 for (i
= 0; i
< num_options
; i
++)
1016 fprintf(stderr
, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options
[i
].name
,
1019 if (!requested
|| cupsArrayFind(requested
, (void *)"all") != NULL
)
1023 send_make_and_model
= 1;
1024 send_model_number
= 1;
1025 send_natural_language
= 1;
1033 send_name
= cupsArrayFind(requested
,
1034 (void *)"ppd-name") != NULL
;
1035 send_make
= cupsArrayFind(requested
,
1036 (void *)"ppd-make") != NULL
;
1037 send_make_and_model
= cupsArrayFind(requested
,
1038 (void *)"ppd-make-and-model") != NULL
;
1039 send_model_number
= cupsArrayFind(requested
,
1040 (void *)"ppd-model-number") != NULL
;
1041 send_natural_language
= cupsArrayFind(requested
,
1042 (void *)"ppd-natural-language") != NULL
;
1043 send_device_id
= cupsArrayFind(requested
,
1044 (void *)"ppd-device-id") != NULL
;
1045 send_product
= cupsArrayFind(requested
,
1046 (void *)"ppd-product") != NULL
;
1047 send_psversion
= cupsArrayFind(requested
,
1048 (void *)"ppd-psversion") != NULL
;
1049 send_type
= cupsArrayFind(requested
,
1050 (void *)"ppd-type") != NULL
;
1054 * Send the content type header to the scheduler; request_id can only be
1055 * 0 when run manually since the scheduler enforces the IPP requirement for
1056 * a request ID from 1 to 2^31-1...
1060 puts("Content-Type: application/ipp\n");
1064 if (limit
<= 0 || limit
> cupsArrayCount(PPDsByMakeModel
))
1065 count
= cupsArrayCount(PPDsByMakeModel
);
1069 if (device_id
|| language
|| make
|| make_and_model
|| model_number_str
||
1072 matches
= cupsArrayNew((cups_array_func_t
)compare_matches
, NULL
);
1075 device_id_re
= regex_device_id(device_id
);
1077 device_id_re
= NULL
;
1080 make_and_model_re
= regex_string(make_and_model
);
1082 make_and_model_re
= NULL
;
1084 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByMakeModel
);
1086 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
1089 * Filter PPDs based on make, model, product, language, model number,
1090 * and/or device ID using the "matches" score value. An exact match
1091 * for product, make-and-model, or device-id adds 3 to the score.
1092 * Partial matches for make-and-model yield 1 or 2 points, and matches
1093 * for the make and language add a single point. Results are then sorted
1094 * by score, highest score first.
1097 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1098 ppd
->record
.type
>= PPD_TYPE_DRV
)
1101 if (cupsArrayFind(exclude
, ppd
->record
.scheme
) ||
1102 (include
&& !cupsArrayFind(include
, ppd
->record
.scheme
)))
1108 !regexec(device_id_re
, ppd
->record
.device_id
,
1109 (int)(sizeof(re_matches
) / sizeof(re_matches
[0])),
1113 * Add the number of matching values from the device ID - it will be
1114 * at least 2 (manufacturer and model), and as much as 3 (command set).
1117 for (i
= 1; i
< (int)(sizeof(re_matches
) / sizeof(re_matches
[0])); i
++)
1118 if (re_matches
[i
].rm_so
>= 0)
1124 for (i
= 0; i
< PPD_MAX_LANG
; i
++)
1125 if (!ppd
->record
.languages
[i
][0])
1127 else if (!strcmp(ppd
->record
.languages
[i
], language
))
1134 if (make
&& !strcasecmp(ppd
->record
.make
, make
))
1137 if (make_and_model_re
&&
1138 !regexec(make_and_model_re
, ppd
->record
.make_and_model
,
1139 (int)(sizeof(re_matches
) / sizeof(re_matches
[0])),
1142 // See how much of the make-and-model string we matched...
1143 if (re_matches
[0].rm_so
== 0)
1145 if (re_matches
[0].rm_eo
== make_and_model_len
)
1146 ppd
->matches
+= 3; // Exact match
1148 ppd
->matches
+= 2; // Prefix match
1151 ppd
->matches
++; // Infix match
1154 if (model_number_str
&& ppd
->record
.model_number
== model_number
)
1159 for (i
= 0; i
< PPD_MAX_PROD
; i
++)
1160 if (!ppd
->record
.products
[i
][0])
1162 else if (!strcasecmp(ppd
->record
.products
[i
], product
))
1171 for (i
= 0; i
< PPD_MAX_VERS
; i
++)
1172 if (!ppd
->record
.psversions
[i
][0])
1174 else if (!strcasecmp(ppd
->record
.psversions
[i
], psversion
))
1181 if (type_str
&& ppd
->record
.type
== type
)
1186 fprintf(stderr
, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
1187 ppd
->record
.name
, ppd
->matches
);
1188 cupsArrayAdd(matches
, ppd
);
1192 else if (include
|| exclude
)
1194 matches
= cupsArrayNew((cups_array_func_t
)compare_ppds
, NULL
);
1196 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByMakeModel
);
1198 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
1201 * Filter PPDs based on the include/exclude lists.
1204 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1205 ppd
->record
.type
>= PPD_TYPE_DRV
)
1208 if (cupsArrayFind(exclude
, ppd
->record
.scheme
) ||
1209 (include
&& !cupsArrayFind(include
, ppd
->record
.scheme
)))
1212 cupsArrayAdd(matches
, ppd
);
1216 matches
= PPDsByMakeModel
;
1218 for (ppd
= (ppd_info_t
*)cupsArrayFirst(matches
);
1220 ppd
= (ppd_info_t
*)cupsArrayNext(matches
))
1223 * Skip invalid PPDs...
1226 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1227 ppd
->record
.type
>= PPD_TYPE_DRV
)
1238 cupsdSendIPPHeader(IPP_OK
, request_id
);
1239 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
1240 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
1241 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
1245 fprintf(stderr
, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
1246 ppd
->record
.name
, ppd
->record
.make_and_model
);
1250 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
1253 cupsdSendIPPString(IPP_TAG_NAME
, "ppd-name", ppd
->record
.name
);
1255 if (send_natural_language
)
1257 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "ppd-natural-language",
1258 ppd
->record
.languages
[0]);
1260 for (i
= 1; i
< PPD_MAX_LANG
&& ppd
->record
.languages
[i
][0]; i
++)
1261 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "", ppd
->record
.languages
[i
]);
1265 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make", ppd
->record
.make
);
1267 if (send_make_and_model
)
1268 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make-and-model",
1269 ppd
->record
.make_and_model
);
1272 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-device-id",
1273 ppd
->record
.device_id
);
1277 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-product",
1278 ppd
->record
.products
[0]);
1280 for (i
= 1; i
< PPD_MAX_PROD
&& ppd
->record
.products
[i
][0]; i
++)
1281 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.products
[i
]);
1286 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-psversion",
1287 ppd
->record
.psversions
[0]);
1289 for (i
= 1; i
< PPD_MAX_VERS
&& ppd
->record
.psversions
[i
][0]; i
++)
1290 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.psversions
[i
]);
1294 cupsdSendIPPString(IPP_TAG_KEYWORD
, "ppd-type",
1295 ppd_types
[ppd
->record
.type
]);
1297 if (send_model_number
)
1298 cupsdSendIPPInteger(IPP_TAG_INTEGER
, "ppd-model-number",
1299 ppd
->record
.model_number
);
1302 * If we have only requested the ppd-make attribute, then skip
1303 * the remaining PPDs with this make...
1306 if (cupsArrayFind(requested
, (void *)"ppd-make") &&
1307 cupsArrayCount(requested
) == 1)
1309 const char *this_make
; /* This ppd-make */
1312 for (this_make
= ppd
->record
.make
,
1313 ppd
= (ppd_info_t
*)cupsArrayNext(matches
);
1315 ppd
= (ppd_info_t
*)cupsArrayNext(matches
))
1316 if (strcasecmp(this_make
, ppd
->record
.make
))
1319 cupsArrayPrev(matches
);
1325 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
1326 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
1327 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
1328 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
1331 cupsdSendIPPTrailer();
1338 * 'load_ppds()' - Load PPD files recursively.
1341 static int /* O - 1 on success, 0 on failure */
1342 load_ppds(const char *d
, /* I - Actual directory */
1343 const char *p
, /* I - Virtual path in name */
1344 int descend
) /* I - Descend into directories? */
1346 struct stat dinfo
, /* Directory information */
1347 *dinfoptr
; /* Pointer to match */
1348 int i
; /* Looping var */
1349 cups_file_t
*fp
; /* Pointer to file */
1350 cups_dir_t
*dir
; /* Directory pointer */
1351 cups_dentry_t
*dent
; /* Directory entry */
1352 char filename
[1024], /* Name of PPD or directory */
1353 line
[256], /* Line from backend */
1354 *ptr
, /* Pointer into name */
1355 name
[128], /* Name of PPD file */
1356 lang_version
[64], /* PPD LanguageVersion */
1357 lang_encoding
[64], /* PPD LanguageEncoding */
1358 country
[64], /* Country code */
1359 manufacturer
[256], /* Manufacturer */
1360 make_model
[256], /* Make and Model */
1361 model_name
[256], /* ModelName */
1362 nick_name
[256], /* NickName */
1363 device_id
[256], /* 1284DeviceID */
1364 product
[256], /* Product */
1365 psversion
[256], /* PSVersion */
1366 temp
[512]; /* Temporary make and model */
1367 int model_number
, /* cupsModelNumber */
1368 type
; /* ppd-type */
1369 cups_array_t
*products
, /* Product array */
1370 *psversions
, /* PSVersion array */
1371 *cups_languages
; /* cupsLanguages array */
1372 ppd_info_t
*ppd
, /* New PPD file */
1373 key
; /* Search key */
1374 int new_ppd
; /* Is this a new PPD? */
1375 struct /* LanguageVersion translation table */
1377 const char *version
, /* LanguageVersion string */
1378 *language
; /* Language code */
1381 { "chinese", "zh" },
1385 { "english", "en" },
1386 { "finnish", "fi" },
1390 { "hungarian", "hu" },
1391 { "italian", "it" },
1392 { "japanese", "ja" },
1394 { "norwegian", "no" },
1396 { "portuguese", "pt" },
1397 { "russian", "ru" },
1398 { "simplified chinese", "zh_CN" },
1400 { "spanish", "es" },
1401 { "swedish", "sv" },
1402 { "traditional chinese", "zh_TW" },
1408 * See if we've loaded this directory before...
1411 if (stat(d
, &dinfo
))
1413 if (errno
!= ENOENT
)
1414 fprintf(stderr
, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d
,
1419 else if (cupsArrayFind(Inodes
, &dinfo
))
1421 fprintf(stderr
, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
1427 * Nope, add it to the Inodes array and continue...
1430 dinfoptr
= (struct stat
*)malloc(sizeof(struct stat
));
1431 memcpy(dinfoptr
, &dinfo
, sizeof(struct stat
));
1432 cupsArrayAdd(Inodes
, dinfoptr
);
1434 if ((dir
= cupsDirOpen(d
)) == NULL
)
1436 if (errno
!= ENOENT
)
1438 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1439 d
, strerror(errno
));
1444 fprintf(stderr
, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d
);
1446 while ((dent
= cupsDirRead(dir
)) != NULL
)
1449 * Skip files/directories starting with "."...
1452 if (dent
->filename
[0] == '.')
1456 * See if this is a file...
1459 snprintf(filename
, sizeof(filename
), "%s/%s", d
, dent
->filename
);
1462 snprintf(name
, sizeof(name
), "%s/%s", p
, dent
->filename
);
1464 strlcpy(name
, dent
->filename
, sizeof(name
));
1466 if (S_ISDIR(dent
->fileinfo
.st_mode
))
1469 * Do subdirectory...
1473 if (!load_ppds(filename
, name
, 1))
1483 * See if this file has been scanned before...
1486 strcpy(key
.record
.filename
, name
);
1487 strcpy(key
.record
.name
, name
);
1489 ppd
= (ppd_info_t
*)cupsArrayFind(PPDsByName
, &key
);
1492 ppd
->record
.size
== dent
->fileinfo
.st_size
&&
1493 ppd
->record
.mtime
== dent
->fileinfo
.st_mtime
)
1499 while ((ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
)) != NULL
&&
1500 !strcmp(ppd
->record
.filename
, name
));
1506 * No, file is new/changed, so re-scan it...
1509 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1513 * Now see if this is a PPD file...
1517 cupsFileGets(fp
, line
, sizeof(line
));
1519 if (strncmp(line
, "*PPD-Adobe:", 11))
1522 * Nope, treat it as a driver information file...
1525 load_drv(filename
, name
, fp
, dent
->fileinfo
.st_mtime
,
1526 dent
->fileinfo
.st_size
);
1531 * Now read until we get the NickName field...
1534 cups_languages
= cupsArrayNew(NULL
, NULL
);
1535 products
= cupsArrayNew(NULL
, NULL
);
1536 psversions
= cupsArrayNew(NULL
, NULL
);
1538 model_name
[0] = '\0';
1539 nick_name
[0] = '\0';
1540 manufacturer
[0] = '\0';
1541 device_id
[0] = '\0';
1542 lang_encoding
[0] = '\0';
1543 strcpy(lang_version
, "en");
1545 type
= PPD_TYPE_POSTSCRIPT
;
1547 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
1549 if (!strncmp(line
, "*Manufacturer:", 14))
1550 sscanf(line
, "%*[^\"]\"%255[^\"]", manufacturer
);
1551 else if (!strncmp(line
, "*ModelName:", 11))
1552 sscanf(line
, "%*[^\"]\"%127[^\"]", model_name
);
1553 else if (!strncmp(line
, "*LanguageEncoding:", 18))
1554 sscanf(line
, "%*[^:]:%63s", lang_encoding
);
1555 else if (!strncmp(line
, "*LanguageVersion:", 17))
1556 sscanf(line
, "%*[^:]:%63s", lang_version
);
1557 else if (!strncmp(line
, "*NickName:", 10))
1558 sscanf(line
, "%*[^\"]\"%255[^\"]", nick_name
);
1559 else if (!strncasecmp(line
, "*1284DeviceID:", 14))
1561 sscanf(line
, "%*[^\"]\"%255[^\"]", device_id
);
1563 // Make sure device ID ends with a semicolon...
1564 if (device_id
[0] && device_id
[strlen(device_id
) - 1] != ';')
1565 strlcat(device_id
, ";", sizeof(device_id
));
1567 else if (!strncmp(line
, "*Product:", 9))
1569 if (sscanf(line
, "%*[^\"]\"(%255[^\"]", product
) == 1)
1572 * Make sure the value ends with a right parenthesis - can't stop at
1573 * the first right paren since the product name may contain escaped
1577 ptr
= product
+ strlen(product
) - 1;
1578 if (ptr
> product
&& *ptr
== ')')
1581 * Yes, ends with a parenthesis, so remove it from the end and
1582 * add the product to the list...
1586 cupsArrayAdd(products
, strdup(product
));
1590 else if (!strncmp(line
, "*PSVersion:", 11))
1592 sscanf(line
, "%*[^\"]\"%255[^\"]", psversion
);
1593 cupsArrayAdd(psversions
, strdup(psversion
));
1595 else if (!strncmp(line
, "*cupsLanguages:", 15))
1597 char *start
; /* Start of language */
1600 for (start
= line
+ 15; *start
&& isspace(*start
& 255); start
++);
1602 if (*start
++ == '\"')
1606 for (ptr
= start
+ 1;
1607 *ptr
&& *ptr
!= '\"' && !isspace(*ptr
& 255);
1614 while (isspace(*ptr
& 255))
1618 cupsArrayAdd(cups_languages
, strdup(start
));
1623 else if (!strncmp(line
, "*cupsFax:", 9))
1625 for (ptr
= line
+ 9; isspace(*ptr
& 255); ptr
++);
1627 if (!strncasecmp(ptr
, "true", 4))
1628 type
= PPD_TYPE_FAX
;
1630 else if (!strncmp(line
, "*cupsFilter:", 12) && type
== PPD_TYPE_POSTSCRIPT
)
1632 if (strstr(line
+ 12, "application/vnd.cups-raster"))
1633 type
= PPD_TYPE_RASTER
;
1634 else if (strstr(line
+ 12, "application/vnd.cups-pdf"))
1635 type
= PPD_TYPE_PDF
;
1637 else if (!strncmp(line
, "*cupsModelNumber:", 17))
1638 sscanf(line
, "*cupsModelNumber:%d", &model_number
);
1639 else if (!strncmp(line
, "*OpenUI", 7))
1642 * Stop early if we have a NickName or ModelName attributes
1643 * before the first OpenUI...
1646 if ((model_name
[0] || nick_name
[0]) && cupsArrayCount(products
) > 0 &&
1647 cupsArrayCount(psversions
) > 0)
1659 * See if we got all of the required info...
1663 cupsCharsetToUTF8((cups_utf8_t
*)make_model
, nick_name
,
1664 sizeof(make_model
), _ppdGetEncoding(lang_encoding
));
1666 strcpy(make_model
, model_name
);
1668 while (isspace(make_model
[0] & 255))
1669 _cups_strcpy(make_model
, make_model
+ 1);
1671 if (!make_model
[0] || cupsArrayCount(products
) == 0 ||
1672 cupsArrayCount(psversions
) == 0)
1675 * We don't have all the info needed, so skip this file...
1679 fprintf(stderr
, "WARNING: Missing NickName and ModelName in %s!\n",
1682 if (cupsArrayCount(products
) == 0)
1683 fprintf(stderr
, "WARNING: Missing Product in %s!\n", filename
);
1685 if (cupsArrayCount(psversions
) == 0)
1686 fprintf(stderr
, "WARNING: Missing PSVersion in %s!\n", filename
);
1688 free_array(products
);
1689 free_array(psversions
);
1690 free_array(cups_languages
);
1696 cupsArrayAdd(products
, strdup(model_name
));
1699 * Normalize the make and model string...
1702 while (isspace(manufacturer
[0] & 255))
1703 _cups_strcpy(manufacturer
, manufacturer
+ 1);
1705 if (!strncasecmp(make_model
, manufacturer
, strlen(manufacturer
)))
1706 strlcpy(temp
, make_model
, sizeof(temp
));
1708 snprintf(temp
, sizeof(temp
), "%s %s", manufacturer
, make_model
);
1710 _ppdNormalizeMakeAndModel(temp
, make_model
, sizeof(make_model
));
1713 * See if we got a manufacturer...
1716 if (!manufacturer
[0] || !strcmp(manufacturer
, "ESP"))
1719 * Nope, copy the first part of the make and model then...
1722 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
1725 * Truncate at the first space, dash, or slash, or make the
1726 * manufacturer "Other"...
1729 for (ptr
= manufacturer
; *ptr
; ptr
++)
1730 if (*ptr
== ' ' || *ptr
== '-' || *ptr
== '/')
1733 if (*ptr
&& ptr
> manufacturer
)
1736 strcpy(manufacturer
, "Other");
1738 else if (!strncasecmp(manufacturer
, "LHAG", 4) ||
1739 !strncasecmp(manufacturer
, "linotype", 8))
1740 strcpy(manufacturer
, "LHAG");
1741 else if (!strncasecmp(manufacturer
, "Hewlett", 7))
1742 strcpy(manufacturer
, "HP");
1745 * Fix the lang_version as needed...
1748 if ((ptr
= strchr(lang_version
, '-')) != NULL
)
1750 else if ((ptr
= strchr(lang_version
, '_')) != NULL
)
1756 * Setup the country suffix...
1760 _cups_strcpy(country
+ 1, ptr
);
1765 * No country suffix...
1771 for (i
= 0; i
< (int)(sizeof(languages
) / sizeof(languages
[0])); i
++)
1772 if (!strcasecmp(languages
[i
].version
, lang_version
))
1775 if (i
< (int)(sizeof(languages
) / sizeof(languages
[0])))
1778 * Found a known language...
1781 snprintf(lang_version
, sizeof(lang_version
), "%s%s",
1782 languages
[i
].language
, country
);
1787 * Unknown language; use "xx"...
1790 strcpy(lang_version
, "xx");
1794 * Record the PPD file...
1802 * Add new PPD file...
1805 fprintf(stderr
, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name
);
1807 ppd
= add_ppd(name
, name
, lang_version
, manufacturer
, make_model
,
1808 device_id
, (char *)cupsArrayFirst(products
),
1809 (char *)cupsArrayFirst(psversions
),
1810 dent
->fileinfo
.st_mtime
, dent
->fileinfo
.st_size
,
1811 model_number
, type
, "file");
1822 * Update existing record...
1825 fprintf(stderr
, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name
);
1827 memset(ppd
, 0, sizeof(ppd_info_t
));
1830 ppd
->record
.mtime
= dent
->fileinfo
.st_mtime
;
1831 ppd
->record
.size
= dent
->fileinfo
.st_size
;
1832 ppd
->record
.model_number
= model_number
;
1833 ppd
->record
.type
= type
;
1835 strlcpy(ppd
->record
.name
, name
, sizeof(ppd
->record
.name
));
1836 strlcpy(ppd
->record
.make
, manufacturer
, sizeof(ppd
->record
.make
));
1837 strlcpy(ppd
->record
.make_and_model
, make_model
,
1838 sizeof(ppd
->record
.make_and_model
));
1839 strlcpy(ppd
->record
.languages
[0], lang_version
,
1840 sizeof(ppd
->record
.languages
[0]));
1841 strlcpy(ppd
->record
.products
[0], (char *)cupsArrayFirst(products
),
1842 sizeof(ppd
->record
.products
[0]));
1843 strlcpy(ppd
->record
.psversions
[0], (char *)cupsArrayFirst(psversions
),
1844 sizeof(ppd
->record
.psversions
[0]));
1845 strlcpy(ppd
->record
.device_id
, device_id
, sizeof(ppd
->record
.device_id
));
1849 * Add remaining products, versions, and languages...
1853 i
< PPD_MAX_PROD
&& (ptr
= (char *)cupsArrayNext(products
)) != NULL
;
1855 strlcpy(ppd
->record
.products
[i
], ptr
,
1856 sizeof(ppd
->record
.products
[0]));
1859 i
< PPD_MAX_VERS
&& (ptr
= (char *)cupsArrayNext(psversions
)) != NULL
;
1861 strlcpy(ppd
->record
.psversions
[i
], ptr
,
1862 sizeof(ppd
->record
.psversions
[0]));
1864 for (i
= 1, ptr
= (char *)cupsArrayFirst(cups_languages
);
1865 i
< PPD_MAX_LANG
&& ptr
;
1866 i
++, ptr
= (char *)cupsArrayNext(cups_languages
))
1867 strlcpy(ppd
->record
.languages
[i
], ptr
,
1868 sizeof(ppd
->record
.languages
[0]));
1871 * Free products, versions, and languages...
1874 free_array(cups_languages
);
1875 free_array(products
);
1876 free_array(psversions
);
1888 * 'load_drv()' - Load the PPDs from a driver information file.
1891 static int /* O - 1 on success, 0 on failure */
1892 load_drv(const char *filename
, /* I - Actual filename */
1893 const char *name
, /* I - Name to the rest of the world */
1894 cups_file_t
*fp
, /* I - File to read from */
1895 time_t mtime
, /* I - Mod time of driver info file */
1896 off_t size
) /* I - Size of driver info file */
1898 ppdcSource
*src
; // Driver information file
1899 ppdcDriver
*d
; // Current driver
1900 ppdcAttr
*device_id
, // 1284DeviceID attribute
1901 *product
, // Current product value
1902 *ps_version
, // PSVersion attribute
1903 *cups_fax
, // cupsFax attribute
1904 *nick_name
; // NickName attribute
1905 ppdcFilter
*filter
; // Current filter
1906 bool product_found
; // Found product?
1907 char uri
[1024], // Driver URI
1908 make_model
[1024]; // Make and model
1909 int type
; // Driver type
1913 * Load the driver info file...
1916 src
= new ppdcSource(filename
, fp
);
1918 if (src
->drivers
->count
== 0)
1921 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1928 * Add a dummy entry for the file...
1931 add_ppd(filename
, filename
, "", "", "", "", "", "", mtime
, size
, 0,
1932 PPD_TYPE_DRV
, "drv");
1936 * Then the drivers in the file...
1939 for (d
= (ppdcDriver
*)src
->drivers
->first();
1941 d
= (ppdcDriver
*)src
->drivers
->next())
1943 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "drv", "", "", 0,
1945 d
->file_name
? d
->file_name
->value
:
1946 d
->pc_file_name
->value
);
1948 device_id
= d
->find_attr("1284DeviceID", NULL
);
1949 ps_version
= d
->find_attr("PSVersion", NULL
);
1950 nick_name
= d
->find_attr("NickName", NULL
);
1953 strlcpy(make_model
, nick_name
->value
->value
, sizeof(make_model
));
1954 else if (strncasecmp(d
->model_name
->value
, d
->manufacturer
->value
,
1955 strlen(d
->manufacturer
->value
)))
1956 snprintf(make_model
, sizeof(make_model
), "%s %s, %s",
1957 d
->manufacturer
->value
, d
->model_name
->value
,
1960 snprintf(make_model
, sizeof(make_model
), "%s, %s", d
->model_name
->value
,
1963 if ((cups_fax
= d
->find_attr("cupsFax", NULL
)) != NULL
&&
1964 !strcasecmp(cups_fax
->value
->value
, "true"))
1965 type
= PPD_TYPE_FAX
;
1966 else if (d
->type
== PPDC_DRIVER_PS
)
1967 type
= PPD_TYPE_POSTSCRIPT
;
1968 else if (d
->type
!= PPDC_DRIVER_CUSTOM
)
1969 type
= PPD_TYPE_RASTER
;
1972 for (filter
= (ppdcFilter
*)d
->filters
->first(),
1973 type
= PPD_TYPE_POSTSCRIPT
;
1975 filter
= (ppdcFilter
*)d
->filters
->next())
1976 if (strcasecmp(filter
->mime_type
->value
, "application/vnd.cups-raster"))
1977 type
= PPD_TYPE_RASTER
;
1978 else if (strcasecmp(filter
->mime_type
->value
,
1979 "application/vnd.cups-pdf"))
1980 type
= PPD_TYPE_PDF
;
1983 for (product
= (ppdcAttr
*)d
->attrs
->first(), product_found
= false;
1985 product
= (ppdcAttr
*)d
->attrs
->next())
1986 if (!strcmp(product
->name
->value
, "Product"))
1988 product_found
= true;
1990 add_ppd(filename
, uri
, "en", d
->manufacturer
->value
, make_model
,
1991 device_id
? device_id
->value
->value
: "",
1992 product
->value
->value
,
1993 ps_version
? ps_version
->value
->value
: "(3010) 0",
1994 mtime
, size
, d
->model_number
, type
, "drv");
1998 add_ppd(filename
, uri
, "en", d
->manufacturer
->value
, make_model
,
1999 device_id
? device_id
->value
->value
: "",
2000 d
->model_name
->value
,
2001 ps_version
? ps_version
->value
->value
: "(3010) 0",
2002 mtime
, size
, d
->model_number
, type
, "drv");
2012 * 'load_drivers()' - Load driver-generated PPD files.
2015 static int /* O - 1 on success, 0 on failure */
2016 load_drivers(cups_array_t
*include
, /* I - Drivers to include */
2017 cups_array_t
*exclude
) /* I - Drivers to exclude */
2019 int i
; /* Looping var */
2020 char *start
, /* Start of value */
2021 *ptr
; /* Pointer into string */
2022 const char *server_bin
, /* CUPS_SERVERBIN env variable */
2023 *scheme
, /* Scheme for this driver */
2024 *scheme_end
; /* Pointer to end of scheme */
2025 char drivers
[1024]; /* Location of driver programs */
2026 int pid
; /* Process ID for driver program */
2027 cups_file_t
*fp
; /* Pipe to driver program */
2028 cups_dir_t
*dir
; /* Directory pointer */
2029 cups_dentry_t
*dent
; /* Directory entry */
2030 char *argv
[3], /* Arguments for command */
2031 filename
[1024], /* Name of driver */
2032 line
[2048], /* Line from driver */
2033 name
[512], /* ppd-name */
2034 make
[128], /* ppd-make */
2035 make_and_model
[128], /* ppd-make-and-model */
2036 device_id
[128], /* ppd-device-id */
2037 languages
[128], /* ppd-natural-language */
2038 product
[128], /* ppd-product */
2039 psversion
[128], /* ppd-psversion */
2040 type_str
[128]; /* ppd-type */
2041 int type
; /* PPD type */
2042 ppd_info_t
*ppd
; /* Newly added PPD */
2046 * Try opening the driver directory...
2049 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
2050 server_bin
= CUPS_SERVERBIN
;
2052 snprintf(drivers
, sizeof(drivers
), "%s/driver", server_bin
);
2054 if ((dir
= cupsDirOpen(drivers
)) == NULL
)
2056 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open driver directory "
2058 drivers
, strerror(errno
));
2063 * Loop through all of the device drivers...
2066 argv
[1] = (char *)"list";
2069 while ((dent
= cupsDirRead(dir
)) != NULL
)
2072 * Only look at executable files...
2075 if (!(dent
->fileinfo
.st_mode
& 0111) || !S_ISREG(dent
->fileinfo
.st_mode
))
2079 * Include/exclude specific drivers...
2085 * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
2088 for (scheme
= (char *)cupsArrayFirst(exclude
);
2090 scheme
= (char *)cupsArrayNext(exclude
))
2092 fprintf(stderr
, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
2093 dent
->filename
, scheme
);
2094 scheme_end
= scheme
+ strlen(scheme
) - 1;
2096 if ((scheme_end
> scheme
&& *scheme_end
== '*' &&
2097 !strncmp(scheme
, dent
->filename
, scheme_end
- scheme
)) ||
2098 !strcmp(scheme
, dent
->filename
))
2100 fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr
);
2112 * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
2115 for (scheme
= (char *)cupsArrayFirst(include
);
2117 scheme
= (char *)cupsArrayNext(include
))
2119 fprintf(stderr
, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
2120 dent
->filename
, scheme
);
2121 scheme_end
= scheme
+ strlen(scheme
) - 1;
2123 if ((scheme_end
> scheme
&& *scheme_end
== '*' &&
2124 !strncmp(scheme
, dent
->filename
, scheme_end
- scheme
)) ||
2125 !strcmp(scheme
, dent
->filename
))
2127 fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr
);
2136 scheme
= dent
->filename
;
2139 * Run the driver with no arguments and collect the output...
2142 argv
[0] = dent
->filename
;
2143 snprintf(filename
, sizeof(filename
), "%s/%s", drivers
, dent
->filename
);
2145 if ((fp
= cupsdPipeCommand(&pid
, filename
, argv
, 0)) != NULL
)
2147 while (cupsFileGets(fp
, line
, sizeof(line
)))
2150 * Each line is of the form:
2152 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
2153 * "ppd-device-id" "ppd-product" "ppd-psversion"
2156 device_id
[0] = '\0';
2158 psversion
[0] = '\0';
2159 strcpy(type_str
, "postscript");
2161 if (sscanf(line
, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
2162 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
2163 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
2164 "%*[ \t]\"%127[^\"]\"",
2165 name
, languages
, make
, make_and_model
,
2166 device_id
, product
, psversion
, type_str
) < 4)
2169 * Bad format; strip trailing newline and write an error message.
2172 if (line
[strlen(line
) - 1] == '\n')
2173 line
[strlen(line
) - 1] = '\0';
2175 fprintf(stderr
, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
2176 dent
->filename
, line
);
2182 * Add the device to the array of available devices...
2185 if ((start
= strchr(languages
, ',')) != NULL
)
2189 type
< (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0]));
2191 if (!strcmp(type_str
, ppd_types
[type
]))
2194 if (type
>= (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0])))
2197 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
2199 type
= PPD_TYPE_UNKNOWN
;
2202 ppd
= add_ppd(filename
, name
, languages
, make
, make_and_model
,
2203 device_id
, product
, psversion
, 0, 0, 0, type
, scheme
);
2212 if (start
&& *start
)
2214 for (i
= 1; i
< PPD_MAX_LANG
&& *start
; i
++)
2216 if ((ptr
= strchr(start
, ',')) != NULL
)
2219 ptr
= start
+ strlen(start
);
2221 strlcpy(ppd
->record
.languages
[i
], start
,
2222 sizeof(ppd
->record
.languages
[0]));
2228 fprintf(stderr
, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
2236 fprintf(stderr
, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
2237 filename
, strerror(errno
));
2247 * 'load_ppds_dat()' - Load the ppds.dat file.
2251 load_ppds_dat(char *filename
, /* I - Filename buffer */
2252 size_t filesize
, /* I - Size of filename buffer */
2253 int verbose
) /* I - Be verbose? */
2255 ppd_info_t
*ppd
; /* Current PPD file */
2256 cups_file_t
*fp
; /* ppds.dat file */
2257 struct stat fileinfo
; /* ppds.dat information */
2258 const char *cups_cachedir
; /* CUPS_CACHEDIR environment variable */
2261 PPDsByName
= cupsArrayNew((cups_array_func_t
)compare_names
, NULL
);
2262 PPDsByMakeModel
= cupsArrayNew((cups_array_func_t
)compare_ppds
, NULL
);
2265 if ((cups_cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
2266 cups_cachedir
= CUPS_CACHEDIR
;
2268 snprintf(filename
, filesize
, "%s/ppds.dat", cups_cachedir
);
2269 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
2272 * See if we have the right sync word...
2275 unsigned ppdsync
; /* Sync word */
2276 int num_ppds
; /* Number of PPDs */
2279 if (cupsFileRead(fp
, (char *)&ppdsync
, sizeof(ppdsync
))
2280 == sizeof(ppdsync
) &&
2281 ppdsync
== PPD_SYNC
&&
2282 !stat(filename
, &fileinfo
) &&
2283 ((fileinfo
.st_size
- sizeof(ppdsync
)) % sizeof(ppd_rec_t
)) == 0 &&
2284 (num_ppds
= (fileinfo
.st_size
- sizeof(ppdsync
)) /
2285 sizeof(ppd_rec_t
)) > 0)
2288 * We have a ppds.dat file, so read it!
2291 for (; num_ppds
> 0; num_ppds
--)
2293 if ((ppd
= (ppd_info_t
*)calloc(1, sizeof(ppd_info_t
))) == NULL
)
2296 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
2301 if (cupsFileRead(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
)) > 0)
2303 cupsArrayAdd(PPDsByName
, ppd
);
2304 cupsArrayAdd(PPDsByMakeModel
, ppd
);
2314 fprintf(stderr
, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
2315 filename
, cupsArrayCount(PPDsByName
));
2324 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2328 static regex_t
* /* O - Regular expression */
2329 regex_device_id(const char *device_id
) /* I - IEEE-1284 device ID */
2331 char res
[2048], /* Regular expression string */
2332 *ptr
; /* Pointer into string */
2333 regex_t
*re
; /* Regular expression */
2334 int cmd
; /* Command set string? */
2337 fprintf(stderr
, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id
);
2340 * Scan the device ID string and insert class, command set, manufacturer, and
2341 * model attributes to match. We assume that the device ID in the PPD and the
2342 * device ID reported by the device itself use the same attribute names and
2343 * order of attributes.
2348 while (*device_id
&& ptr
< (res
+ sizeof(res
) - 6))
2350 cmd
= !strncasecmp(device_id
, "COMMAND SET:", 12) ||
2351 !strncasecmp(device_id
, "CMD:", 4);
2353 if (cmd
|| !strncasecmp(device_id
, "MANUFACTURER:", 13) ||
2354 !strncasecmp(device_id
, "MFG:", 4) ||
2355 !strncasecmp(device_id
, "MFR:", 4) ||
2356 !strncasecmp(device_id
, "MODEL:", 6) ||
2357 !strncasecmp(device_id
, "MDL:", 4))
2367 while (*device_id
&& *device_id
!= ';' && ptr
< (res
+ sizeof(res
) - 4))
2369 if (strchr("[]{}().*\\|", *device_id
))
2371 *ptr
++ = *device_id
++;
2374 if (*device_id
== ';' || !*device_id
)
2380 else if ((device_id
= strchr(device_id
, ';')) == NULL
)
2388 fprintf(stderr
, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res
);
2391 * Compile the regular expression and return...
2394 if (res
[0] && (re
= (regex_t
*)calloc(1, sizeof(regex_t
))) != NULL
)
2396 if (!regcomp(re
, res
, REG_EXTENDED
| REG_ICASE
))
2398 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr
);
2410 * 'regex_string()' - Construct a regular expression to compare a simple string.
2413 static regex_t
* /* O - Regular expression */
2414 regex_string(const char *s
) /* I - String to compare */
2416 char res
[2048], /* Regular expression string */
2417 *ptr
; /* Pointer into string */
2418 regex_t
*re
; /* Regular expression */
2421 fprintf(stderr
, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s
);
2424 * Convert the string to a regular expression, escaping special characters
2430 while (*s
&& ptr
< (res
+ sizeof(res
) - 2))
2432 if (strchr("[]{}().*\\", *s
))
2440 fprintf(stderr
, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res
);
2443 * Create a case-insensitive regular expression...
2446 if (res
[0] && (re
= (regex_t
*)calloc(1, sizeof(regex_t
))) != NULL
)
2448 if (!regcomp(re
, res
, REG_ICASE
))
2450 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr
);