2 * PPD/driver support for CUPS.
4 * This program handles listing and installing static PPD files, PPD files
5 * created from driver information files, and dynamically generated PPD files
6 * using driver helper programs.
8 * Copyright 2007-2018 by Apple Inc.
9 * Copyright 1997-2007 by Easy Software Products.
11 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
15 * Include necessary headers...
20 #include <cups/transcode.h>
21 #include <cups/ppd-private.h>
22 #include <ppdc/ppdc.h>
30 #define PPD_SYNC 0x50504441 /* Sync word for ppds.dat (PPDA) */
31 #define PPD_MAX_LANG 32 /* Maximum languages */
32 #define PPD_MAX_PROD 32 /* Maximum products */
33 #define PPD_MAX_VERS 32 /* Maximum versions */
35 #define PPD_TYPE_POSTSCRIPT 0 /* PostScript PPD */
36 #define PPD_TYPE_PDF 1 /* PDF PPD */
37 #define PPD_TYPE_RASTER 2 /* CUPS raster PPD */
38 #define PPD_TYPE_FAX 3 /* Facsimile/MFD PPD */
39 #define PPD_TYPE_UNKNOWN 4 /* Other/hybrid PPD */
40 #define PPD_TYPE_DRV 5 /* Driver info file */
41 #define PPD_TYPE_ARCHIVE 6 /* Archive file */
43 #define TAR_BLOCK 512 /* Number of bytes in a block */
44 #define TAR_BLOCKS 10 /* Blocking factor */
46 #define TAR_MAGIC "ustar" /* 5 chars and a null */
47 #define TAR_VERSION "00" /* POSIX tar version */
49 #define TAR_OLDNORMAL '\0' /* Normal disk file, Unix compat */
50 #define TAR_NORMAL '0' /* Normal disk file */
51 #define TAR_LINK '1' /* Link to previously dumped file */
52 #define TAR_SYMLINK '2' /* Symbolic link */
53 #define TAR_CHR '3' /* Character special file */
54 #define TAR_BLK '4' /* Block special file */
55 #define TAR_DIR '5' /* Directory */
56 #define TAR_FIFO '6' /* FIFO special file */
57 #define TAR_CONTIG '7' /* Contiguous file */
61 * PPD information structures...
64 typedef struct /**** PPD record ****/
66 time_t mtime
; /* Modification time */
67 off_t size
; /* Size in bytes */
68 int model_number
; /* cupsModelNumber */
69 int type
; /* ppd-type */
70 char filename
[512], /* Filename */
71 name
[256], /* PPD name */
72 languages
[PPD_MAX_LANG
][6],
73 /* LanguageVersion/cupsLanguages */
74 products
[PPD_MAX_PROD
][128],
76 psversions
[PPD_MAX_VERS
][32],
77 /* PSVersion strings */
78 make
[128], /* Manufacturer */
79 make_and_model
[128], /* NickName/ModelName */
80 device_id
[256], /* IEEE 1284 Device ID */
81 scheme
[128]; /* PPD scheme */
84 typedef struct /**** In-memory record ****/
86 int found
; /* 1 if PPD is found */
87 int matches
; /* Match count */
88 ppd_rec_t record
; /* PPDs.dat record */
91 typedef union /**** TAR record format ****/
93 unsigned char all
[TAR_BLOCK
]; /* Raw data block */
96 char pathname
[100], /* Destination path */
97 mode
[8], /* Octal file permissions */
98 uid
[8], /* Octal user ID */
99 gid
[8], /* Octal group ID */
100 size
[12], /* Octal size in bytes */
101 mtime
[12], /* Octal modification time */
102 chksum
[8], /* Octal checksum value */
103 linkflag
, /* File type */
104 linkname
[100], /* Source path for link */
105 magic
[6], /* Magic string */
106 version
[2], /* Format version */
107 uname
[32], /* User name */
108 gname
[32], /* Group name */
109 devmajor
[8], /* Octal device major number */
110 devminor
[8], /* Octal device minor number */
111 prefix
[155]; /* Prefix for long filenames */
120 static cups_array_t
*Inodes
= NULL
, /* Inodes of directories we've visited */
122 /* PPD files sorted by filename and name */
123 *PPDsByMakeModel
= NULL
;
124 /* PPD files sorted by make and model */
125 static int ChangedPPD
; /* Did we change the PPD database? */
126 static const char * const PPDTypes
[] = /* ppd-type values */
145 static ppd_info_t
*add_ppd(const char *filename
, const char *name
,
146 const char *language
, const char *make
,
147 const char *make_and_model
,
148 const char *device_id
, const char *product
,
149 const char *psversion
, time_t mtime
,
150 size_t size
, int model_number
, int type
,
152 static int cat_drv(const char *name
, int request_id
);
153 static void cat_ppd(const char *name
, int request_id
);
154 static int cat_static(const char *name
, int request_id
);
155 static int cat_tar(const char *name
, int request_id
);
156 static int compare_inodes(struct stat
*a
, struct stat
*b
);
157 static int compare_matches(const ppd_info_t
*p0
,
158 const ppd_info_t
*p1
);
159 static int compare_names(const ppd_info_t
*p0
,
160 const ppd_info_t
*p1
);
161 static int compare_ppds(const ppd_info_t
*p0
,
162 const ppd_info_t
*p1
);
163 static void dump_ppds_dat(const char *filename
);
164 static void free_array(cups_array_t
*a
);
165 static cups_file_t
*get_file(const char *name
, int request_id
,
166 const char *subdir
, char *buffer
,
167 size_t bufsize
, char **subfile
);
168 static void list_ppds(int request_id
, int limit
, const char *opt
);
169 static int load_drivers(cups_array_t
*include
,
170 cups_array_t
*exclude
);
171 static int load_drv(const char *filename
, const char *name
,
172 cups_file_t
*fp
, time_t mtime
, off_t size
);
173 static void load_ppd(const char *filename
, const char *name
,
174 const char *scheme
, struct stat
*fileinfo
,
175 ppd_info_t
*ppd
, cups_file_t
*fp
, off_t end
);
176 static int load_ppds(const char *d
, const char *p
, int descend
);
177 static void load_ppds_dat(char *filename
, size_t filesize
,
179 static int load_tar(const char *filename
, const char *name
,
180 cups_file_t
*fp
, time_t mtime
, off_t size
);
181 static int read_tar(cups_file_t
*fp
, char *name
, size_t namesize
,
183 static regex_t
*regex_device_id(const char *device_id
);
184 static regex_t
*regex_string(const char *s
);
188 * 'main()' - Scan for drivers and return an IPP response.
192 * cups-driverd request_id limit options
195 int /* O - Exit code */
196 main(int argc
, /* I - Number of command-line args */
197 char *argv
[]) /* I - Command-line arguments */
200 * Install or list PPDs...
203 if (argc
== 3 && !strcmp(argv
[1], "cat"))
205 else if ((argc
== 2 || argc
== 3) && !strcmp(argv
[1], "dump"))
206 dump_ppds_dat(argv
[2]);
207 else if (argc
== 4 && !strcmp(argv
[1], "get"))
208 cat_ppd(argv
[3], atoi(argv
[2]));
209 else if (argc
== 5 && !strcmp(argv
[1], "list"))
210 list_ppds(atoi(argv
[2]), atoi(argv
[3]), argv
[4]);
213 fputs("Usage: cups-driverd cat ppd-name\n", stderr
);
214 fputs("Usage: cups-driverd dump\n", stderr
);
215 fputs("Usage: cups-driverd get request_id ppd-name\n", stderr
);
216 fputs("Usage: cups-driverd list request_id limit options\n", stderr
);
223 * 'add_ppd()' - Add a PPD file.
226 static ppd_info_t
* /* O - PPD */
227 add_ppd(const char *filename
, /* I - PPD filename */
228 const char *name
, /* I - PPD name */
229 const char *language
, /* I - LanguageVersion */
230 const char *make
, /* I - Manufacturer */
231 const char *make_and_model
, /* I - NickName/ModelName */
232 const char *device_id
, /* I - 1284DeviceID */
233 const char *product
, /* I - Product */
234 const char *psversion
, /* I - PSVersion */
235 time_t mtime
, /* I - Modification time */
236 size_t size
, /* I - File size */
237 int model_number
, /* I - Model number */
238 int type
, /* I - Driver type */
239 const char *scheme
) /* I - PPD scheme */
241 ppd_info_t
*ppd
; /* PPD */
242 char *recommended
; /* Foomatic driver string */
246 * Add a new PPD file...
249 if ((ppd
= (ppd_info_t
*)calloc(1, sizeof(ppd_info_t
))) == NULL
)
252 "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n",
253 cupsArrayCount(PPDsByName
));
258 * Zero-out the PPD data and copy the values over...
262 ppd
->record
.mtime
= mtime
;
263 ppd
->record
.size
= (off_t
)size
;
264 ppd
->record
.model_number
= model_number
;
265 ppd
->record
.type
= type
;
267 strlcpy(ppd
->record
.filename
, filename
, sizeof(ppd
->record
.filename
));
268 strlcpy(ppd
->record
.name
, name
, sizeof(ppd
->record
.name
));
269 strlcpy(ppd
->record
.languages
[0], language
,
270 sizeof(ppd
->record
.languages
[0]));
271 strlcpy(ppd
->record
.products
[0], product
, sizeof(ppd
->record
.products
[0]));
272 strlcpy(ppd
->record
.psversions
[0], psversion
,
273 sizeof(ppd
->record
.psversions
[0]));
274 strlcpy(ppd
->record
.make
, make
, sizeof(ppd
->record
.make
));
275 strlcpy(ppd
->record
.make_and_model
, make_and_model
,
276 sizeof(ppd
->record
.make_and_model
));
277 strlcpy(ppd
->record
.device_id
, device_id
, sizeof(ppd
->record
.device_id
));
278 strlcpy(ppd
->record
.scheme
, scheme
, sizeof(ppd
->record
.scheme
));
281 * Strip confusing (and often wrong) "recommended" suffix added by
282 * Foomatic drivers...
285 if ((recommended
= strstr(ppd
->record
.make_and_model
,
286 " (recommended)")) != NULL
)
290 * Add the PPD to the PPD arrays...
293 cupsArrayAdd(PPDsByName
, ppd
);
294 cupsArrayAdd(PPDsByMakeModel
, ppd
);
297 * Return the new PPD pointer...
305 * 'cat_drv()' - Generate a PPD from a driver info file.
308 static int /* O - Exit code */
309 cat_drv(const char *name
, /* I - PPD name */
310 int request_id
) /* I - Request ID for response? */
312 cups_file_t
*fp
; // File pointer
313 ppdcSource
*src
; // PPD source file data
314 ppdcDriver
*d
; // Current driver
315 cups_file_t
*out
; // Stdout via CUPS file API
316 char message
[2048], // status-message
317 filename
[1024], // Full path to .drv file(s)
318 scheme
[32], // URI scheme ("drv")
319 userpass
[256], // User/password info (unused)
320 host
[2], // Hostname (unused)
321 resource
[1024], // Resource path (/dir/to/filename.drv)
322 *pc_file_name
; // Filename portion of URI
323 int port
; // Port number (unused)
326 // Pull out the path to the .drv file...
327 if (httpSeparateURI(HTTP_URI_CODING_ALL
, name
, scheme
, sizeof(scheme
),
328 userpass
, sizeof(userpass
), host
, sizeof(host
), &port
,
329 resource
, sizeof(resource
)) < HTTP_URI_OK
)
331 fprintf(stderr
, "ERROR: Bad PPD name \"%s\".\n", name
);
335 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\".", name
);
337 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
338 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
339 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
340 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
342 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
343 cupsdSendIPPTrailer();
349 if ((fp
= get_file(resource
, request_id
, "drv", filename
, sizeof(filename
),
350 &pc_file_name
)) == NULL
)
353 src
= new ppdcSource(filename
, fp
);
355 for (d
= (ppdcDriver
*)src
->drivers
->first();
357 d
= (ppdcDriver
*)src
->drivers
->next())
358 if (!strcmp(pc_file_name
, d
->pc_file_name
->value
) ||
359 (d
->file_name
&& !strcmp(pc_file_name
, d
->file_name
->value
)))
364 ppdcArray
*locales
; // Locale names
365 ppdcCatalog
*catalog
; // Message catalog in .drv file
368 fprintf(stderr
, "DEBUG2: [cups-driverd] %d locales defined in \"%s\"...\n",
369 src
->po_files
->count
, filename
);
371 locales
= new ppdcArray();
372 for (catalog
= (ppdcCatalog
*)src
->po_files
->first();
374 catalog
= (ppdcCatalog
*)src
->po_files
->next())
376 fprintf(stderr
, "DEBUG2: [cups-driverd] Adding locale \"%s\"...\n",
377 catalog
->locale
->value
);
378 catalog
->locale
->retain();
379 locales
->add(catalog
->locale
);
384 cupsdSendIPPHeader(IPP_OK
, request_id
);
385 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
386 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
387 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
389 cupsdSendIPPTrailer();
393 out
= cupsFileStdout();
394 d
->write_ppd_file(out
, NULL
, locales
, src
, PPDC_LFONLY
);
401 fprintf(stderr
, "ERROR: PPD \"%s\" not found.\n", name
);
405 snprintf(message
, sizeof(message
), "PPD \"%s\" not found.", name
);
407 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
408 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
409 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
410 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
412 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
413 cupsdSendIPPTrailer();
425 * 'cat_ppd()' - Copy a PPD file to stdout.
429 cat_ppd(const char *name
, /* I - PPD name */
430 int request_id
) /* I - Request ID for response? */
432 char scheme
[256], /* Scheme from PPD name */
433 *sptr
, /* Pointer into scheme */
434 line
[1024], /* Line/filename */
435 message
[2048]; /* status-message */
439 * Figure out if this is a static or dynamic PPD file...
442 if (strstr(name
, "../"))
444 fputs("ERROR: Invalid PPD name.\n", stderr
);
448 strlcpy(scheme
, name
, sizeof(scheme
));
449 if ((sptr
= strchr(scheme
, ':')) != NULL
)
453 if (!strcmp(scheme
, "file"))
456 * "file:name" == "name"...
464 if (!strstr(name
, ".tar/") && !strstr(name
, ".tar.gz/"))
472 puts("Content-Type: application/ipp\n");
475 exit(cat_static(name
, request_id
));
476 else if (!strcmp(scheme
, "drv"))
477 exit(cat_drv(name
, request_id
));
478 else if (!strcmp(scheme
, "file"))
479 exit(cat_tar(name
, request_id
));
483 * Dynamic PPD, see if we have a driver program to support it...
486 const char *serverbin
; /* CUPS_SERVERBIN env var */
487 char *argv
[4]; /* Arguments for program */
490 if ((serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
491 serverbin
= CUPS_SERVERBIN
;
493 snprintf(line
, sizeof(line
), "%s/driver/%s", serverbin
, scheme
);
494 if (access(line
, X_OK
))
497 * File does not exist or is not executable...
500 fprintf(stderr
, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n",
501 line
, strerror(errno
));
505 snprintf(message
, sizeof(message
), "Unable to access \"%s\" - %s",
506 line
, strerror(errno
));
508 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
509 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
510 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
511 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
513 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
514 cupsdSendIPPTrailer();
521 * Yes, let it cat the PPD file...
526 cupsdSendIPPHeader(IPP_OK
, request_id
);
527 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
528 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
529 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
531 cupsdSendIPPTrailer();
535 argv
[1] = (char *)"cat";
536 argv
[2] = (char *)name
;
539 if (cupsdExec(line
, argv
))
542 * Unable to execute driver...
545 fprintf(stderr
, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n",
546 line
, strerror(errno
));
552 * Exit with no errors...
560 * 'copy_static()' - Copy a static PPD file to stdout.
563 static int /* O - Exit code */
564 cat_static(const char *name
, /* I - PPD name */
565 int request_id
) /* I - Request ID for response? */
567 cups_file_t
*fp
; /* PPD file */
568 char filename
[1024], /* PPD filename */
569 line
[1024]; /* Line buffer */
572 if ((fp
= get_file(name
, request_id
, "model", filename
, sizeof(filename
),
578 cupsdSendIPPHeader(IPP_OK
, request_id
);
579 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
580 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
581 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
583 cupsdSendIPPTrailer();
587 * Now copy the file to stdout...
590 while (cupsFileGets(fp
, line
, sizeof(line
)))
600 * 'cat_tar()' - Copy an archived PPD file to stdout.
603 static int /* O - Exit code */
604 cat_tar(const char *name
, /* I - PPD name */
605 int request_id
) /* I - Request ID */
607 cups_file_t
*fp
; /* Archive file pointer */
608 char filename
[1024], /* Archive filename */
609 *ppdname
, /* PPD filename in archive */
610 curname
[256], /* Current name in archive */
611 buffer
[8192]; /* Copy buffer */
612 struct stat curinfo
; /* Current file info in archive */
613 off_t total
, /* Total bytes copied */
614 next
; /* Offset for next record in archive */
615 ssize_t bytes
; /* Bytes read */
619 * Open the archive file...
622 if ((fp
= get_file(name
, request_id
, "model", filename
, sizeof(filename
),
623 &ppdname
)) == NULL
|| !ppdname
)
627 * Scan the archive for the PPD...
630 while (read_tar(fp
, curname
, sizeof(curname
), &curinfo
))
632 next
= cupsFileTell(fp
) + ((curinfo
.st_size
+ TAR_BLOCK
- 1) &
635 if (!strcmp(ppdname
, curname
))
639 cupsdSendIPPHeader(IPP_OK
, request_id
);
640 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
641 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
642 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
644 cupsdSendIPPTrailer();
647 for (total
= 0; total
< curinfo
.st_size
; total
+= bytes
)
649 if ((size_t)(bytes
= (curinfo
.st_size
- total
)) > sizeof(buffer
))
650 bytes
= sizeof(buffer
);
652 if ((bytes
= cupsFileRead(fp
, buffer
, (size_t)bytes
)) < 0)
654 if (errno
== EINTR
|| errno
== EAGAIN
)
660 perror("ERROR: [cups-driverd] Read error");
664 else if (bytes
> 0 && fwrite(buffer
, (size_t)bytes
, 1, stdout
) != 1)
672 if (cupsFileTell(fp
) != next
)
673 cupsFileSeek(fp
, next
);
678 fprintf(stderr
, "ERROR: PPD \"%s\" not found.\n", name
);
682 snprintf(buffer
, sizeof(buffer
), "PPD \"%s\" not found.", name
);
684 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
685 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
686 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
687 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
689 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", buffer
);
690 cupsdSendIPPTrailer();
698 * 'compare_inodes()' - Compare two inodes.
701 static int /* O - Result of comparison */
702 compare_inodes(struct stat
*a
, /* I - First inode */
703 struct stat
*b
) /* I - Second inode */
705 if (a
->st_dev
!= b
->st_dev
)
706 return (a
->st_dev
- b
->st_dev
);
708 return (a
->st_ino
- b
->st_ino
);
713 * 'compare_matches()' - Compare PPD match scores for sorting.
717 compare_matches(const ppd_info_t
*p0
, /* I - First PPD */
718 const ppd_info_t
*p1
) /* I - Second PPD */
720 if (p1
->matches
!= p0
->matches
)
721 return (p1
->matches
- p0
->matches
);
723 return (cupsdCompareNames(p0
->record
.make_and_model
,
724 p1
->record
.make_and_model
));
729 * 'compare_names()' - Compare PPD filenames for sorting.
732 static int /* O - Result of comparison */
733 compare_names(const ppd_info_t
*p0
, /* I - First PPD file */
734 const ppd_info_t
*p1
) /* I - Second PPD file */
736 int diff
; /* Difference between strings */
739 if ((diff
= strcmp(p0
->record
.filename
, p1
->record
.filename
)) != 0)
742 return (strcmp(p0
->record
.name
, p1
->record
.name
));
747 * 'compare_ppds()' - Compare PPD file make and model names for sorting.
750 static int /* O - Result of comparison */
751 compare_ppds(const ppd_info_t
*p0
, /* I - First PPD file */
752 const ppd_info_t
*p1
) /* I - Second PPD file */
754 int diff
; /* Difference between strings */
758 * First compare manufacturers...
761 if ((diff
= _cups_strcasecmp(p0
->record
.make
, p1
->record
.make
)) != 0)
763 else if ((diff
= cupsdCompareNames(p0
->record
.make_and_model
,
764 p1
->record
.make_and_model
)) != 0)
766 else if ((diff
= strcmp(p0
->record
.languages
[0],
767 p1
->record
.languages
[0])) != 0)
770 return (compare_names(p0
, p1
));
775 * 'dump_ppds_dat()' - Dump the contents of the ppds.dat file.
779 dump_ppds_dat(const char *filename
) /* I - Filename */
781 char temp
[1024]; /* ppds.dat filename */
782 ppd_info_t
*ppd
; /* Current PPD */
786 * See if we a PPD database file...
790 strlcpy(temp
, filename
, sizeof(temp
));
794 load_ppds_dat(temp
, sizeof(temp
), 0);
796 puts("mtime,size,model_number,type,filename,name,languages0,products0,"
797 "psversions0,make,make_and_model,device_id,scheme");
798 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
800 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
801 printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
803 (int)ppd
->record
.mtime
, (long)ppd
->record
.size
,
804 ppd
->record
.model_number
, ppd
->record
.type
, ppd
->record
.filename
,
805 ppd
->record
.name
, ppd
->record
.languages
[0], ppd
->record
.products
[0],
806 ppd
->record
.psversions
[0], ppd
->record
.make
,
807 ppd
->record
.make_and_model
, ppd
->record
.device_id
,
815 * 'free_array()' - Free an array of strings.
819 free_array(cups_array_t
*a
) /* I - Array to free */
821 char *ptr
; /* Pointer to string */
824 for (ptr
= (char *)cupsArrayFirst(a
);
826 ptr
= (char *)cupsArrayNext(a
))
834 * 'get_file()' - Get the filename associated with a request.
837 static cups_file_t
* /* O - File pointer or NULL */
838 get_file(const char *name
, /* I - Name */
839 int request_id
, /* I - Request ID */
840 const char *subdir
, /* I - Subdirectory for file */
841 char *buffer
, /* I - Filename buffer */
842 size_t bufsize
, /* I - Size of filename buffer */
843 char **subfile
) /* O - Sub-filename */
845 cups_file_t
*fp
; /* File pointer */
846 const char *datadir
; /* CUPS_DATADIR env var */
847 char *bufptr
, /* Pointer into filename buffer */
848 message
[2048]; /* status-message */
850 const char *printerDriver
, /* Pointer to .printerDriver extension */
851 *slash
; /* Pointer to next slash */
852 #endif /* __APPLE__ */
861 if (strstr(name
, "../") || strstr(name
, "/.."))
867 fprintf(stderr
, "ERROR: [cups-driverd] Bad PPD name \"%s\".\n", name
);
871 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\".", name
);
873 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
874 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
875 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
876 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
878 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
879 cupsdSendIPPTrailer();
886 * Try opening the file...
890 if (!strncmp(name
, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
891 !strncmp(name
, "Library/Printers/PPDs/Contents/Resources/", 41) ||
892 (!strncmp(name
, "System/Library/Printers/", 24) &&
895 ".printerDriver/Contents/Resources/PPDs")) != NULL
&&
896 (slash
= strchr(name
+ 24, '/')) != NULL
&&
897 slash
> printerDriver
) ||
898 (!strncmp(name
, "Library/Printers/", 17) &&
901 ".printerDriver/Contents/Resources/PPDs")) != NULL
&&
902 (slash
= strchr(name
+ 17, '/')) != NULL
&&
903 slash
> printerDriver
))
906 * Map ppd-name to macOS standard locations...
909 snprintf(buffer
, bufsize
, "/%s", name
);
913 #elif defined(__linux)
914 if (!strncmp(name
, "lsb/usr/", 8))
917 * Map ppd-name to LSB standard /usr/share/ppd location...
920 snprintf(buffer
, bufsize
, "/usr/share/ppd/%s", name
+ 8);
922 else if (!strncmp(name
, "lsb/opt/", 8))
925 * Map ppd-name to LSB standard /opt/share/ppd location...
928 snprintf(buffer
, bufsize
, "/opt/share/ppd/%s", name
+ 8);
930 else if (!strncmp(name
, "lsb/local/", 10))
933 * Map ppd-name to LSB standard /usr/local/share/ppd location...
936 snprintf(buffer
, bufsize
, "/usr/local/share/ppd/%s", name
+ 10);
940 #endif /* __APPLE__ */
942 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
943 datadir
= CUPS_DATADIR
;
945 snprintf(buffer
, bufsize
, "%s/%s/%s", datadir
, subdir
, name
);
949 * Strip anything after ".drv/", ".drv.gz/", ".tar/", or ".tar.gz/"...
954 if ((bufptr
= strstr(buffer
, ".drv/")) != NULL
)
956 else if ((bufptr
= strstr(buffer
, ".drv.gz/")) != NULL
)
958 else if ((bufptr
= strstr(buffer
, ".tar/")) != NULL
)
960 else if ((bufptr
= strstr(buffer
, ".tar.gz/")) != NULL
)
971 * Try opening the file...
974 if ((fp
= cupsFileOpen(buffer
, "r")) == NULL
)
976 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n",
977 buffer
, strerror(errno
));
981 snprintf(message
, sizeof(message
), "Unable to open \"%s\" - %s",
982 buffer
, strerror(errno
));
984 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
985 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
986 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
987 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
989 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
990 cupsdSendIPPTrailer();
1001 * 'list_ppds()' - List PPD files.
1005 list_ppds(int request_id
, /* I - Request ID */
1006 int limit
, /* I - Limit */
1007 const char *opt
) /* I - Option argument */
1009 int i
; /* Looping vars */
1010 int count
; /* Number of PPDs to send */
1011 ppd_info_t
*ppd
; /* Current PPD file */
1012 cups_file_t
*fp
; /* ppds.dat file */
1013 char filename
[1024], /* ppds.dat filename */
1014 model
[1024]; /* Model directory */
1015 const char *cups_datadir
; /* CUPS_DATADIR environment variable */
1016 int num_options
; /* Number of options */
1017 cups_option_t
*options
; /* Options */
1018 cups_array_t
*requested
, /* requested-attributes values */
1019 *include
, /* PPD schemes to include */
1020 *exclude
; /* PPD schemes to exclude */
1021 const char *device_id
, /* ppd-device-id option */
1022 *language
, /* ppd-natural-language option */
1023 *make
, /* ppd-make option */
1024 *make_and_model
, /* ppd-make-and-model option */
1025 *model_number_str
, /* ppd-model-number option */
1026 *product
, /* ppd-product option */
1027 *psversion
, /* ppd-psversion option */
1028 *type_str
; /* ppd-type option */
1029 int model_number
, /* ppd-model-number value */
1030 type
, /* ppd-type value */
1031 send_device_id
, /* Send ppd-device-id? */
1032 send_make
, /* Send ppd-make? */
1033 send_make_and_model
, /* Send ppd-make-and-model? */
1034 send_model_number
, /* Send ppd-model-number? */
1035 send_name
, /* Send ppd-name? */
1036 send_natural_language
, /* Send ppd-natural-language? */
1037 send_product
, /* Send ppd-product? */
1038 send_psversion
, /* Send ppd-psversion? */
1039 send_type
, /* Send ppd-type? */
1040 sent_header
; /* Sent the IPP header? */
1041 size_t make_and_model_len
, /* Length of ppd-make-and-model */
1042 product_len
; /* Length of ppd-product */
1043 regex_t
*device_id_re
, /* Regular expression for matching device ID */
1044 *make_and_model_re
; /* Regular expression for matching make and model */
1045 regmatch_t re_matches
[6]; /* Regular expression matches */
1046 cups_array_t
*matches
; /* Matching PPDs */
1050 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
1051 "opt=\"%s\"\n", request_id
, limit
, opt
);
1054 * See if we a PPD database file...
1058 load_ppds_dat(filename
, sizeof(filename
), 1);
1061 * Load all PPDs in the specified directory and below...
1064 if ((cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
1065 cups_datadir
= CUPS_DATADIR
;
1067 Inodes
= cupsArrayNew((cups_array_func_t
)compare_inodes
, NULL
);
1069 snprintf(model
, sizeof(model
), "%s/model", cups_datadir
);
1070 load_ppds(model
, "", 1);
1072 snprintf(model
, sizeof(model
), "%s/drv", cups_datadir
);
1073 load_ppds(model
, "", 1);
1077 * Load PPDs from standard macOS locations...
1080 load_ppds("/Library/Printers",
1081 "Library/Printers", 0);
1082 load_ppds("/Library/Printers/PPDs/Contents/Resources",
1083 "Library/Printers/PPDs/Contents/Resources", 0);
1084 load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
1085 "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
1086 load_ppds("/System/Library/Printers",
1087 "System/Library/Printers", 0);
1088 load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
1089 "System/Library/Printers/PPDs/Contents/Resources", 0);
1090 load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
1091 "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
1093 #elif defined(__linux)
1095 * Load PPDs from LSB-defined locations...
1098 if (!access("/usr/local/share/ppd", 0))
1099 load_ppds("/usr/local/share/ppd", "lsb/local", 1);
1100 if (!access("/usr/share/ppd", 0))
1101 load_ppds("/usr/share/ppd", "lsb/usr", 1);
1102 if (!access("/opt/share/ppd", 0))
1103 load_ppds("/opt/share/ppd", "lsb/opt", 1);
1104 #endif /* __APPLE__ */
1107 * Cull PPD files that are no longer present...
1110 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
1112 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
1116 * Remove this PPD file from the list...
1119 cupsArrayRemove(PPDsByName
, ppd
);
1120 cupsArrayRemove(PPDsByMakeModel
, ppd
);
1127 * Write the new ppds.dat file...
1130 fprintf(stderr
, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD
);
1134 char newname
[1024]; /* New filename */
1136 snprintf(newname
, sizeof(newname
), "%s.%d", filename
, (int)getpid());
1138 if ((fp
= cupsFileOpen(newname
, "w")) != NULL
)
1140 unsigned ppdsync
= PPD_SYNC
; /* Sync word */
1142 cupsFileWrite(fp
, (char *)&ppdsync
, sizeof(ppdsync
));
1144 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
1146 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
1147 cupsFileWrite(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
));
1151 if (rename(newname
, filename
))
1152 fprintf(stderr
, "ERROR: [cups-driverd] Unable to rename \"%s\" - %s\n",
1153 newname
, strerror(errno
));
1155 fprintf(stderr
, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
1156 filename
, cupsArrayCount(PPDsByName
));
1159 fprintf(stderr
, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
1160 filename
, strerror(errno
));
1163 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr
);
1166 * Scan for dynamic PPD files...
1169 num_options
= cupsParseOptions(opt
, 0, &options
);
1170 exclude
= cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
1171 num_options
, options
));
1172 include
= cupsdCreateStringsArray(cupsGetOption("include-schemes",
1173 num_options
, options
));
1175 load_drivers(include
, exclude
);
1178 * Add the raw driver...
1181 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
1182 PPD_TYPE_UNKNOWN
, "raw");
1185 * Send IPP attributes...
1188 requested
= cupsdCreateStringsArray(
1189 cupsGetOption("requested-attributes", num_options
,
1191 device_id
= cupsGetOption("ppd-device-id", num_options
, options
);
1192 language
= cupsGetOption("ppd-natural-language", num_options
, options
);
1193 make
= cupsGetOption("ppd-make", num_options
, options
);
1194 make_and_model
= cupsGetOption("ppd-make-and-model", num_options
, options
);
1195 model_number_str
= cupsGetOption("ppd-model-number", num_options
, options
);
1196 product
= cupsGetOption("ppd-product", num_options
, options
);
1197 psversion
= cupsGetOption("ppd-psversion", num_options
, options
);
1198 type_str
= cupsGetOption("ppd-type", num_options
, options
);
1201 make_and_model_len
= strlen(make_and_model
);
1203 make_and_model_len
= 0;
1206 product_len
= strlen(product
);
1210 if (model_number_str
)
1211 model_number
= atoi(model_number_str
);
1218 type
< (int)(sizeof(PPDTypes
) / sizeof(PPDTypes
[0]));
1220 if (!strcmp(type_str
, PPDTypes
[type
]))
1223 if (type
>= (int)(sizeof(PPDTypes
) / sizeof(PPDTypes
[0])))
1225 fprintf(stderr
, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
1233 for (i
= 0; i
< num_options
; i
++)
1234 fprintf(stderr
, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options
[i
].name
,
1237 if (!requested
|| cupsArrayFind(requested
, (void *)"all") != NULL
)
1241 send_make_and_model
= 1;
1242 send_model_number
= 1;
1243 send_natural_language
= 1;
1251 send_name
= cupsArrayFind(requested
,
1252 (void *)"ppd-name") != NULL
;
1253 send_make
= cupsArrayFind(requested
,
1254 (void *)"ppd-make") != NULL
;
1255 send_make_and_model
= cupsArrayFind(requested
,
1256 (void *)"ppd-make-and-model") != NULL
;
1257 send_model_number
= cupsArrayFind(requested
,
1258 (void *)"ppd-model-number") != NULL
;
1259 send_natural_language
= cupsArrayFind(requested
,
1260 (void *)"ppd-natural-language") != NULL
;
1261 send_device_id
= cupsArrayFind(requested
,
1262 (void *)"ppd-device-id") != NULL
;
1263 send_product
= cupsArrayFind(requested
,
1264 (void *)"ppd-product") != NULL
;
1265 send_psversion
= cupsArrayFind(requested
,
1266 (void *)"ppd-psversion") != NULL
;
1267 send_type
= cupsArrayFind(requested
,
1268 (void *)"ppd-type") != NULL
;
1272 * Send the content type header to the scheduler; request_id can only be
1273 * 0 when run manually since the scheduler enforces the IPP requirement for
1274 * a request ID from 1 to 2^31-1...
1278 puts("Content-Type: application/ipp\n");
1282 if (limit
<= 0 || limit
> cupsArrayCount(PPDsByMakeModel
))
1283 count
= cupsArrayCount(PPDsByMakeModel
);
1287 if (device_id
|| language
|| make
|| make_and_model
|| model_number_str
||
1290 matches
= cupsArrayNew((cups_array_func_t
)compare_matches
, NULL
);
1293 device_id_re
= regex_device_id(device_id
);
1295 device_id_re
= NULL
;
1298 make_and_model_re
= regex_string(make_and_model
);
1300 make_and_model_re
= NULL
;
1302 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByMakeModel
);
1304 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
1307 * Filter PPDs based on make, model, product, language, model number,
1308 * and/or device ID using the "matches" score value. An exact match
1309 * for product, make-and-model, or device-id adds 3 to the score.
1310 * Partial matches for make-and-model yield 1 or 2 points, and matches
1311 * for the make and language add a single point. Results are then sorted
1312 * by score, highest score first.
1315 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1316 ppd
->record
.type
>= PPD_TYPE_DRV
)
1319 if (cupsArrayFind(exclude
, ppd
->record
.scheme
) ||
1320 (include
&& !cupsArrayFind(include
, ppd
->record
.scheme
)))
1326 !regexec(device_id_re
, ppd
->record
.device_id
,
1327 (size_t)(sizeof(re_matches
) / sizeof(re_matches
[0])),
1331 * Add the number of matching values from the device ID - it will be
1332 * at least 2 (manufacturer and model), and as much as 3 (command set).
1335 for (i
= 1; i
< (int)(sizeof(re_matches
) / sizeof(re_matches
[0])); i
++)
1336 if (re_matches
[i
].rm_so
>= 0)
1342 for (i
= 0; i
< PPD_MAX_LANG
; i
++)
1343 if (!ppd
->record
.languages
[i
][0])
1345 else if (!strcmp(ppd
->record
.languages
[i
], language
))
1352 if (make
&& !_cups_strcasecmp(ppd
->record
.make
, make
))
1355 if (make_and_model_re
&&
1356 !regexec(make_and_model_re
, ppd
->record
.make_and_model
,
1357 (size_t)(sizeof(re_matches
) / sizeof(re_matches
[0])),
1360 // See how much of the make-and-model string we matched...
1361 if (re_matches
[0].rm_so
== 0)
1363 if ((size_t)re_matches
[0].rm_eo
== make_and_model_len
)
1364 ppd
->matches
+= 3; // Exact match
1366 ppd
->matches
+= 2; // Prefix match
1369 ppd
->matches
++; // Infix match
1372 if (model_number_str
&& ppd
->record
.model_number
== model_number
)
1377 for (i
= 0; i
< PPD_MAX_PROD
; i
++)
1378 if (!ppd
->record
.products
[i
][0])
1380 else if (!_cups_strcasecmp(ppd
->record
.products
[i
], product
))
1385 else if (!_cups_strncasecmp(ppd
->record
.products
[i
], product
,
1395 for (i
= 0; i
< PPD_MAX_VERS
; i
++)
1396 if (!ppd
->record
.psversions
[i
][0])
1398 else if (!_cups_strcasecmp(ppd
->record
.psversions
[i
], psversion
))
1405 if (type_str
&& ppd
->record
.type
== type
)
1410 fprintf(stderr
, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
1411 ppd
->record
.name
, ppd
->matches
);
1412 cupsArrayAdd(matches
, ppd
);
1416 else if (include
|| exclude
)
1418 matches
= cupsArrayNew((cups_array_func_t
)compare_ppds
, NULL
);
1420 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByMakeModel
);
1422 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
1425 * Filter PPDs based on the include/exclude lists.
1428 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1429 ppd
->record
.type
>= PPD_TYPE_DRV
)
1432 if (cupsArrayFind(exclude
, ppd
->record
.scheme
) ||
1433 (include
&& !cupsArrayFind(include
, ppd
->record
.scheme
)))
1436 cupsArrayAdd(matches
, ppd
);
1440 matches
= PPDsByMakeModel
;
1442 for (ppd
= (ppd_info_t
*)cupsArrayFirst(matches
);
1444 ppd
= (ppd_info_t
*)cupsArrayNext(matches
))
1447 * Skip invalid PPDs...
1450 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1451 ppd
->record
.type
>= PPD_TYPE_DRV
)
1464 cupsdSendIPPHeader(IPP_OK
, request_id
);
1465 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
1466 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
1467 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
1472 fprintf(stderr
, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
1473 ppd
->record
.name
, ppd
->record
.make_and_model
);
1479 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
1482 cupsdSendIPPString(IPP_TAG_NAME
, "ppd-name", ppd
->record
.name
);
1484 if (send_natural_language
)
1486 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "ppd-natural-language",
1487 ppd
->record
.languages
[0]);
1489 for (i
= 1; i
< PPD_MAX_LANG
&& ppd
->record
.languages
[i
][0]; i
++)
1490 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "", ppd
->record
.languages
[i
]);
1494 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make", ppd
->record
.make
);
1496 if (send_make_and_model
)
1497 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make-and-model",
1498 ppd
->record
.make_and_model
);
1501 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-device-id",
1502 ppd
->record
.device_id
);
1506 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-product",
1507 ppd
->record
.products
[0]);
1509 for (i
= 1; i
< PPD_MAX_PROD
&& ppd
->record
.products
[i
][0]; i
++)
1510 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.products
[i
]);
1515 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-psversion",
1516 ppd
->record
.psversions
[0]);
1518 for (i
= 1; i
< PPD_MAX_VERS
&& ppd
->record
.psversions
[i
][0]; i
++)
1519 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.psversions
[i
]);
1524 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
|| ppd
->record
.type
> PPD_TYPE_ARCHIVE
)
1527 * This cache file is corrupted, remove it!
1532 cupsdSendIPPString(IPP_TAG_KEYWORD
, "ppd-type", PPDTypes
[PPD_TYPE_UNKNOWN
]);
1535 cupsdSendIPPString(IPP_TAG_KEYWORD
, "ppd-type", PPDTypes
[ppd
->record
.type
]);
1538 if (send_model_number
)
1539 cupsdSendIPPInteger(IPP_TAG_INTEGER
, "ppd-model-number",
1540 ppd
->record
.model_number
);
1543 printf("%s (%s)\n", ppd
->record
.name
, ppd
->record
.make_and_model
);
1546 * If we have only requested the ppd-make attribute, then skip
1547 * the remaining PPDs with this make...
1550 if (cupsArrayFind(requested
, (void *)"ppd-make") &&
1551 cupsArrayCount(requested
) == 1)
1553 const char *this_make
; /* This ppd-make */
1556 for (this_make
= ppd
->record
.make
,
1557 ppd
= (ppd_info_t
*)cupsArrayNext(matches
);
1559 ppd
= (ppd_info_t
*)cupsArrayNext(matches
))
1560 if (_cups_strcasecmp(this_make
, ppd
->record
.make
))
1563 cupsArrayPrev(matches
);
1567 if (!sent_header
&& request_id
)
1569 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
1570 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
1571 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
1572 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
1576 cupsdSendIPPTrailer();
1583 * 'load_drv()' - Load the PPDs from a driver information file.
1586 static int /* O - 1 on success, 0 on failure */
1587 load_drv(const char *filename
, /* I - Actual filename */
1588 const char *name
, /* I - Name to the rest of the world */
1589 cups_file_t
*fp
, /* I - File to read from */
1590 time_t mtime
, /* I - Mod time of driver info file */
1591 off_t size
) /* I - Size of driver info file */
1593 ppdcSource
*src
; // Driver information file
1594 ppdcDriver
*d
; // Current driver
1595 ppdcAttr
*device_id
, // 1284DeviceID attribute
1596 *product
, // Current product value
1597 *ps_version
, // PSVersion attribute
1598 *cups_fax
, // cupsFax attribute
1599 *nick_name
; // NickName attribute
1600 ppdcFilter
*filter
; // Current filter
1601 ppd_info_t
*ppd
; // Current PPD
1602 int products_found
; // Number of products found
1603 char uri
[1024], // Driver URI
1604 make_model
[1024]; // Make and model
1605 int type
; // Driver type
1609 * Load the driver info file...
1612 src
= new ppdcSource(filename
, fp
);
1614 if (src
->drivers
->count
== 0)
1617 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1624 * Add a dummy entry for the file...
1627 add_ppd(name
, name
, "", "", "", "", "", "", mtime
, (size_t)size
, 0, PPD_TYPE_DRV
, "drv");
1631 * Then the drivers in the file...
1634 for (d
= (ppdcDriver
*)src
->drivers
->first();
1636 d
= (ppdcDriver
*)src
->drivers
->next())
1638 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "drv", "", "", 0,
1640 d
->file_name
? d
->file_name
->value
:
1641 d
->pc_file_name
->value
);
1643 device_id
= d
->find_attr("1284DeviceID", NULL
);
1644 ps_version
= d
->find_attr("PSVersion", NULL
);
1645 nick_name
= d
->find_attr("NickName", NULL
);
1648 strlcpy(make_model
, nick_name
->value
->value
, sizeof(make_model
));
1649 else if (_cups_strncasecmp(d
->model_name
->value
, d
->manufacturer
->value
,
1650 strlen(d
->manufacturer
->value
)))
1651 snprintf(make_model
, sizeof(make_model
), "%s %s, %s",
1652 d
->manufacturer
->value
, d
->model_name
->value
,
1655 snprintf(make_model
, sizeof(make_model
), "%s, %s", d
->model_name
->value
,
1658 if ((cups_fax
= d
->find_attr("cupsFax", NULL
)) != NULL
&&
1659 !_cups_strcasecmp(cups_fax
->value
->value
, "true"))
1660 type
= PPD_TYPE_FAX
;
1661 else if (d
->type
== PPDC_DRIVER_PS
)
1662 type
= PPD_TYPE_POSTSCRIPT
;
1663 else if (d
->type
!= PPDC_DRIVER_CUSTOM
)
1664 type
= PPD_TYPE_RASTER
;
1667 for (filter
= (ppdcFilter
*)d
->filters
->first(),
1668 type
= PPD_TYPE_POSTSCRIPT
;
1670 filter
= (ppdcFilter
*)d
->filters
->next())
1671 if (_cups_strcasecmp(filter
->mime_type
->value
, "application/vnd.cups-raster"))
1672 type
= PPD_TYPE_RASTER
;
1673 else if (_cups_strcasecmp(filter
->mime_type
->value
,
1674 "application/vnd.cups-pdf"))
1675 type
= PPD_TYPE_PDF
;
1678 for (product
= (ppdcAttr
*)d
->attrs
->first(), products_found
= 0,
1681 product
= (ppdcAttr
*)d
->attrs
->next())
1682 if (!strcmp(product
->name
->value
, "Product"))
1684 if (!products_found
)
1685 ppd
= add_ppd(name
, uri
, "en", d
->manufacturer
->value
, make_model
, device_id
? device_id
->value
->value
: "", product
->value
->value
,
1686 ps_version
? ps_version
->value
->value
: "(3010) 0", mtime
, (size_t)size
, d
->model_number
, type
, "drv");
1687 else if (products_found
< PPD_MAX_PROD
)
1688 strlcpy(ppd
->record
.products
[products_found
], product
->value
->value
, sizeof(ppd
->record
.products
[0]));
1695 if (!products_found
)
1696 add_ppd(name
, uri
, "en", d
->manufacturer
->value
, make_model
, device_id
? device_id
->value
->value
: "", d
->model_name
->value
, ps_version
? ps_version
->value
->value
: "(3010) 0", mtime
, (size_t)size
, d
->model_number
, type
, "drv");
1706 * 'load_drivers()' - Load driver-generated PPD files.
1709 static int /* O - 1 on success, 0 on failure */
1710 load_drivers(cups_array_t
*include
, /* I - Drivers to include */
1711 cups_array_t
*exclude
) /* I - Drivers to exclude */
1713 int i
; /* Looping var */
1714 char *start
, /* Start of value */
1715 *ptr
; /* Pointer into string */
1716 const char *server_bin
, /* CUPS_SERVERBIN env variable */
1717 *scheme
, /* Scheme for this driver */
1718 *scheme_end
; /* Pointer to end of scheme */
1719 char drivers
[1024]; /* Location of driver programs */
1720 int pid
; /* Process ID for driver program */
1721 cups_file_t
*fp
; /* Pipe to driver program */
1722 cups_dir_t
*dir
; /* Directory pointer */
1723 cups_dentry_t
*dent
; /* Directory entry */
1724 char *argv
[3], /* Arguments for command */
1725 filename
[1024], /* Name of driver */
1726 line
[2048], /* Line from driver */
1727 name
[256], /* ppd-name */
1728 make
[128], /* ppd-make */
1729 make_and_model
[128], /* ppd-make-and-model */
1730 device_id
[256], /* ppd-device-id */
1731 languages
[128], /* ppd-natural-language */
1732 product
[128], /* ppd-product */
1733 psversion
[128], /* ppd-psversion */
1734 type_str
[128]; /* ppd-type */
1735 int type
; /* PPD type */
1736 ppd_info_t
*ppd
; /* Newly added PPD */
1740 * Try opening the driver directory...
1743 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
1744 server_bin
= CUPS_SERVERBIN
;
1746 snprintf(drivers
, sizeof(drivers
), "%s/driver", server_bin
);
1748 if ((dir
= cupsDirOpen(drivers
)) == NULL
)
1750 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open driver directory "
1752 drivers
, strerror(errno
));
1757 * Loop through all of the device drivers...
1760 argv
[1] = (char *)"list";
1763 while ((dent
= cupsDirRead(dir
)) != NULL
)
1766 * Only look at executable files...
1769 if (!(dent
->fileinfo
.st_mode
& 0111) || !S_ISREG(dent
->fileinfo
.st_mode
))
1773 * Include/exclude specific drivers...
1779 * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
1782 for (scheme
= (char *)cupsArrayFirst(exclude
);
1784 scheme
= (char *)cupsArrayNext(exclude
))
1786 fprintf(stderr
, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
1787 dent
->filename
, scheme
);
1788 scheme_end
= scheme
+ strlen(scheme
) - 1;
1790 if ((scheme_end
> scheme
&& *scheme_end
== '*' &&
1791 !strncmp(scheme
, dent
->filename
, (size_t)(scheme_end
- scheme
))) ||
1792 !strcmp(scheme
, dent
->filename
))
1794 fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr
);
1806 * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
1809 for (scheme
= (char *)cupsArrayFirst(include
);
1811 scheme
= (char *)cupsArrayNext(include
))
1813 fprintf(stderr
, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
1814 dent
->filename
, scheme
);
1815 scheme_end
= scheme
+ strlen(scheme
) - 1;
1817 if ((scheme_end
> scheme
&& *scheme_end
== '*' &&
1818 !strncmp(scheme
, dent
->filename
, (size_t)(scheme_end
- scheme
))) ||
1819 !strcmp(scheme
, dent
->filename
))
1821 fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr
);
1830 scheme
= dent
->filename
;
1833 * Run the driver with no arguments and collect the output...
1836 snprintf(filename
, sizeof(filename
), "%s/%s", drivers
, dent
->filename
);
1838 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_PROGRAM
, !geteuid(),
1839 _cupsFileCheckFilter
, NULL
))
1842 argv
[0] = dent
->filename
;
1844 if ((fp
= cupsdPipeCommand(&pid
, filename
, argv
, 0)) != NULL
)
1846 while (cupsFileGets(fp
, line
, sizeof(line
)))
1849 * Each line is of the form:
1851 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
1852 * "ppd-device-id" "ppd-product" "ppd-psversion"
1855 device_id
[0] = '\0';
1857 psversion
[0] = '\0';
1858 strlcpy(type_str
, "postscript", sizeof(type_str
));
1860 if (sscanf(line
, "\"%255[^\"]\"%127s%*[ \t]\"%127[^\"]\""
1861 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%255[^\"]\""
1862 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1863 "%*[ \t]\"%127[^\"]\"",
1864 name
, languages
, make
, make_and_model
,
1865 device_id
, product
, psversion
, type_str
) < 4)
1868 * Bad format; strip trailing newline and write an error message.
1871 if (line
[strlen(line
) - 1] == '\n')
1872 line
[strlen(line
) - 1] = '\0';
1874 fprintf(stderr
, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1875 dent
->filename
, line
);
1881 * Add the device to the array of available devices...
1884 if ((start
= strchr(languages
, ',')) != NULL
)
1888 type
< (int)(sizeof(PPDTypes
) / sizeof(PPDTypes
[0]));
1890 if (!strcmp(type_str
, PPDTypes
[type
]))
1893 if (type
>= (int)(sizeof(PPDTypes
) / sizeof(PPDTypes
[0])))
1896 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
1898 type
= PPD_TYPE_UNKNOWN
;
1901 ppd
= add_ppd(filename
, name
, languages
, make
, make_and_model
,
1902 device_id
, product
, psversion
, 0, 0, 0, type
, scheme
);
1911 if (start
&& *start
)
1913 for (i
= 1; i
< PPD_MAX_LANG
&& *start
; i
++)
1915 if ((ptr
= strchr(start
, ',')) != NULL
)
1918 ptr
= start
+ strlen(start
);
1920 strlcpy(ppd
->record
.languages
[i
], start
,
1921 sizeof(ppd
->record
.languages
[0]));
1927 fprintf(stderr
, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1935 fprintf(stderr
, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1936 filename
, strerror(errno
));
1946 * 'load_ppd()' - Load a PPD file.
1950 load_ppd(const char *filename
, /* I - Real filename */
1951 const char *name
, /* I - Virtual filename */
1952 const char *scheme
, /* I - PPD scheme */
1953 struct stat
*fileinfo
, /* I - File information */
1954 ppd_info_t
*ppd
, /* I - Existing PPD file or NULL */
1955 cups_file_t
*fp
, /* I - File to read from */
1956 off_t end
) /* I - End of file position or 0 */
1958 int i
; /* Looping var */
1959 char line
[256], /* Line from file */
1960 *ptr
, /* Pointer into line */
1961 lang_version
[64], /* PPD LanguageVersion */
1962 lang_encoding
[64], /* PPD LanguageEncoding */
1963 country
[64], /* Country code */
1964 manufacturer
[256], /* Manufacturer */
1965 make_model
[256], /* Make and Model */
1966 model_name
[256], /* ModelName */
1967 nick_name
[256], /* NickName */
1968 device_id
[256], /* 1284DeviceID */
1969 product
[256], /* Product */
1970 psversion
[256], /* PSVersion */
1971 temp
[512]; /* Temporary make and model */
1972 int install_group
, /* In the installable options group? */
1973 model_number
, /* cupsModelNumber */
1974 type
; /* ppd-type */
1975 cups_array_t
*products
, /* Product array */
1976 *psversions
, /* PSVersion array */
1977 *cups_languages
; /* cupsLanguages array */
1978 int new_ppd
; /* Is this a new PPD? */
1979 struct /* LanguageVersion translation table */
1981 const char *version
, /* LanguageVersion string */
1982 *language
; /* Language code */
1985 { "chinese", "zh" },
1989 { "english", "en" },
1990 { "finnish", "fi" },
1994 { "hungarian", "hu" },
1995 { "italian", "it" },
1996 { "japanese", "ja" },
1998 { "norwegian", "no" },
2000 { "portuguese", "pt" },
2001 { "russian", "ru" },
2002 { "simplified chinese", "zh_CN" },
2004 { "spanish", "es" },
2005 { "swedish", "sv" },
2006 { "traditional chinese", "zh_TW" },
2012 * Now read until we get the required fields...
2015 cups_languages
= cupsArrayNew(NULL
, NULL
);
2016 products
= cupsArrayNew(NULL
, NULL
);
2017 psversions
= cupsArrayNew(NULL
, NULL
);
2019 model_name
[0] = '\0';
2020 nick_name
[0] = '\0';
2021 manufacturer
[0] = '\0';
2022 device_id
[0] = '\0';
2023 lang_encoding
[0] = '\0';
2024 strlcpy(lang_version
, "en", sizeof(lang_version
));
2027 type
= PPD_TYPE_POSTSCRIPT
;
2029 while ((end
== 0 || cupsFileTell(fp
) < end
) &&
2030 cupsFileGets(fp
, line
, sizeof(line
)))
2032 if (!strncmp(line
, "*Manufacturer:", 14))
2033 sscanf(line
, "%*[^\"]\"%255[^\"]", manufacturer
);
2034 else if (!strncmp(line
, "*ModelName:", 11))
2035 sscanf(line
, "%*[^\"]\"%127[^\"]", model_name
);
2036 else if (!strncmp(line
, "*LanguageEncoding:", 18))
2037 sscanf(line
, "%*[^:]:%63s", lang_encoding
);
2038 else if (!strncmp(line
, "*LanguageVersion:", 17))
2039 sscanf(line
, "%*[^:]:%63s", lang_version
);
2040 else if (!strncmp(line
, "*NickName:", 10))
2041 sscanf(line
, "%*[^\"]\"%255[^\"]", nick_name
);
2042 else if (!_cups_strncasecmp(line
, "*1284DeviceID:", 14))
2044 sscanf(line
, "%*[^\"]\"%255[^\"]", device_id
);
2046 // Make sure device ID ends with a semicolon...
2047 if (device_id
[0] && device_id
[strlen(device_id
) - 1] != ';')
2048 strlcat(device_id
, ";", sizeof(device_id
));
2050 else if (!strncmp(line
, "*Product:", 9))
2052 if (sscanf(line
, "%*[^\"]\"(%255[^\"]", product
) == 1)
2055 * Make sure the value ends with a right parenthesis - can't stop at
2056 * the first right paren since the product name may contain escaped
2060 ptr
= product
+ strlen(product
) - 1;
2061 if (ptr
> product
&& *ptr
== ')')
2064 * Yes, ends with a parenthesis, so remove it from the end and
2065 * add the product to the list...
2069 cupsArrayAdd(products
, strdup(product
));
2073 else if (!strncmp(line
, "*PSVersion:", 11))
2075 sscanf(line
, "%*[^\"]\"%255[^\"]", psversion
);
2076 cupsArrayAdd(psversions
, strdup(psversion
));
2078 else if (!strncmp(line
, "*cupsLanguages:", 15))
2080 char *start
; /* Start of language */
2083 for (start
= line
+ 15; *start
&& isspace(*start
& 255); start
++);
2085 if (*start
++ == '\"')
2089 for (ptr
= start
+ 1;
2090 *ptr
&& *ptr
!= '\"' && !isspace(*ptr
& 255);
2097 while (isspace(*ptr
& 255))
2101 cupsArrayAdd(cups_languages
, strdup(start
));
2106 else if (!strncmp(line
, "*cupsFax:", 9))
2108 for (ptr
= line
+ 9; isspace(*ptr
& 255); ptr
++);
2110 if (!_cups_strncasecmp(ptr
, "true", 4))
2111 type
= PPD_TYPE_FAX
;
2113 else if ((!strncmp(line
, "*cupsFilter:", 12) || !strncmp(line
, "*cupsFilter2:", 13)) && type
== PPD_TYPE_POSTSCRIPT
)
2115 if (strstr(line
+ 12, "application/vnd.cups-raster"))
2116 type
= PPD_TYPE_RASTER
;
2117 else if (strstr(line
+ 12, "application/vnd.cups-pdf"))
2118 type
= PPD_TYPE_PDF
;
2120 else if (!strncmp(line
, "*cupsModelNumber:", 17))
2121 sscanf(line
, "*cupsModelNumber:%d", &model_number
);
2122 else if (!strncmp(line
, "*OpenGroup: Installable", 23))
2124 else if (!strncmp(line
, "*CloseGroup:", 12))
2126 else if (!strncmp(line
, "*OpenUI", 7))
2129 * Stop early if we have a NickName or ModelName attributes
2130 * before the first non-installable OpenUI...
2133 if (!install_group
&& (model_name
[0] || nick_name
[0]) &&
2134 cupsArrayCount(products
) > 0 && cupsArrayCount(psversions
) > 0)
2140 * See if we got all of the required info...
2144 cupsCharsetToUTF8((cups_utf8_t
*)make_model
, nick_name
,
2145 sizeof(make_model
), _ppdGetEncoding(lang_encoding
));
2147 strlcpy(make_model
, model_name
, sizeof(make_model
));
2149 while (isspace(make_model
[0] & 255))
2150 _cups_strcpy(make_model
, make_model
+ 1);
2152 if (!make_model
[0] || cupsArrayCount(products
) == 0 ||
2153 cupsArrayCount(psversions
) == 0)
2156 * We don't have all the info needed, so skip this file...
2160 fprintf(stderr
, "WARNING: Missing NickName and ModelName in %s!\n",
2163 if (cupsArrayCount(products
) == 0)
2164 fprintf(stderr
, "WARNING: Missing Product in %s!\n", filename
);
2166 if (cupsArrayCount(psversions
) == 0)
2167 fprintf(stderr
, "WARNING: Missing PSVersion in %s!\n", filename
);
2169 free_array(products
);
2170 free_array(psversions
);
2171 free_array(cups_languages
);
2177 cupsArrayAdd(products
, strdup(model_name
));
2180 * Normalize the make and model string...
2183 while (isspace(manufacturer
[0] & 255))
2184 _cups_strcpy(manufacturer
, manufacturer
+ 1);
2186 if (!_cups_strncasecmp(make_model
, manufacturer
, strlen(manufacturer
)))
2187 strlcpy(temp
, make_model
, sizeof(temp
));
2189 snprintf(temp
, sizeof(temp
), "%s %s", manufacturer
, make_model
);
2191 _ppdNormalizeMakeAndModel(temp
, make_model
, sizeof(make_model
));
2194 * See if we got a manufacturer...
2197 if (!manufacturer
[0] || !strcmp(manufacturer
, "ESP"))
2200 * Nope, copy the first part of the make and model then...
2203 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
2206 * Truncate at the first space, dash, or slash, or make the
2207 * manufacturer "Other"...
2210 for (ptr
= manufacturer
; *ptr
; ptr
++)
2211 if (*ptr
== ' ' || *ptr
== '-' || *ptr
== '/')
2214 if (*ptr
&& ptr
> manufacturer
)
2217 strlcpy(manufacturer
, "Other", sizeof(manufacturer
));
2219 else if (!_cups_strncasecmp(manufacturer
, "LHAG", 4) ||
2220 !_cups_strncasecmp(manufacturer
, "linotype", 8))
2221 strlcpy(manufacturer
, "LHAG", sizeof(manufacturer
));
2222 else if (!_cups_strncasecmp(manufacturer
, "Hewlett", 7))
2223 strlcpy(manufacturer
, "HP", sizeof(manufacturer
));
2226 * Fix the lang_version as needed...
2229 if ((ptr
= strchr(lang_version
, '-')) != NULL
)
2231 else if ((ptr
= strchr(lang_version
, '_')) != NULL
)
2237 * Setup the country suffix...
2241 _cups_strcpy(country
+ 1, ptr
);
2246 * No country suffix...
2252 for (i
= 0; i
< (int)(sizeof(languages
) / sizeof(languages
[0])); i
++)
2253 if (!_cups_strcasecmp(languages
[i
].version
, lang_version
))
2256 if (i
< (int)(sizeof(languages
) / sizeof(languages
[0])))
2259 * Found a known language...
2262 snprintf(lang_version
, sizeof(lang_version
), "%s%s",
2263 languages
[i
].language
, country
);
2268 * Unknown language; use "xx"...
2271 strlcpy(lang_version
, "xx", sizeof(lang_version
));
2275 * Record the PPD file...
2283 * Add new PPD file...
2286 fprintf(stderr
, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name
);
2288 ppd
= add_ppd(name
, name
, lang_version
, manufacturer
, make_model
, device_id
, (char *)cupsArrayFirst(products
), (char *)cupsArrayFirst(psversions
), fileinfo
->st_mtime
, (size_t)fileinfo
->st_size
, model_number
, type
, scheme
);
2296 * Update existing record...
2299 fprintf(stderr
, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name
);
2301 memset(ppd
, 0, sizeof(ppd_info_t
));
2304 ppd
->record
.mtime
= fileinfo
->st_mtime
;
2305 ppd
->record
.size
= fileinfo
->st_size
;
2306 ppd
->record
.model_number
= model_number
;
2307 ppd
->record
.type
= type
;
2309 strlcpy(ppd
->record
.filename
, name
, sizeof(ppd
->record
.filename
));
2310 strlcpy(ppd
->record
.name
, name
, sizeof(ppd
->record
.name
));
2311 strlcpy(ppd
->record
.languages
[0], lang_version
,
2312 sizeof(ppd
->record
.languages
[0]));
2313 strlcpy(ppd
->record
.products
[0], (char *)cupsArrayFirst(products
),
2314 sizeof(ppd
->record
.products
[0]));
2315 strlcpy(ppd
->record
.psversions
[0], (char *)cupsArrayFirst(psversions
),
2316 sizeof(ppd
->record
.psversions
[0]));
2317 strlcpy(ppd
->record
.make
, manufacturer
, sizeof(ppd
->record
.make
));
2318 strlcpy(ppd
->record
.make_and_model
, make_model
,
2319 sizeof(ppd
->record
.make_and_model
));
2320 strlcpy(ppd
->record
.device_id
, device_id
, sizeof(ppd
->record
.device_id
));
2321 strlcpy(ppd
->record
.scheme
, scheme
, sizeof(ppd
->record
.scheme
));
2325 * Add remaining products, versions, and languages...
2329 i
< PPD_MAX_PROD
&& (ptr
= (char *)cupsArrayNext(products
)) != NULL
;
2331 strlcpy(ppd
->record
.products
[i
], ptr
,
2332 sizeof(ppd
->record
.products
[0]));
2335 i
< PPD_MAX_VERS
&& (ptr
= (char *)cupsArrayNext(psversions
)) != NULL
;
2337 strlcpy(ppd
->record
.psversions
[i
], ptr
,
2338 sizeof(ppd
->record
.psversions
[0]));
2340 for (i
= 1, ptr
= (char *)cupsArrayFirst(cups_languages
);
2341 i
< PPD_MAX_LANG
&& ptr
;
2342 i
++, ptr
= (char *)cupsArrayNext(cups_languages
))
2343 strlcpy(ppd
->record
.languages
[i
], ptr
,
2344 sizeof(ppd
->record
.languages
[0]));
2347 * Free products, versions, and languages...
2350 free_array(cups_languages
);
2351 free_array(products
);
2352 free_array(psversions
);
2359 * 'load_ppds()' - Load PPD files recursively.
2362 static int /* O - 1 on success, 0 on failure */
2363 load_ppds(const char *d
, /* I - Actual directory */
2364 const char *p
, /* I - Virtual path in name */
2365 int descend
) /* I - Descend into directories? */
2367 struct stat dinfo
, /* Directory information */
2368 *dinfoptr
; /* Pointer to match */
2369 cups_file_t
*fp
; /* Pointer to file */
2370 cups_dir_t
*dir
; /* Directory pointer */
2371 cups_dentry_t
*dent
; /* Directory entry */
2372 char filename
[1024], /* Name of PPD or directory */
2373 line
[256], /* Line from file */
2374 *ptr
, /* Pointer into name */
2375 name
[256]; /* Name of PPD file */
2376 ppd_info_t
*ppd
, /* New PPD file */
2377 key
; /* Search key */
2381 * See if we've loaded this directory before...
2384 if (stat(d
, &dinfo
))
2386 if (errno
!= ENOENT
)
2387 fprintf(stderr
, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d
,
2392 else if (cupsArrayFind(Inodes
, &dinfo
))
2394 fprintf(stderr
, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
2400 * Nope, add it to the Inodes array and continue...
2403 dinfoptr
= (struct stat
*)malloc(sizeof(struct stat
));
2404 memcpy(dinfoptr
, &dinfo
, sizeof(struct stat
));
2405 cupsArrayAdd(Inodes
, dinfoptr
);
2408 * Check permissions...
2411 if (_cupsFileCheck(d
, _CUPS_FILE_CHECK_DIRECTORY
, !geteuid(),
2412 _cupsFileCheckFilter
, NULL
))
2415 if ((dir
= cupsDirOpen(d
)) == NULL
)
2417 if (errno
!= ENOENT
)
2419 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
2420 d
, strerror(errno
));
2425 fprintf(stderr
, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d
);
2427 while ((dent
= cupsDirRead(dir
)) != NULL
)
2430 * Skip files/directories starting with "."...
2433 if (dent
->filename
[0] == '.')
2437 * See if this is a file...
2440 snprintf(filename
, sizeof(filename
), "%s/%s", d
, dent
->filename
);
2443 snprintf(name
, sizeof(name
), "%s/%s", p
, dent
->filename
);
2445 strlcpy(name
, dent
->filename
, sizeof(name
));
2447 if (S_ISDIR(dent
->fileinfo
.st_mode
))
2450 * Do subdirectory...
2455 if (!load_ppds(filename
, name
, 1))
2461 else if ((ptr
= filename
+ strlen(filename
) - 14) > filename
&&
2462 !strcmp(ptr
, ".printerDriver"))
2465 * Load PPDs in a printer driver bundle.
2468 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_DIRECTORY
, !geteuid(),
2469 _cupsFileCheckFilter
, NULL
))
2472 strlcat(filename
, "/Contents/Resources/PPDs", sizeof(filename
));
2473 strlcat(name
, "/Contents/Resources/PPDs", sizeof(name
));
2475 load_ppds(filename
, name
, 0);
2480 else if (strstr(filename
, ".plist"))
2483 * Skip plist files in the PPDs directory...
2488 else if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_FILE_ONLY
, !geteuid(),
2489 _cupsFileCheckFilter
, NULL
))
2493 * See if this file has been scanned before...
2496 strlcpy(key
.record
.filename
, name
, sizeof(key
.record
.filename
));
2497 strlcpy(key
.record
.name
, name
, sizeof(key
.record
.name
));
2499 ppd
= (ppd_info_t
*)cupsArrayFind(PPDsByName
, &key
);
2502 ppd
->record
.size
== dent
->fileinfo
.st_size
&&
2503 ppd
->record
.mtime
== dent
->fileinfo
.st_mtime
)
2506 * Rewind to the first entry for this file...
2509 while ((ppd
= (ppd_info_t
*)cupsArrayPrev(PPDsByName
)) != NULL
&&
2510 !strcmp(ppd
->record
.filename
, name
));
2513 * Then mark all of the matches for this file as found...
2516 while ((ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
)) != NULL
&&
2517 !strcmp(ppd
->record
.filename
, name
))
2524 * No, file is new/changed, so re-scan it...
2527 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2531 * Now see if this is a PPD file...
2535 cupsFileGets(fp
, line
, sizeof(line
));
2537 if (!strncmp(line
, "*PPD-Adobe:", 11))
2543 load_ppd(filename
, name
, "file", &dent
->fileinfo
, ppd
, fp
, 0);
2548 * Nope, treat it as a driver information file or archive...
2553 if ((ptr
= strstr(filename
, ".tar")) != NULL
&&
2554 (!strcmp(ptr
, ".tar") || !strcmp(ptr
, ".tar.gz")))
2555 load_tar(filename
, name
, fp
, dent
->fileinfo
.st_mtime
,
2556 dent
->fileinfo
.st_size
);
2558 load_drv(filename
, name
, fp
, dent
->fileinfo
.st_mtime
,
2559 dent
->fileinfo
.st_size
);
2576 * 'load_ppds_dat()' - Load the ppds.dat file.
2580 load_ppds_dat(char *filename
, /* I - Filename buffer */
2581 size_t filesize
, /* I - Size of filename buffer */
2582 int verbose
) /* I - Be verbose? */
2584 ppd_info_t
*ppd
; /* Current PPD file */
2585 cups_file_t
*fp
; /* ppds.dat file */
2586 struct stat fileinfo
; /* ppds.dat information */
2587 const char *cups_cachedir
; /* CUPS_CACHEDIR environment variable */
2590 PPDsByName
= cupsArrayNew((cups_array_func_t
)compare_names
, NULL
);
2591 PPDsByMakeModel
= cupsArrayNew((cups_array_func_t
)compare_ppds
, NULL
);
2596 if ((cups_cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
2597 cups_cachedir
= CUPS_CACHEDIR
;
2599 snprintf(filename
, filesize
, "%s/ppds.dat", cups_cachedir
);
2602 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
2605 * See if we have the right sync word...
2608 unsigned ppdsync
; /* Sync word */
2609 int num_ppds
; /* Number of PPDs */
2611 if ((size_t)cupsFileRead(fp
, (char *)&ppdsync
, sizeof(ppdsync
)) == sizeof(ppdsync
) &&
2612 ppdsync
== PPD_SYNC
&&
2613 !stat(filename
, &fileinfo
) &&
2614 (((size_t)fileinfo
.st_size
- sizeof(ppdsync
)) % sizeof(ppd_rec_t
)) == 0 &&
2615 (num_ppds
= ((size_t)fileinfo
.st_size
- sizeof(ppdsync
)) / sizeof(ppd_rec_t
)) > 0)
2618 * We have a ppds.dat file, so read it!
2621 for (; num_ppds
> 0; num_ppds
--)
2623 if ((ppd
= (ppd_info_t
*)calloc(1, sizeof(ppd_info_t
))) == NULL
)
2626 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
2631 if (cupsFileRead(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
)) > 0)
2633 cupsArrayAdd(PPDsByName
, ppd
);
2634 cupsArrayAdd(PPDsByMakeModel
, ppd
);
2644 fprintf(stderr
, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
2645 filename
, cupsArrayCount(PPDsByName
));
2654 * 'load_tar()' - Load archived PPD files.
2657 static int /* O - 1 on success, 0 on failure */
2658 load_tar(const char *filename
, /* I - Actual filename */
2659 const char *name
, /* I - Name to the rest of the world */
2660 cups_file_t
*fp
, /* I - File to read from */
2661 time_t mtime
, /* I - Mod time of driver info file */
2662 off_t size
) /* I - Size of driver info file */
2664 char curname
[256], /* Current archive file name */
2665 uri
[1024]; /* Virtual file URI */
2666 const char *curext
; /* Extension on file */
2667 struct stat curinfo
; /* Current archive file information */
2668 off_t next
; /* Position for next header */
2672 * Add a dummy entry for the file...
2677 add_ppd(name
, name
, "", "", "", "", "", "", mtime
, (size_t)size
, 0, PPD_TYPE_ARCHIVE
, "file");
2681 * Scan for PPDs in the archive...
2684 while (read_tar(fp
, curname
, sizeof(curname
), &curinfo
))
2686 next
= cupsFileTell(fp
) + ((curinfo
.st_size
+ TAR_BLOCK
- 1) &
2689 if ((curext
= strrchr(curname
, '.')) != NULL
&&
2690 !_cups_strcasecmp(curext
, ".ppd"))
2692 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "file", "", "",
2693 0, "/%s/%s", name
, curname
);
2694 load_ppd(name
, uri
, "file", &curinfo
, NULL
, fp
, next
);
2697 if (cupsFileTell(fp
) != next
)
2698 cupsFileSeek(fp
, next
);
2706 * 'read_tar()' - Read a file header from an archive.
2708 * This function skips all directories and special files.
2711 static int /* O - 1 if found, 0 on EOF */
2712 read_tar(cups_file_t
*fp
, /* I - Archive to read */
2713 char *name
, /* I - Filename buffer */
2714 size_t namesize
, /* I - Size of filename buffer */
2715 struct stat
*info
) /* O - File information */
2717 tar_rec_t record
; /* Record from file */
2720 while ((size_t)cupsFileRead(fp
, (char *)&record
, sizeof(record
)) == sizeof(record
))
2723 * Check for a valid tar header...
2726 if (memcmp(record
.header
.magic
, TAR_MAGIC
, 6) ||
2727 memcmp(record
.header
.version
, TAR_VERSION
, 2))
2729 if (record
.header
.magic
[0] ||
2730 memcmp(record
.header
.magic
, record
.header
.magic
+ 1, 5))
2731 fputs("ERROR: [cups-driverd] Bad tar magic/version.\n", stderr
);
2736 * Ignore non-files...
2739 if (record
.header
.linkflag
!= TAR_OLDNORMAL
&&
2740 record
.header
.linkflag
!= TAR_NORMAL
)
2744 * Grab size and name from tar header and return...
2747 if (record
.header
.prefix
[0])
2748 snprintf(name
, namesize
, "%s/%s", record
.header
.prefix
,
2749 record
.header
.pathname
);
2751 strlcpy(name
, record
.header
.pathname
, namesize
);
2753 info
->st_mtime
= strtol(record
.header
.mtime
, NULL
, 8);
2754 info
->st_size
= strtoll(record
.header
.size
, NULL
, 8);
2764 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2768 static regex_t
* /* O - Regular expression */
2769 regex_device_id(const char *device_id
) /* I - IEEE-1284 device ID */
2771 char res
[2048], /* Regular expression string */
2772 *ptr
; /* Pointer into string */
2773 regex_t
*re
; /* Regular expression */
2774 int cmd
; /* Command set string? */
2777 fprintf(stderr
, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id
);
2780 * Scan the device ID string and insert class, command set, manufacturer, and
2781 * model attributes to match. We assume that the device ID in the PPD and the
2782 * device ID reported by the device itself use the same attribute names and
2783 * order of attributes.
2788 while (*device_id
&& ptr
< (res
+ sizeof(res
) - 6))
2790 cmd
= !_cups_strncasecmp(device_id
, "COMMAND SET:", 12) ||
2791 !_cups_strncasecmp(device_id
, "CMD:", 4);
2793 if (cmd
|| !_cups_strncasecmp(device_id
, "MANUFACTURER:", 13) ||
2794 !_cups_strncasecmp(device_id
, "MFG:", 4) ||
2795 !_cups_strncasecmp(device_id
, "MFR:", 4) ||
2796 !_cups_strncasecmp(device_id
, "MODEL:", 6) ||
2797 !_cups_strncasecmp(device_id
, "MDL:", 4))
2807 while (*device_id
&& *device_id
!= ';' && ptr
< (res
+ sizeof(res
) - 8))
2809 if (strchr("[]{}().*\\|", *device_id
))
2811 if (*device_id
== ':')
2817 *ptr
++ = *device_id
++;
2822 *ptr
++ = *device_id
++;
2825 if (*device_id
== ';' || !*device_id
)
2839 else if ((device_id
= strchr(device_id
, ';')) == NULL
)
2847 fprintf(stderr
, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res
);
2850 * Compile the regular expression and return...
2853 if (res
[0] && (re
= (regex_t
*)calloc(1, sizeof(regex_t
))) != NULL
)
2855 if (!regcomp(re
, res
, REG_EXTENDED
| REG_ICASE
))
2857 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr
);
2869 * 'regex_string()' - Construct a regular expression to compare a simple string.
2872 static regex_t
* /* O - Regular expression */
2873 regex_string(const char *s
) /* I - String to compare */
2875 char res
[2048], /* Regular expression string */
2876 *ptr
; /* Pointer into string */
2877 regex_t
*re
; /* Regular expression */
2880 fprintf(stderr
, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s
);
2883 * Convert the string to a regular expression, escaping special characters
2889 while (*s
&& ptr
< (res
+ sizeof(res
) - 2))
2891 if (strchr("[]{}().*\\", *s
))
2899 fprintf(stderr
, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res
);
2902 * Create a case-insensitive regular expression...
2905 if (res
[0] && (re
= (regex_t
*)calloc(1, sizeof(regex_t
))) != NULL
)
2907 if (!regcomp(re
, res
, REG_ICASE
))
2909 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr
);