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-2015 by Apple Inc.
9 * Copyright 1997-2007 by Easy Software Products.
11 * These coded instructions, statements, and computer programs are the
12 * property of Apple Inc. and are protected by Federal copyright
13 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
14 * which should have been included with this file. If this file is
15 * file is missing or damaged, see the license at "http://www.cups.org/".
19 * Include necessary headers...
24 #include <cups/transcode.h>
25 #include <cups/ppd-private.h>
26 #include <ppdc/ppdc.h>
34 #define PPD_SYNC 0x50504438 /* Sync word for ppds.dat (PPD8) */
35 #define PPD_MAX_LANG 32 /* Maximum languages */
36 #define PPD_MAX_PROD 32 /* Maximum products */
37 #define PPD_MAX_VERS 32 /* Maximum versions */
39 #define PPD_TYPE_POSTSCRIPT 0 /* PostScript PPD */
40 #define PPD_TYPE_PDF 1 /* PDF PPD */
41 #define PPD_TYPE_RASTER 2 /* CUPS raster PPD */
42 #define PPD_TYPE_FAX 3 /* Facsimile/MFD PPD */
43 #define PPD_TYPE_OBJECT_ANY 4 /* 3D (AMF/STL/g-code) PPD */
44 #define PPD_TYPE_OBJECT_DIRECT 5 /* 3D (AMF/STL/g-code) PPD over any connection */
45 #define PPD_TYPE_OBJECT_STORAGE 6 /* 3D (AMF/STL/g-code) PPD for storage to SD card, etc. */
46 #define PPD_TYPE_UNKNOWN 7 /* Other/hybrid PPD */
47 #define PPD_TYPE_DRV 8 /* Driver info file */
48 #define PPD_TYPE_ARCHIVE 9 /* Archive file */
50 #define TAR_BLOCK 512 /* Number of bytes in a block */
51 #define TAR_BLOCKS 10 /* Blocking factor */
53 #define TAR_MAGIC "ustar" /* 5 chars and a null */
54 #define TAR_VERSION "00" /* POSIX tar version */
56 #define TAR_OLDNORMAL '\0' /* Normal disk file, Unix compat */
57 #define TAR_NORMAL '0' /* Normal disk file */
58 #define TAR_LINK '1' /* Link to previously dumped file */
59 #define TAR_SYMLINK '2' /* Symbolic link */
60 #define TAR_CHR '3' /* Character special file */
61 #define TAR_BLK '4' /* Block special file */
62 #define TAR_DIR '5' /* Directory */
63 #define TAR_FIFO '6' /* FIFO special file */
64 #define TAR_CONTIG '7' /* Contiguous file */
68 * PPD information structures...
71 typedef struct /**** PPD record ****/
73 time_t mtime
; /* Modification time */
74 off_t size
; /* Size in bytes */
75 int model_number
; /* cupsModelNumber */
76 int type
; /* ppd-type */
77 char filename
[512], /* Filename */
78 name
[512], /* PPD name */
79 languages
[PPD_MAX_LANG
][6],
80 /* LanguageVersion/cupsLanguages */
81 products
[PPD_MAX_PROD
][128],
83 psversions
[PPD_MAX_VERS
][32],
84 /* PSVersion strings */
85 make
[128], /* Manufacturer */
86 make_and_model
[128], /* NickName/ModelName */
87 device_id
[256], /* IEEE 1284 Device ID */
88 scheme
[128]; /* PPD scheme */
91 typedef struct /**** In-memory record ****/
93 int found
; /* 1 if PPD is found */
94 int matches
; /* Match count */
95 ppd_rec_t record
; /* PPDs.dat record */
98 typedef union /**** TAR record format ****/
100 unsigned char all
[TAR_BLOCK
]; /* Raw data block */
103 char pathname
[100], /* Destination path */
104 mode
[8], /* Octal file permissions */
105 uid
[8], /* Octal user ID */
106 gid
[8], /* Octal group ID */
107 size
[12], /* Octal size in bytes */
108 mtime
[12], /* Octal modification time */
109 chksum
[8], /* Octal checksum value */
110 linkflag
, /* File type */
111 linkname
[100], /* Source path for link */
112 magic
[6], /* Magic string */
113 version
[2], /* Format version */
114 uname
[32], /* User name */
115 gname
[32], /* Group name */
116 devmajor
[8], /* Octal device major number */
117 devminor
[8], /* Octal device minor number */
118 prefix
[155]; /* Prefix for long filenames */
127 static cups_array_t
*Inodes
= NULL
, /* Inodes of directories we've visited */
129 /* PPD files sorted by filename and name */
130 *PPDsByMakeModel
= NULL
;
131 /* PPD files sorted by make and model */
132 static int ChangedPPD
; /* Did we change the PPD database? */
133 static const char * const PPDTypes
[] = /* ppd-type values */
152 static ppd_info_t
*add_ppd(const char *filename
, const char *name
,
153 const char *language
, const char *make
,
154 const char *make_and_model
,
155 const char *device_id
, const char *product
,
156 const char *psversion
, time_t mtime
,
157 size_t size
, int model_number
, int type
,
159 static int cat_drv(const char *name
, int request_id
);
160 static int cat_ppd(const char *name
, int request_id
);
161 static int cat_static(const char *name
, int request_id
);
162 static int cat_tar(const char *name
, int request_id
);
163 static int compare_inodes(struct stat
*a
, struct stat
*b
);
164 static int compare_matches(const ppd_info_t
*p0
,
165 const ppd_info_t
*p1
);
166 static int compare_names(const ppd_info_t
*p0
,
167 const ppd_info_t
*p1
);
168 static int compare_ppds(const ppd_info_t
*p0
,
169 const ppd_info_t
*p1
);
170 static int dump_ppds_dat(const char *filename
);
171 static void free_array(cups_array_t
*a
);
172 static cups_file_t
*get_file(const char *name
, int request_id
,
173 const char *subdir
, char *buffer
,
174 size_t bufsize
, char **subfile
);
175 static int list_ppds(int request_id
, int limit
, const char *opt
);
176 static int load_drivers(cups_array_t
*include
,
177 cups_array_t
*exclude
);
178 static int load_drv(const char *filename
, const char *name
,
179 cups_file_t
*fp
, time_t mtime
, off_t size
);
180 static void load_ppd(const char *filename
, const char *name
,
181 const char *scheme
, struct stat
*fileinfo
,
182 ppd_info_t
*ppd
, cups_file_t
*fp
, off_t end
);
183 static int load_ppds(const char *d
, const char *p
, int descend
);
184 static void load_ppds_dat(char *filename
, size_t filesize
,
186 static int load_tar(const char *filename
, const char *name
,
187 cups_file_t
*fp
, time_t mtime
, off_t size
);
188 static int read_tar(cups_file_t
*fp
, char *name
, size_t namesize
,
190 static regex_t
*regex_device_id(const char *device_id
);
191 static regex_t
*regex_string(const char *s
);
195 * 'main()' - Scan for drivers and return an IPP response.
199 * cups-driverd request_id limit options
202 int /* O - Exit code */
203 main(int argc
, /* I - Number of command-line args */
204 char *argv
[]) /* I - Command-line arguments */
207 * Install or list PPDs...
210 if (argc
== 3 && !strcmp(argv
[1], "cat"))
211 return (cat_ppd(argv
[2], 0));
212 else if ((argc
== 2 || argc
== 3) && !strcmp(argv
[1], "dump"))
213 return (dump_ppds_dat(argv
[2]));
214 else if (argc
== 4 && !strcmp(argv
[1], "get"))
215 return (cat_ppd(argv
[3], atoi(argv
[2])));
216 else if (argc
== 5 && !strcmp(argv
[1], "list"))
217 return (list_ppds(atoi(argv
[2]), atoi(argv
[3]), argv
[4]));
220 fputs("Usage: cups-driverd cat ppd-name\n", stderr
);
221 fputs("Usage: cups-driverd dump\n", stderr
);
222 fputs("Usage: cups-driverd get request_id ppd-name\n", stderr
);
223 fputs("Usage: cups-driverd list request_id limit options\n", stderr
);
230 * 'add_ppd()' - Add a PPD file.
233 static ppd_info_t
* /* O - PPD */
234 add_ppd(const char *filename
, /* I - PPD filename */
235 const char *name
, /* I - PPD name */
236 const char *language
, /* I - LanguageVersion */
237 const char *make
, /* I - Manufacturer */
238 const char *make_and_model
, /* I - NickName/ModelName */
239 const char *device_id
, /* I - 1284DeviceID */
240 const char *product
, /* I - Product */
241 const char *psversion
, /* I - PSVersion */
242 time_t mtime
, /* I - Modification time */
243 size_t size
, /* I - File size */
244 int model_number
, /* I - Model number */
245 int type
, /* I - Driver type */
246 const char *scheme
) /* I - PPD scheme */
248 ppd_info_t
*ppd
; /* PPD */
249 char *recommended
; /* Foomatic driver string */
253 * Add a new PPD file...
256 if ((ppd
= (ppd_info_t
*)calloc(1, sizeof(ppd_info_t
))) == NULL
)
259 "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n",
260 cupsArrayCount(PPDsByName
));
265 * Zero-out the PPD data and copy the values over...
269 ppd
->record
.mtime
= mtime
;
270 ppd
->record
.size
= (off_t
)size
;
271 ppd
->record
.model_number
= model_number
;
272 ppd
->record
.type
= type
;
274 strlcpy(ppd
->record
.filename
, filename
, sizeof(ppd
->record
.filename
));
275 strlcpy(ppd
->record
.name
, name
, sizeof(ppd
->record
.name
));
276 strlcpy(ppd
->record
.languages
[0], language
,
277 sizeof(ppd
->record
.languages
[0]));
278 strlcpy(ppd
->record
.products
[0], product
, sizeof(ppd
->record
.products
[0]));
279 strlcpy(ppd
->record
.psversions
[0], psversion
,
280 sizeof(ppd
->record
.psversions
[0]));
281 strlcpy(ppd
->record
.make
, make
, sizeof(ppd
->record
.make
));
282 strlcpy(ppd
->record
.make_and_model
, make_and_model
,
283 sizeof(ppd
->record
.make_and_model
));
284 strlcpy(ppd
->record
.device_id
, device_id
, sizeof(ppd
->record
.device_id
));
285 strlcpy(ppd
->record
.scheme
, scheme
, sizeof(ppd
->record
.scheme
));
288 * Strip confusing (and often wrong) "recommended" suffix added by
289 * Foomatic drivers...
292 if ((recommended
= strstr(ppd
->record
.make_and_model
,
293 " (recommended)")) != NULL
)
297 * Add the PPD to the PPD arrays...
300 cupsArrayAdd(PPDsByName
, ppd
);
301 cupsArrayAdd(PPDsByMakeModel
, ppd
);
304 * Return the new PPD pointer...
312 * 'cat_drv()' - Generate a PPD from a driver info file.
315 static int /* O - Exit code */
316 cat_drv(const char *name
, /* I - PPD name */
317 int request_id
) /* I - Request ID for response? */
319 cups_file_t
*fp
; // File pointer
320 ppdcSource
*src
; // PPD source file data
321 ppdcDriver
*d
; // Current driver
322 cups_file_t
*out
; // Stdout via CUPS file API
323 char message
[2048], // status-message
324 filename
[1024], // Full path to .drv file(s)
325 scheme
[32], // URI scheme ("drv")
326 userpass
[256], // User/password info (unused)
327 host
[2], // Hostname (unused)
328 resource
[1024], // Resource path (/dir/to/filename.drv)
329 *pc_file_name
; // Filename portion of URI
330 int port
; // Port number (unused)
333 // Pull out the path to the .drv file...
334 if (httpSeparateURI(HTTP_URI_CODING_ALL
, name
, scheme
, sizeof(scheme
),
335 userpass
, sizeof(userpass
), host
, sizeof(host
), &port
,
336 resource
, sizeof(resource
)) < HTTP_URI_OK
)
338 fprintf(stderr
, "ERROR: Bad PPD name \"%s\".\n", name
);
342 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\".", name
);
344 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
345 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
346 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
347 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
349 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
350 cupsdSendIPPTrailer();
356 if ((fp
= get_file(resource
, request_id
, "drv", filename
, sizeof(filename
),
357 &pc_file_name
)) == NULL
)
360 src
= new ppdcSource(filename
, fp
);
362 for (d
= (ppdcDriver
*)src
->drivers
->first();
364 d
= (ppdcDriver
*)src
->drivers
->next())
365 if (!strcmp(pc_file_name
, d
->pc_file_name
->value
) ||
366 (d
->file_name
&& !strcmp(pc_file_name
, d
->file_name
->value
)))
371 ppdcArray
*locales
; // Locale names
372 ppdcCatalog
*catalog
; // Message catalog in .drv file
375 fprintf(stderr
, "DEBUG2: [cups-driverd] %d locales defined in \"%s\"...\n",
376 src
->po_files
->count
, filename
);
378 locales
= new ppdcArray();
379 for (catalog
= (ppdcCatalog
*)src
->po_files
->first();
381 catalog
= (ppdcCatalog
*)src
->po_files
->next())
383 fprintf(stderr
, "DEBUG2: [cups-driverd] Adding locale \"%s\"...\n",
384 catalog
->locale
->value
);
385 catalog
->locale
->retain();
386 locales
->add(catalog
->locale
);
391 cupsdSendIPPHeader(IPP_OK
, request_id
);
392 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
393 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
394 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
396 cupsdSendIPPTrailer();
400 out
= cupsFileStdout();
401 d
->write_ppd_file(out
, NULL
, locales
, src
, PPDC_LFONLY
);
408 fprintf(stderr
, "ERROR: PPD \"%s\" not found.\n", name
);
412 snprintf(message
, sizeof(message
), "PPD \"%s\" not found.", name
);
414 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
415 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
416 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
417 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
419 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
420 cupsdSendIPPTrailer();
432 * 'cat_ppd()' - Copy a PPD file to stdout.
435 static int /* O - Exit code */
436 cat_ppd(const char *name
, /* I - PPD name */
437 int request_id
) /* I - Request ID for response? */
439 char scheme
[256], /* Scheme from PPD name */
440 *sptr
, /* Pointer into scheme */
441 line
[1024], /* Line/filename */
442 message
[2048]; /* status-message */
446 * Figure out if this is a static or dynamic PPD file...
449 if (strstr(name
, "../"))
451 fputs("ERROR: Invalid PPD name.\n", stderr
);
455 strlcpy(scheme
, name
, sizeof(scheme
));
456 if ((sptr
= strchr(scheme
, ':')) != NULL
)
460 if (!strcmp(scheme
, "file"))
463 * "file:name" == "name"...
471 if (!strstr(name
, ".tar/") && !strstr(name
, ".tar.gz/"))
479 puts("Content-Type: application/ipp\n");
482 return (cat_static(name
, request_id
));
483 else if (!strcmp(scheme
, "drv"))
484 return (cat_drv(name
, request_id
));
485 else if (!strcmp(scheme
, "file"))
486 return (cat_tar(name
, request_id
));
490 * Dynamic PPD, see if we have a driver program to support it...
493 const char *serverbin
; /* CUPS_SERVERBIN env var */
494 char *argv
[4]; /* Arguments for program */
497 if ((serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
498 serverbin
= CUPS_SERVERBIN
;
500 snprintf(line
, sizeof(line
), "%s/driver/%s", serverbin
, scheme
);
501 if (access(line
, X_OK
))
504 * File does not exist or is not executable...
507 fprintf(stderr
, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n",
508 line
, strerror(errno
));
512 snprintf(message
, sizeof(message
), "Unable to access \"%s\" - %s",
513 line
, strerror(errno
));
515 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
516 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
517 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
518 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
520 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
521 cupsdSendIPPTrailer();
528 * Yes, let it cat the PPD file...
533 cupsdSendIPPHeader(IPP_OK
, request_id
);
534 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
535 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
536 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
538 cupsdSendIPPTrailer();
542 argv
[1] = (char *)"cat";
543 argv
[2] = (char *)name
;
546 if (cupsdExec(line
, argv
))
549 * Unable to execute driver...
552 fprintf(stderr
, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n",
553 line
, strerror(errno
));
559 * Return with no errors...
567 * 'copy_static()' - Copy a static PPD file to stdout.
570 static int /* O - Exit code */
571 cat_static(const char *name
, /* I - PPD name */
572 int request_id
) /* I - Request ID for response? */
574 cups_file_t
*fp
; /* PPD file */
575 char filename
[1024], /* PPD filename */
576 line
[1024]; /* Line buffer */
579 if ((fp
= get_file(name
, request_id
, "model", filename
, sizeof(filename
),
585 cupsdSendIPPHeader(IPP_OK
, request_id
);
586 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
587 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
588 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
590 cupsdSendIPPTrailer();
594 * Now copy the file to stdout...
597 while (cupsFileGets(fp
, line
, sizeof(line
)))
607 * 'cat_tar()' - Copy an archived PPD file to stdout.
610 static int /* O - Exit code */
611 cat_tar(const char *name
, /* I - PPD name */
612 int request_id
) /* I - Request ID */
614 cups_file_t
*fp
; /* Archive file pointer */
615 char filename
[1024], /* Archive filename */
616 *ppdname
, /* PPD filename in archive */
617 curname
[256], /* Current name in archive */
618 buffer
[8192]; /* Copy buffer */
619 struct stat curinfo
; /* Current file info in archive */
620 off_t total
, /* Total bytes copied */
621 next
; /* Offset for next record in archive */
622 ssize_t bytes
; /* Bytes read */
626 * Open the archive file...
629 if ((fp
= get_file(name
, request_id
, "model", filename
, sizeof(filename
),
630 &ppdname
)) == NULL
|| !ppdname
)
634 * Scan the archive for the PPD...
637 while (read_tar(fp
, curname
, sizeof(curname
), &curinfo
))
639 next
= cupsFileTell(fp
) + ((curinfo
.st_size
+ TAR_BLOCK
- 1) &
642 if (!strcmp(ppdname
, curname
))
646 cupsdSendIPPHeader(IPP_OK
, request_id
);
647 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
648 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
649 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
651 cupsdSendIPPTrailer();
654 for (total
= 0; total
< curinfo
.st_size
; total
+= bytes
)
656 if ((size_t)(bytes
= (curinfo
.st_size
- total
)) > sizeof(buffer
))
657 bytes
= sizeof(buffer
);
659 if ((bytes
= cupsFileRead(fp
, buffer
, (size_t)bytes
)) < 0)
661 if (errno
== EINTR
|| errno
== EAGAIN
)
667 perror("ERROR: [cups-driverd] Read error");
671 else if (bytes
> 0 && fwrite(buffer
, (size_t)bytes
, 1, stdout
) != 1)
679 if (cupsFileTell(fp
) != next
)
680 cupsFileSeek(fp
, next
);
685 fprintf(stderr
, "ERROR: PPD \"%s\" not found.\n", name
);
689 snprintf(buffer
, sizeof(buffer
), "PPD \"%s\" not found.", name
);
691 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
692 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
693 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
694 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
696 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", buffer
);
697 cupsdSendIPPTrailer();
705 * 'compare_inodes()' - Compare two inodes.
708 static int /* O - Result of comparison */
709 compare_inodes(struct stat
*a
, /* I - First inode */
710 struct stat
*b
) /* I - Second inode */
712 if (a
->st_dev
!= b
->st_dev
)
713 return (a
->st_dev
- b
->st_dev
);
715 return (a
->st_ino
- b
->st_ino
);
720 * 'compare_matches()' - Compare PPD match scores for sorting.
724 compare_matches(const ppd_info_t
*p0
, /* I - First PPD */
725 const ppd_info_t
*p1
) /* I - Second PPD */
727 if (p1
->matches
!= p0
->matches
)
728 return (p1
->matches
- p0
->matches
);
730 return (cupsdCompareNames(p0
->record
.make_and_model
,
731 p1
->record
.make_and_model
));
736 * 'compare_names()' - Compare PPD filenames for sorting.
739 static int /* O - Result of comparison */
740 compare_names(const ppd_info_t
*p0
, /* I - First PPD file */
741 const ppd_info_t
*p1
) /* I - Second PPD file */
743 int diff
; /* Difference between strings */
746 if ((diff
= strcmp(p0
->record
.filename
, p1
->record
.filename
)) != 0)
749 return (strcmp(p0
->record
.name
, p1
->record
.name
));
754 * 'compare_ppds()' - Compare PPD file make and model names for sorting.
757 static int /* O - Result of comparison */
758 compare_ppds(const ppd_info_t
*p0
, /* I - First PPD file */
759 const ppd_info_t
*p1
) /* I - Second PPD file */
761 int diff
; /* Difference between strings */
765 * First compare manufacturers...
768 if ((diff
= _cups_strcasecmp(p0
->record
.make
, p1
->record
.make
)) != 0)
770 else if ((diff
= cupsdCompareNames(p0
->record
.make_and_model
,
771 p1
->record
.make_and_model
)) != 0)
773 else if ((diff
= strcmp(p0
->record
.languages
[0],
774 p1
->record
.languages
[0])) != 0)
777 return (compare_names(p0
, p1
));
782 * 'dump_ppds_dat()' - Dump the contents of the ppds.dat file.
785 static int /* O - Exit status */
786 dump_ppds_dat(const char *filename
) /* I - Filename */
788 char temp
[1024]; /* ppds.dat filename */
789 ppd_info_t
*ppd
; /* Current PPD */
793 * See if we a PPD database file...
797 strlcpy(temp
, filename
, sizeof(temp
));
801 load_ppds_dat(temp
, sizeof(temp
), 0);
803 puts("mtime,size,model_number,type,filename,name,languages0,products0,"
804 "psversions0,make,make_and_model,device_id,scheme");
805 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
807 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
808 printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
810 (int)ppd
->record
.mtime
, (long)ppd
->record
.size
,
811 ppd
->record
.model_number
, ppd
->record
.type
, ppd
->record
.filename
,
812 ppd
->record
.name
, ppd
->record
.languages
[0], ppd
->record
.products
[0],
813 ppd
->record
.psversions
[0], ppd
->record
.make
,
814 ppd
->record
.make_and_model
, ppd
->record
.device_id
,
822 * 'free_array()' - Free an array of strings.
826 free_array(cups_array_t
*a
) /* I - Array to free */
828 char *ptr
; /* Pointer to string */
831 for (ptr
= (char *)cupsArrayFirst(a
);
833 ptr
= (char *)cupsArrayNext(a
))
841 * 'get_file()' - Get the filename associated with a request.
844 static cups_file_t
* /* O - File pointer or NULL */
845 get_file(const char *name
, /* I - Name */
846 int request_id
, /* I - Request ID */
847 const char *subdir
, /* I - Subdirectory for file */
848 char *buffer
, /* I - Filename buffer */
849 size_t bufsize
, /* I - Size of filename buffer */
850 char **subfile
) /* O - Sub-filename */
852 cups_file_t
*fp
; /* File pointer */
853 const char *datadir
; /* CUPS_DATADIR env var */
854 char *bufptr
, /* Pointer into filename buffer */
855 message
[2048]; /* status-message */
857 const char *printerDriver
, /* Pointer to .printerDriver extension */
858 *slash
; /* Pointer to next slash */
859 #endif /* __APPLE__ */
868 if (strstr(name
, "../") || strstr(name
, "/.."))
874 fprintf(stderr
, "ERROR: [cups-driverd] Bad PPD name \"%s\".\n", name
);
878 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\".", name
);
880 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
881 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
882 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
883 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
885 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
886 cupsdSendIPPTrailer();
893 * Try opening the file...
897 if (!strncmp(name
, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
898 !strncmp(name
, "Library/Printers/PPDs/Contents/Resources/", 41) ||
899 (!strncmp(name
, "System/Library/Printers/", 24) &&
902 ".printerDriver/Contents/Resources/PPDs")) != NULL
&&
903 (slash
= strchr(name
+ 24, '/')) != NULL
&&
904 slash
> printerDriver
) ||
905 (!strncmp(name
, "Library/Printers/", 17) &&
908 ".printerDriver/Contents/Resources/PPDs")) != NULL
&&
909 (slash
= strchr(name
+ 17, '/')) != NULL
&&
910 slash
> printerDriver
))
913 * Map ppd-name to macOS standard locations...
916 snprintf(buffer
, bufsize
, "/%s", name
);
920 #elif defined(__linux)
921 if (!strncmp(name
, "lsb/usr/", 8))
924 * Map ppd-name to LSB standard /usr/share/ppd location...
927 snprintf(buffer
, bufsize
, "/usr/share/ppd/%s", name
+ 8);
929 else if (!strncmp(name
, "lsb/opt/", 8))
932 * Map ppd-name to LSB standard /opt/share/ppd location...
935 snprintf(buffer
, bufsize
, "/opt/share/ppd/%s", name
+ 8);
937 else if (!strncmp(name
, "lsb/local/", 10))
940 * Map ppd-name to LSB standard /usr/local/share/ppd location...
943 snprintf(buffer
, bufsize
, "/usr/local/share/ppd/%s", name
+ 10);
947 #endif /* __APPLE__ */
949 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
950 datadir
= CUPS_DATADIR
;
952 snprintf(buffer
, bufsize
, "%s/%s/%s", datadir
, subdir
, name
);
956 * Strip anything after ".drv/", ".drv.gz/", ".tar/", or ".tar.gz/"...
961 if ((bufptr
= strstr(buffer
, ".drv/")) != NULL
)
963 else if ((bufptr
= strstr(buffer
, ".drv.gz/")) != NULL
)
965 else if ((bufptr
= strstr(buffer
, ".tar/")) != NULL
)
967 else if ((bufptr
= strstr(buffer
, ".tar.gz/")) != NULL
)
978 * Try opening the file...
981 if ((fp
= cupsFileOpen(buffer
, "r")) == NULL
)
983 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n",
984 buffer
, strerror(errno
));
988 snprintf(message
, sizeof(message
), "Unable to open \"%s\" - %s",
989 buffer
, strerror(errno
));
991 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
992 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
993 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
994 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
996 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
997 cupsdSendIPPTrailer();
1008 * 'list_ppds()' - List PPD files.
1011 static int /* O - Exit code */
1012 list_ppds(int request_id
, /* I - Request ID */
1013 int limit
, /* I - Limit */
1014 const char *opt
) /* I - Option argument */
1016 int i
; /* Looping vars */
1017 int count
; /* Number of PPDs to send */
1018 ppd_info_t
*ppd
; /* Current PPD file */
1019 cups_file_t
*fp
; /* ppds.dat file */
1020 char filename
[1024], /* ppds.dat filename */
1021 model
[1024]; /* Model directory */
1022 const char *cups_datadir
; /* CUPS_DATADIR environment variable */
1023 int num_options
; /* Number of options */
1024 cups_option_t
*options
; /* Options */
1025 cups_array_t
*requested
, /* requested-attributes values */
1026 *include
, /* PPD schemes to include */
1027 *exclude
; /* PPD schemes to exclude */
1028 const char *device_id
, /* ppd-device-id option */
1029 *language
, /* ppd-natural-language option */
1030 *make
, /* ppd-make option */
1031 *make_and_model
, /* ppd-make-and-model option */
1032 *model_number_str
, /* ppd-model-number option */
1033 *product
, /* ppd-product option */
1034 *psversion
, /* ppd-psversion option */
1035 *type_str
; /* ppd-type option */
1036 int model_number
, /* ppd-model-number value */
1037 type
, /* ppd-type value */
1038 send_device_id
, /* Send ppd-device-id? */
1039 send_make
, /* Send ppd-make? */
1040 send_make_and_model
, /* Send ppd-make-and-model? */
1041 send_model_number
, /* Send ppd-model-number? */
1042 send_name
, /* Send ppd-name? */
1043 send_natural_language
, /* Send ppd-natural-language? */
1044 send_product
, /* Send ppd-product? */
1045 send_psversion
, /* Send ppd-psversion? */
1046 send_type
, /* Send ppd-type? */
1047 sent_header
; /* Sent the IPP header? */
1048 size_t make_and_model_len
, /* Length of ppd-make-and-model */
1049 product_len
; /* Length of ppd-product */
1050 regex_t
*device_id_re
, /* Regular expression for matching device ID */
1051 *make_and_model_re
; /* Regular expression for matching make and model */
1052 regmatch_t re_matches
[6]; /* Regular expression matches */
1053 cups_array_t
*matches
; /* Matching PPDs */
1057 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
1058 "opt=\"%s\"\n", request_id
, limit
, opt
);
1061 * See if we a PPD database file...
1065 load_ppds_dat(filename
, sizeof(filename
), 1);
1068 * Load all PPDs in the specified directory and below...
1071 if ((cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
1072 cups_datadir
= CUPS_DATADIR
;
1074 Inodes
= cupsArrayNew((cups_array_func_t
)compare_inodes
, NULL
);
1076 snprintf(model
, sizeof(model
), "%s/model", cups_datadir
);
1077 load_ppds(model
, "", 1);
1079 snprintf(model
, sizeof(model
), "%s/drv", cups_datadir
);
1080 load_ppds(model
, "", 1);
1084 * Load PPDs from standard macOS locations...
1087 load_ppds("/Library/Printers",
1088 "Library/Printers", 0);
1089 load_ppds("/Library/Printers/PPDs/Contents/Resources",
1090 "Library/Printers/PPDs/Contents/Resources", 0);
1091 load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
1092 "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
1093 load_ppds("/System/Library/Printers",
1094 "System/Library/Printers", 0);
1095 load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
1096 "System/Library/Printers/PPDs/Contents/Resources", 0);
1097 load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
1098 "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
1100 #elif defined(__linux)
1102 * Load PPDs from LSB-defined locations...
1105 if (!access("/usr/local/share/ppd", 0))
1106 load_ppds("/usr/local/share/ppd", "lsb/local", 1);
1107 if (!access("/usr/share/ppd", 0))
1108 load_ppds("/usr/share/ppd", "lsb/usr", 1);
1109 if (!access("/opt/share/ppd", 0))
1110 load_ppds("/opt/share/ppd", "lsb/opt", 1);
1111 #endif /* __APPLE__ */
1114 * Cull PPD files that are no longer present...
1117 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
1119 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
1123 * Remove this PPD file from the list...
1126 cupsArrayRemove(PPDsByName
, ppd
);
1127 cupsArrayRemove(PPDsByMakeModel
, ppd
);
1134 * Write the new ppds.dat file...
1137 fprintf(stderr
, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD
);
1141 char newname
[1024]; /* New filename */
1143 snprintf(newname
, sizeof(newname
), "%s.%d", filename
, (int)getpid());
1145 if ((fp
= cupsFileOpen(newname
, "w")) != NULL
)
1147 unsigned ppdsync
= PPD_SYNC
; /* Sync word */
1149 cupsFileWrite(fp
, (char *)&ppdsync
, sizeof(ppdsync
));
1151 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
1153 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
1154 cupsFileWrite(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
));
1158 if (rename(newname
, filename
))
1159 fprintf(stderr
, "ERROR: [cups-driverd] Unable to rename \"%s\" - %s\n",
1160 newname
, strerror(errno
));
1162 fprintf(stderr
, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
1163 filename
, cupsArrayCount(PPDsByName
));
1166 fprintf(stderr
, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
1167 filename
, strerror(errno
));
1170 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr
);
1173 * Scan for dynamic PPD files...
1176 num_options
= cupsParseOptions(opt
, 0, &options
);
1177 exclude
= cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
1178 num_options
, options
));
1179 include
= cupsdCreateStringsArray(cupsGetOption("include-schemes",
1180 num_options
, options
));
1182 load_drivers(include
, exclude
);
1185 * Add the raw driver...
1188 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
1189 PPD_TYPE_UNKNOWN
, "raw");
1192 * Send IPP attributes...
1195 requested
= cupsdCreateStringsArray(
1196 cupsGetOption("requested-attributes", num_options
,
1198 device_id
= cupsGetOption("ppd-device-id", num_options
, options
);
1199 language
= cupsGetOption("ppd-natural-language", num_options
, options
);
1200 make
= cupsGetOption("ppd-make", num_options
, options
);
1201 make_and_model
= cupsGetOption("ppd-make-and-model", num_options
, options
);
1202 model_number_str
= cupsGetOption("ppd-model-number", num_options
, options
);
1203 product
= cupsGetOption("ppd-product", num_options
, options
);
1204 psversion
= cupsGetOption("ppd-psversion", num_options
, options
);
1205 type_str
= cupsGetOption("ppd-type", num_options
, options
);
1208 make_and_model_len
= strlen(make_and_model
);
1210 make_and_model_len
= 0;
1213 product_len
= strlen(product
);
1217 if (model_number_str
)
1218 model_number
= atoi(model_number_str
);
1225 type
< (int)(sizeof(PPDTypes
) / sizeof(PPDTypes
[0]));
1227 if (!strcmp(type_str
, PPDTypes
[type
]))
1230 if (type
>= (int)(sizeof(PPDTypes
) / sizeof(PPDTypes
[0])))
1232 fprintf(stderr
, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
1240 for (i
= 0; i
< num_options
; i
++)
1241 fprintf(stderr
, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options
[i
].name
,
1244 if (!requested
|| cupsArrayFind(requested
, (void *)"all") != NULL
)
1248 send_make_and_model
= 1;
1249 send_model_number
= 1;
1250 send_natural_language
= 1;
1258 send_name
= cupsArrayFind(requested
,
1259 (void *)"ppd-name") != NULL
;
1260 send_make
= cupsArrayFind(requested
,
1261 (void *)"ppd-make") != NULL
;
1262 send_make_and_model
= cupsArrayFind(requested
,
1263 (void *)"ppd-make-and-model") != NULL
;
1264 send_model_number
= cupsArrayFind(requested
,
1265 (void *)"ppd-model-number") != NULL
;
1266 send_natural_language
= cupsArrayFind(requested
,
1267 (void *)"ppd-natural-language") != NULL
;
1268 send_device_id
= cupsArrayFind(requested
,
1269 (void *)"ppd-device-id") != NULL
;
1270 send_product
= cupsArrayFind(requested
,
1271 (void *)"ppd-product") != NULL
;
1272 send_psversion
= cupsArrayFind(requested
,
1273 (void *)"ppd-psversion") != NULL
;
1274 send_type
= cupsArrayFind(requested
,
1275 (void *)"ppd-type") != NULL
;
1279 * Send the content type header to the scheduler; request_id can only be
1280 * 0 when run manually since the scheduler enforces the IPP requirement for
1281 * a request ID from 1 to 2^31-1...
1285 puts("Content-Type: application/ipp\n");
1289 if (limit
<= 0 || limit
> cupsArrayCount(PPDsByMakeModel
))
1290 count
= cupsArrayCount(PPDsByMakeModel
);
1294 if (device_id
|| language
|| make
|| make_and_model
|| model_number_str
||
1297 matches
= cupsArrayNew((cups_array_func_t
)compare_matches
, NULL
);
1300 device_id_re
= regex_device_id(device_id
);
1302 device_id_re
= NULL
;
1305 make_and_model_re
= regex_string(make_and_model
);
1307 make_and_model_re
= NULL
;
1309 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByMakeModel
);
1311 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
1314 * Filter PPDs based on make, model, product, language, model number,
1315 * and/or device ID using the "matches" score value. An exact match
1316 * for product, make-and-model, or device-id adds 3 to the score.
1317 * Partial matches for make-and-model yield 1 or 2 points, and matches
1318 * for the make and language add a single point. Results are then sorted
1319 * by score, highest score first.
1322 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1323 ppd
->record
.type
>= PPD_TYPE_DRV
)
1326 if (cupsArrayFind(exclude
, ppd
->record
.scheme
) ||
1327 (include
&& !cupsArrayFind(include
, ppd
->record
.scheme
)))
1333 !regexec(device_id_re
, ppd
->record
.device_id
,
1334 (size_t)(sizeof(re_matches
) / sizeof(re_matches
[0])),
1338 * Add the number of matching values from the device ID - it will be
1339 * at least 2 (manufacturer and model), and as much as 3 (command set).
1342 for (i
= 1; i
< (int)(sizeof(re_matches
) / sizeof(re_matches
[0])); i
++)
1343 if (re_matches
[i
].rm_so
>= 0)
1349 for (i
= 0; i
< PPD_MAX_LANG
; i
++)
1350 if (!ppd
->record
.languages
[i
][0])
1352 else if (!strcmp(ppd
->record
.languages
[i
], language
))
1359 if (make
&& !_cups_strcasecmp(ppd
->record
.make
, make
))
1362 if (make_and_model_re
&&
1363 !regexec(make_and_model_re
, ppd
->record
.make_and_model
,
1364 (size_t)(sizeof(re_matches
) / sizeof(re_matches
[0])),
1367 // See how much of the make-and-model string we matched...
1368 if (re_matches
[0].rm_so
== 0)
1370 if ((size_t)re_matches
[0].rm_eo
== make_and_model_len
)
1371 ppd
->matches
+= 3; // Exact match
1373 ppd
->matches
+= 2; // Prefix match
1376 ppd
->matches
++; // Infix match
1379 if (model_number_str
&& ppd
->record
.model_number
== model_number
)
1384 for (i
= 0; i
< PPD_MAX_PROD
; i
++)
1385 if (!ppd
->record
.products
[i
][0])
1387 else if (!_cups_strcasecmp(ppd
->record
.products
[i
], product
))
1392 else if (!_cups_strncasecmp(ppd
->record
.products
[i
], product
,
1402 for (i
= 0; i
< PPD_MAX_VERS
; i
++)
1403 if (!ppd
->record
.psversions
[i
][0])
1405 else if (!_cups_strcasecmp(ppd
->record
.psversions
[i
], psversion
))
1412 if (type_str
&& ppd
->record
.type
== type
)
1417 fprintf(stderr
, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
1418 ppd
->record
.name
, ppd
->matches
);
1419 cupsArrayAdd(matches
, ppd
);
1423 else if (include
|| exclude
)
1425 matches
= cupsArrayNew((cups_array_func_t
)compare_ppds
, NULL
);
1427 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByMakeModel
);
1429 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
1432 * Filter PPDs based on the include/exclude lists.
1435 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1436 ppd
->record
.type
>= PPD_TYPE_DRV
)
1439 if (cupsArrayFind(exclude
, ppd
->record
.scheme
) ||
1440 (include
&& !cupsArrayFind(include
, ppd
->record
.scheme
)))
1443 cupsArrayAdd(matches
, ppd
);
1447 matches
= PPDsByMakeModel
;
1449 for (ppd
= (ppd_info_t
*)cupsArrayFirst(matches
);
1451 ppd
= (ppd_info_t
*)cupsArrayNext(matches
))
1454 * Skip invalid PPDs...
1457 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1458 ppd
->record
.type
>= PPD_TYPE_DRV
)
1471 cupsdSendIPPHeader(IPP_OK
, request_id
);
1472 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
1473 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
1474 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
1479 fprintf(stderr
, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
1480 ppd
->record
.name
, ppd
->record
.make_and_model
);
1486 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
1489 cupsdSendIPPString(IPP_TAG_NAME
, "ppd-name", ppd
->record
.name
);
1491 if (send_natural_language
)
1493 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "ppd-natural-language",
1494 ppd
->record
.languages
[0]);
1496 for (i
= 1; i
< PPD_MAX_LANG
&& ppd
->record
.languages
[i
][0]; i
++)
1497 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "", ppd
->record
.languages
[i
]);
1501 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make", ppd
->record
.make
);
1503 if (send_make_and_model
)
1504 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make-and-model",
1505 ppd
->record
.make_and_model
);
1508 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-device-id",
1509 ppd
->record
.device_id
);
1513 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-product",
1514 ppd
->record
.products
[0]);
1516 for (i
= 1; i
< PPD_MAX_PROD
&& ppd
->record
.products
[i
][0]; i
++)
1517 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.products
[i
]);
1522 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-psversion",
1523 ppd
->record
.psversions
[0]);
1525 for (i
= 1; i
< PPD_MAX_VERS
&& ppd
->record
.psversions
[i
][0]; i
++)
1526 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.psversions
[i
]);
1530 cupsdSendIPPString(IPP_TAG_KEYWORD
, "ppd-type",
1531 PPDTypes
[ppd
->record
.type
]);
1533 if (send_model_number
)
1534 cupsdSendIPPInteger(IPP_TAG_INTEGER
, "ppd-model-number",
1535 ppd
->record
.model_number
);
1538 printf("%s (%s)\n", ppd
->record
.name
, ppd
->record
.make_and_model
);
1541 * If we have only requested the ppd-make attribute, then skip
1542 * the remaining PPDs with this make...
1545 if (cupsArrayFind(requested
, (void *)"ppd-make") &&
1546 cupsArrayCount(requested
) == 1)
1548 const char *this_make
; /* This ppd-make */
1551 for (this_make
= ppd
->record
.make
,
1552 ppd
= (ppd_info_t
*)cupsArrayNext(matches
);
1554 ppd
= (ppd_info_t
*)cupsArrayNext(matches
))
1555 if (_cups_strcasecmp(this_make
, ppd
->record
.make
))
1558 cupsArrayPrev(matches
);
1562 if (!sent_header
&& request_id
)
1564 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
1565 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
1566 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
1567 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
1571 cupsdSendIPPTrailer();
1578 * 'load_drv()' - Load the PPDs from a driver information file.
1581 static int /* O - 1 on success, 0 on failure */
1582 load_drv(const char *filename
, /* I - Actual filename */
1583 const char *name
, /* I - Name to the rest of the world */
1584 cups_file_t
*fp
, /* I - File to read from */
1585 time_t mtime
, /* I - Mod time of driver info file */
1586 off_t size
) /* I - Size of driver info file */
1588 ppdcSource
*src
; // Driver information file
1589 ppdcDriver
*d
; // Current driver
1590 ppdcAttr
*device_id
, // 1284DeviceID attribute
1591 *product
, // Current product value
1592 *ps_version
, // PSVersion attribute
1593 *cups_fax
, // cupsFax attribute
1594 *nick_name
; // NickName attribute
1595 ppdcFilter
*filter
; // Current filter
1596 ppd_info_t
*ppd
; // Current PPD
1597 int products_found
; // Number of products found
1598 char uri
[1024], // Driver URI
1599 make_model
[1024]; // Make and model
1600 int type
; // Driver type
1604 * Load the driver info file...
1607 src
= new ppdcSource(filename
, fp
);
1609 if (src
->drivers
->count
== 0)
1612 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1619 * Add a dummy entry for the file...
1622 add_ppd(name
, name
, "", "", "", "", "", "", mtime
, (size_t)size
, 0, PPD_TYPE_DRV
, "drv");
1626 * Then the drivers in the file...
1629 for (d
= (ppdcDriver
*)src
->drivers
->first();
1631 d
= (ppdcDriver
*)src
->drivers
->next())
1633 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "drv", "", "", 0,
1635 d
->file_name
? d
->file_name
->value
:
1636 d
->pc_file_name
->value
);
1638 device_id
= d
->find_attr("1284DeviceID", NULL
);
1639 ps_version
= d
->find_attr("PSVersion", NULL
);
1640 nick_name
= d
->find_attr("NickName", NULL
);
1643 strlcpy(make_model
, nick_name
->value
->value
, sizeof(make_model
));
1644 else if (_cups_strncasecmp(d
->model_name
->value
, d
->manufacturer
->value
,
1645 strlen(d
->manufacturer
->value
)))
1646 snprintf(make_model
, sizeof(make_model
), "%s %s, %s",
1647 d
->manufacturer
->value
, d
->model_name
->value
,
1650 snprintf(make_model
, sizeof(make_model
), "%s, %s", d
->model_name
->value
,
1653 if ((cups_fax
= d
->find_attr("cupsFax", NULL
)) != NULL
&&
1654 !_cups_strcasecmp(cups_fax
->value
->value
, "true"))
1655 type
= PPD_TYPE_FAX
;
1656 else if (d
->type
== PPDC_DRIVER_PS
)
1657 type
= PPD_TYPE_POSTSCRIPT
;
1658 else if (d
->type
!= PPDC_DRIVER_CUSTOM
)
1659 type
= PPD_TYPE_RASTER
;
1662 for (filter
= (ppdcFilter
*)d
->filters
->first(),
1663 type
= PPD_TYPE_POSTSCRIPT
;
1665 filter
= (ppdcFilter
*)d
->filters
->next())
1666 if (_cups_strcasecmp(filter
->mime_type
->value
, "application/vnd.cups-raster"))
1667 type
= PPD_TYPE_RASTER
;
1668 else if (_cups_strcasecmp(filter
->mime_type
->value
,
1669 "application/vnd.cups-pdf"))
1670 type
= PPD_TYPE_PDF
;
1673 for (product
= (ppdcAttr
*)d
->attrs
->first(), products_found
= 0,
1676 product
= (ppdcAttr
*)d
->attrs
->next())
1677 if (!strcmp(product
->name
->value
, "Product"))
1679 if (!products_found
)
1680 ppd
= add_ppd(name
, uri
, "en", d
->manufacturer
->value
, make_model
, device_id
? device_id
->value
->value
: "", product
->value
->value
,
1681 ps_version
? ps_version
->value
->value
: "(3010) 0", mtime
, (size_t)size
, d
->model_number
, type
, "drv");
1682 else if (products_found
< PPD_MAX_PROD
)
1683 strlcpy(ppd
->record
.products
[products_found
], product
->value
->value
, sizeof(ppd
->record
.products
[0]));
1690 if (!products_found
)
1691 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");
1701 * 'load_drivers()' - Load driver-generated PPD files.
1704 static int /* O - 1 on success, 0 on failure */
1705 load_drivers(cups_array_t
*include
, /* I - Drivers to include */
1706 cups_array_t
*exclude
) /* I - Drivers to exclude */
1708 int i
; /* Looping var */
1709 char *start
, /* Start of value */
1710 *ptr
; /* Pointer into string */
1711 const char *server_bin
, /* CUPS_SERVERBIN env variable */
1712 *scheme
, /* Scheme for this driver */
1713 *scheme_end
; /* Pointer to end of scheme */
1714 char drivers
[1024]; /* Location of driver programs */
1715 int pid
; /* Process ID for driver program */
1716 cups_file_t
*fp
; /* Pipe to driver program */
1717 cups_dir_t
*dir
; /* Directory pointer */
1718 cups_dentry_t
*dent
; /* Directory entry */
1719 char *argv
[3], /* Arguments for command */
1720 filename
[1024], /* Name of driver */
1721 line
[2048], /* Line from driver */
1722 name
[512], /* ppd-name */
1723 make
[128], /* ppd-make */
1724 make_and_model
[128], /* ppd-make-and-model */
1725 device_id
[256], /* ppd-device-id */
1726 languages
[128], /* ppd-natural-language */
1727 product
[128], /* ppd-product */
1728 psversion
[128], /* ppd-psversion */
1729 type_str
[128]; /* ppd-type */
1730 int type
; /* PPD type */
1731 ppd_info_t
*ppd
; /* Newly added PPD */
1735 * Try opening the driver directory...
1738 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
1739 server_bin
= CUPS_SERVERBIN
;
1741 snprintf(drivers
, sizeof(drivers
), "%s/driver", server_bin
);
1743 if ((dir
= cupsDirOpen(drivers
)) == NULL
)
1745 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open driver directory "
1747 drivers
, strerror(errno
));
1752 * Loop through all of the device drivers...
1755 argv
[1] = (char *)"list";
1758 while ((dent
= cupsDirRead(dir
)) != NULL
)
1761 * Only look at executable files...
1764 if (!(dent
->fileinfo
.st_mode
& 0111) || !S_ISREG(dent
->fileinfo
.st_mode
))
1768 * Include/exclude specific drivers...
1774 * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
1777 for (scheme
= (char *)cupsArrayFirst(exclude
);
1779 scheme
= (char *)cupsArrayNext(exclude
))
1781 fprintf(stderr
, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
1782 dent
->filename
, scheme
);
1783 scheme_end
= scheme
+ strlen(scheme
) - 1;
1785 if ((scheme_end
> scheme
&& *scheme_end
== '*' &&
1786 !strncmp(scheme
, dent
->filename
, (size_t)(scheme_end
- scheme
))) ||
1787 !strcmp(scheme
, dent
->filename
))
1789 fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr
);
1801 * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
1804 for (scheme
= (char *)cupsArrayFirst(include
);
1806 scheme
= (char *)cupsArrayNext(include
))
1808 fprintf(stderr
, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
1809 dent
->filename
, scheme
);
1810 scheme_end
= scheme
+ strlen(scheme
) - 1;
1812 if ((scheme_end
> scheme
&& *scheme_end
== '*' &&
1813 !strncmp(scheme
, dent
->filename
, (size_t)(scheme_end
- scheme
))) ||
1814 !strcmp(scheme
, dent
->filename
))
1816 fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr
);
1825 scheme
= dent
->filename
;
1828 * Run the driver with no arguments and collect the output...
1831 snprintf(filename
, sizeof(filename
), "%s/%s", drivers
, dent
->filename
);
1833 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_PROGRAM
, !geteuid(),
1834 _cupsFileCheckFilter
, NULL
))
1837 argv
[0] = dent
->filename
;
1839 if ((fp
= cupsdPipeCommand(&pid
, filename
, argv
, 0)) != NULL
)
1841 while (cupsFileGets(fp
, line
, sizeof(line
)))
1844 * Each line is of the form:
1846 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
1847 * "ppd-device-id" "ppd-product" "ppd-psversion"
1850 device_id
[0] = '\0';
1852 psversion
[0] = '\0';
1853 strlcpy(type_str
, "postscript", sizeof(type_str
));
1855 if (sscanf(line
, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
1856 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%255[^\"]\""
1857 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1858 "%*[ \t]\"%127[^\"]\"",
1859 name
, languages
, make
, make_and_model
,
1860 device_id
, product
, psversion
, type_str
) < 4)
1863 * Bad format; strip trailing newline and write an error message.
1866 if (line
[strlen(line
) - 1] == '\n')
1867 line
[strlen(line
) - 1] = '\0';
1869 fprintf(stderr
, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1870 dent
->filename
, line
);
1876 * Add the device to the array of available devices...
1879 if ((start
= strchr(languages
, ',')) != NULL
)
1883 type
< (int)(sizeof(PPDTypes
) / sizeof(PPDTypes
[0]));
1885 if (!strcmp(type_str
, PPDTypes
[type
]))
1888 if (type
>= (int)(sizeof(PPDTypes
) / sizeof(PPDTypes
[0])))
1891 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
1893 type
= PPD_TYPE_UNKNOWN
;
1896 ppd
= add_ppd(filename
, name
, languages
, make
, make_and_model
,
1897 device_id
, product
, psversion
, 0, 0, 0, type
, scheme
);
1906 if (start
&& *start
)
1908 for (i
= 1; i
< PPD_MAX_LANG
&& *start
; i
++)
1910 if ((ptr
= strchr(start
, ',')) != NULL
)
1913 ptr
= start
+ strlen(start
);
1915 strlcpy(ppd
->record
.languages
[i
], start
,
1916 sizeof(ppd
->record
.languages
[0]));
1922 fprintf(stderr
, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1930 fprintf(stderr
, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1931 filename
, strerror(errno
));
1941 * 'load_ppd()' - Load a PPD file.
1945 load_ppd(const char *filename
, /* I - Real filename */
1946 const char *name
, /* I - Virtual filename */
1947 const char *scheme
, /* I - PPD scheme */
1948 struct stat
*fileinfo
, /* I - File information */
1949 ppd_info_t
*ppd
, /* I - Existing PPD file or NULL */
1950 cups_file_t
*fp
, /* I - File to read from */
1951 off_t end
) /* I - End of file position or 0 */
1953 int i
; /* Looping var */
1954 char line
[256], /* Line from file */
1955 *ptr
, /* Pointer into line */
1956 lang_version
[64], /* PPD LanguageVersion */
1957 lang_encoding
[64], /* PPD LanguageEncoding */
1958 country
[64], /* Country code */
1959 manufacturer
[256], /* Manufacturer */
1960 make_model
[256], /* Make and Model */
1961 model_name
[256], /* ModelName */
1962 nick_name
[256], /* NickName */
1963 device_id
[256], /* 1284DeviceID */
1964 product
[256], /* Product */
1965 psversion
[256], /* PSVersion */
1966 temp
[512]; /* Temporary make and model */
1967 int install_group
, /* In the installable options group? */
1968 model_number
, /* cupsModelNumber */
1969 type
; /* ppd-type */
1970 cups_array_t
*products
, /* Product array */
1971 *psversions
, /* PSVersion array */
1972 *cups_languages
; /* cupsLanguages array */
1973 int new_ppd
; /* Is this a new PPD? */
1974 struct /* LanguageVersion translation table */
1976 const char *version
, /* LanguageVersion string */
1977 *language
; /* Language code */
1980 { "chinese", "zh" },
1984 { "english", "en" },
1985 { "finnish", "fi" },
1989 { "hungarian", "hu" },
1990 { "italian", "it" },
1991 { "japanese", "ja" },
1993 { "norwegian", "no" },
1995 { "portuguese", "pt" },
1996 { "russian", "ru" },
1997 { "simplified chinese", "zh_CN" },
1999 { "spanish", "es" },
2000 { "swedish", "sv" },
2001 { "traditional chinese", "zh_TW" },
2007 * Now read until we get the required fields...
2010 cups_languages
= cupsArrayNew(NULL
, NULL
);
2011 products
= cupsArrayNew(NULL
, NULL
);
2012 psversions
= cupsArrayNew(NULL
, NULL
);
2014 model_name
[0] = '\0';
2015 nick_name
[0] = '\0';
2016 manufacturer
[0] = '\0';
2017 device_id
[0] = '\0';
2018 lang_encoding
[0] = '\0';
2019 strlcpy(lang_version
, "en", sizeof(lang_version
));
2022 type
= PPD_TYPE_POSTSCRIPT
;
2024 while ((end
== 0 || cupsFileTell(fp
) < end
) &&
2025 cupsFileGets(fp
, line
, sizeof(line
)))
2027 if (!strncmp(line
, "*Manufacturer:", 14))
2028 sscanf(line
, "%*[^\"]\"%255[^\"]", manufacturer
);
2029 else if (!strncmp(line
, "*ModelName:", 11))
2030 sscanf(line
, "%*[^\"]\"%127[^\"]", model_name
);
2031 else if (!strncmp(line
, "*LanguageEncoding:", 18))
2032 sscanf(line
, "%*[^:]:%63s", lang_encoding
);
2033 else if (!strncmp(line
, "*LanguageVersion:", 17))
2034 sscanf(line
, "%*[^:]:%63s", lang_version
);
2035 else if (!strncmp(line
, "*NickName:", 10))
2036 sscanf(line
, "%*[^\"]\"%255[^\"]", nick_name
);
2037 else if (!_cups_strncasecmp(line
, "*1284DeviceID:", 14))
2039 sscanf(line
, "%*[^\"]\"%255[^\"]", device_id
);
2041 // Make sure device ID ends with a semicolon...
2042 if (device_id
[0] && device_id
[strlen(device_id
) - 1] != ';')
2043 strlcat(device_id
, ";", sizeof(device_id
));
2045 else if (!strncmp(line
, "*Product:", 9))
2047 if (sscanf(line
, "%*[^\"]\"(%255[^\"]", product
) == 1)
2050 * Make sure the value ends with a right parenthesis - can't stop at
2051 * the first right paren since the product name may contain escaped
2055 ptr
= product
+ strlen(product
) - 1;
2056 if (ptr
> product
&& *ptr
== ')')
2059 * Yes, ends with a parenthesis, so remove it from the end and
2060 * add the product to the list...
2064 cupsArrayAdd(products
, strdup(product
));
2068 else if (!strncmp(line
, "*PSVersion:", 11))
2070 sscanf(line
, "%*[^\"]\"%255[^\"]", psversion
);
2071 cupsArrayAdd(psversions
, strdup(psversion
));
2073 else if (!strncmp(line
, "*cupsLanguages:", 15))
2075 char *start
; /* Start of language */
2078 for (start
= line
+ 15; *start
&& isspace(*start
& 255); start
++);
2080 if (*start
++ == '\"')
2084 for (ptr
= start
+ 1;
2085 *ptr
&& *ptr
!= '\"' && !isspace(*ptr
& 255);
2092 while (isspace(*ptr
& 255))
2096 cupsArrayAdd(cups_languages
, strdup(start
));
2101 else if (!strncmp(line
, "*cupsFax:", 9))
2103 for (ptr
= line
+ 9; isspace(*ptr
& 255); ptr
++);
2105 if (!_cups_strncasecmp(ptr
, "true", 4))
2106 type
= PPD_TYPE_FAX
;
2108 else if ((!strncmp(line
, "*cupsFilter:", 12) || !strncmp(line
, "*cupsFilter2:", 13)) && type
== PPD_TYPE_POSTSCRIPT
)
2110 if (strstr(line
+ 12, "application/vnd.cups-raster"))
2111 type
= PPD_TYPE_RASTER
;
2112 else if (strstr(line
+ 12, "application/vnd.cups-pdf"))
2113 type
= PPD_TYPE_PDF
;
2114 else if (strstr(line
+ 12, "application/amf") ||
2115 strstr(line
+ 12, "application/g-code") ||
2116 strstr(line
+ 12, "application/sla"))
2117 type
= PPD_TYPE_OBJECT_ANY
;
2119 else if (!strncmp(line
, "*cups3DWorkflows:", 17))
2121 int is_direct
= strstr(line
+ 17, "direct") != NULL
;
2122 int is_storage
= strstr(line
+ 17, "storage") != NULL
;
2124 if (is_direct
&& !is_storage
)
2125 type
= PPD_TYPE_OBJECT_DIRECT
;
2126 if (!is_direct
&& is_storage
)
2127 type
= PPD_TYPE_OBJECT_STORAGE
;
2129 type
= PPD_TYPE_OBJECT_ANY
;
2131 else if (!strncmp(line
, "*cupsModelNumber:", 17))
2132 sscanf(line
, "*cupsModelNumber:%d", &model_number
);
2133 else if (!strncmp(line
, "*OpenGroup: Installable", 23))
2135 else if (!strncmp(line
, "*CloseGroup:", 12))
2137 else if (!strncmp(line
, "*OpenUI", 7))
2140 * Stop early if we have a NickName or ModelName attributes
2141 * before the first non-installable OpenUI...
2144 if (!install_group
&& (model_name
[0] || nick_name
[0]) &&
2145 cupsArrayCount(products
) > 0 && cupsArrayCount(psversions
) > 0)
2151 * See if we got all of the required info...
2155 cupsCharsetToUTF8((cups_utf8_t
*)make_model
, nick_name
,
2156 sizeof(make_model
), _ppdGetEncoding(lang_encoding
));
2158 strlcpy(make_model
, model_name
, sizeof(make_model
));
2160 while (isspace(make_model
[0] & 255))
2161 _cups_strcpy(make_model
, make_model
+ 1);
2163 if (!make_model
[0] || cupsArrayCount(products
) == 0 ||
2164 cupsArrayCount(psversions
) == 0)
2167 * We don't have all the info needed, so skip this file...
2171 fprintf(stderr
, "WARNING: Missing NickName and ModelName in %s!\n",
2174 if (cupsArrayCount(products
) == 0)
2175 fprintf(stderr
, "WARNING: Missing Product in %s!\n", filename
);
2177 if (cupsArrayCount(psversions
) == 0)
2178 fprintf(stderr
, "WARNING: Missing PSVersion in %s!\n", filename
);
2180 free_array(products
);
2181 free_array(psversions
);
2182 free_array(cups_languages
);
2188 cupsArrayAdd(products
, strdup(model_name
));
2191 * Normalize the make and model string...
2194 while (isspace(manufacturer
[0] & 255))
2195 _cups_strcpy(manufacturer
, manufacturer
+ 1);
2197 if (!_cups_strncasecmp(make_model
, manufacturer
, strlen(manufacturer
)))
2198 strlcpy(temp
, make_model
, sizeof(temp
));
2200 snprintf(temp
, sizeof(temp
), "%s %s", manufacturer
, make_model
);
2202 _ppdNormalizeMakeAndModel(temp
, make_model
, sizeof(make_model
));
2205 * See if we got a manufacturer...
2208 if (!manufacturer
[0] || !strcmp(manufacturer
, "ESP"))
2211 * Nope, copy the first part of the make and model then...
2214 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
2217 * Truncate at the first space, dash, or slash, or make the
2218 * manufacturer "Other"...
2221 for (ptr
= manufacturer
; *ptr
; ptr
++)
2222 if (*ptr
== ' ' || *ptr
== '-' || *ptr
== '/')
2225 if (*ptr
&& ptr
> manufacturer
)
2228 strlcpy(manufacturer
, "Other", sizeof(manufacturer
));
2230 else if (!_cups_strncasecmp(manufacturer
, "LHAG", 4) ||
2231 !_cups_strncasecmp(manufacturer
, "linotype", 8))
2232 strlcpy(manufacturer
, "LHAG", sizeof(manufacturer
));
2233 else if (!_cups_strncasecmp(manufacturer
, "Hewlett", 7))
2234 strlcpy(manufacturer
, "HP", sizeof(manufacturer
));
2237 * Fix the lang_version as needed...
2240 if ((ptr
= strchr(lang_version
, '-')) != NULL
)
2242 else if ((ptr
= strchr(lang_version
, '_')) != NULL
)
2248 * Setup the country suffix...
2252 _cups_strcpy(country
+ 1, ptr
);
2257 * No country suffix...
2263 for (i
= 0; i
< (int)(sizeof(languages
) / sizeof(languages
[0])); i
++)
2264 if (!_cups_strcasecmp(languages
[i
].version
, lang_version
))
2267 if (i
< (int)(sizeof(languages
) / sizeof(languages
[0])))
2270 * Found a known language...
2273 snprintf(lang_version
, sizeof(lang_version
), "%s%s",
2274 languages
[i
].language
, country
);
2279 * Unknown language; use "xx"...
2282 strlcpy(lang_version
, "xx", sizeof(lang_version
));
2286 * Record the PPD file...
2294 * Add new PPD file...
2297 fprintf(stderr
, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name
);
2299 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
);
2307 * Update existing record...
2310 fprintf(stderr
, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name
);
2312 memset(ppd
, 0, sizeof(ppd_info_t
));
2315 ppd
->record
.mtime
= fileinfo
->st_mtime
;
2316 ppd
->record
.size
= fileinfo
->st_size
;
2317 ppd
->record
.model_number
= model_number
;
2318 ppd
->record
.type
= type
;
2320 strlcpy(ppd
->record
.filename
, name
, sizeof(ppd
->record
.filename
));
2321 strlcpy(ppd
->record
.name
, name
, sizeof(ppd
->record
.name
));
2322 strlcpy(ppd
->record
.languages
[0], lang_version
,
2323 sizeof(ppd
->record
.languages
[0]));
2324 strlcpy(ppd
->record
.products
[0], (char *)cupsArrayFirst(products
),
2325 sizeof(ppd
->record
.products
[0]));
2326 strlcpy(ppd
->record
.psversions
[0], (char *)cupsArrayFirst(psversions
),
2327 sizeof(ppd
->record
.psversions
[0]));
2328 strlcpy(ppd
->record
.make
, manufacturer
, sizeof(ppd
->record
.make
));
2329 strlcpy(ppd
->record
.make_and_model
, make_model
,
2330 sizeof(ppd
->record
.make_and_model
));
2331 strlcpy(ppd
->record
.device_id
, device_id
, sizeof(ppd
->record
.device_id
));
2332 strlcpy(ppd
->record
.scheme
, scheme
, sizeof(ppd
->record
.scheme
));
2336 * Add remaining products, versions, and languages...
2340 i
< PPD_MAX_PROD
&& (ptr
= (char *)cupsArrayNext(products
)) != NULL
;
2342 strlcpy(ppd
->record
.products
[i
], ptr
,
2343 sizeof(ppd
->record
.products
[0]));
2346 i
< PPD_MAX_VERS
&& (ptr
= (char *)cupsArrayNext(psversions
)) != NULL
;
2348 strlcpy(ppd
->record
.psversions
[i
], ptr
,
2349 sizeof(ppd
->record
.psversions
[0]));
2351 for (i
= 1, ptr
= (char *)cupsArrayFirst(cups_languages
);
2352 i
< PPD_MAX_LANG
&& ptr
;
2353 i
++, ptr
= (char *)cupsArrayNext(cups_languages
))
2354 strlcpy(ppd
->record
.languages
[i
], ptr
,
2355 sizeof(ppd
->record
.languages
[0]));
2358 * Free products, versions, and languages...
2361 free_array(cups_languages
);
2362 free_array(products
);
2363 free_array(psversions
);
2370 * 'load_ppds()' - Load PPD files recursively.
2373 static int /* O - 1 on success, 0 on failure */
2374 load_ppds(const char *d
, /* I - Actual directory */
2375 const char *p
, /* I - Virtual path in name */
2376 int descend
) /* I - Descend into directories? */
2378 struct stat dinfo
, /* Directory information */
2379 *dinfoptr
; /* Pointer to match */
2380 cups_file_t
*fp
; /* Pointer to file */
2381 cups_dir_t
*dir
; /* Directory pointer */
2382 cups_dentry_t
*dent
; /* Directory entry */
2383 char filename
[1024], /* Name of PPD or directory */
2384 line
[256], /* Line from file */
2385 *ptr
, /* Pointer into name */
2386 name
[128]; /* Name of PPD file */
2387 ppd_info_t
*ppd
, /* New PPD file */
2388 key
; /* Search key */
2392 * See if we've loaded this directory before...
2395 if (stat(d
, &dinfo
))
2397 if (errno
!= ENOENT
)
2398 fprintf(stderr
, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d
,
2403 else if (cupsArrayFind(Inodes
, &dinfo
))
2405 fprintf(stderr
, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
2411 * Nope, add it to the Inodes array and continue...
2414 dinfoptr
= (struct stat
*)malloc(sizeof(struct stat
));
2415 memcpy(dinfoptr
, &dinfo
, sizeof(struct stat
));
2416 cupsArrayAdd(Inodes
, dinfoptr
);
2419 * Check permissions...
2422 if (_cupsFileCheck(d
, _CUPS_FILE_CHECK_DIRECTORY
, !geteuid(),
2423 _cupsFileCheckFilter
, NULL
))
2426 if ((dir
= cupsDirOpen(d
)) == NULL
)
2428 if (errno
!= ENOENT
)
2430 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
2431 d
, strerror(errno
));
2436 fprintf(stderr
, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d
);
2438 while ((dent
= cupsDirRead(dir
)) != NULL
)
2441 * Skip files/directories starting with "."...
2444 if (dent
->filename
[0] == '.')
2448 * See if this is a file...
2451 snprintf(filename
, sizeof(filename
), "%s/%s", d
, dent
->filename
);
2454 snprintf(name
, sizeof(name
), "%s/%s", p
, dent
->filename
);
2456 strlcpy(name
, dent
->filename
, sizeof(name
));
2458 if (S_ISDIR(dent
->fileinfo
.st_mode
))
2461 * Do subdirectory...
2466 if (!load_ppds(filename
, name
, 1))
2472 else if ((ptr
= filename
+ strlen(filename
) - 14) > filename
&&
2473 !strcmp(ptr
, ".printerDriver"))
2476 * Load PPDs in a printer driver bundle.
2479 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_DIRECTORY
, !geteuid(),
2480 _cupsFileCheckFilter
, NULL
))
2483 strlcat(filename
, "/Contents/Resources/PPDs", sizeof(filename
));
2484 strlcat(name
, "/Contents/Resources/PPDs", sizeof(name
));
2486 load_ppds(filename
, name
, 0);
2491 else if (strstr(filename
, ".plist"))
2494 * Skip plist files in the PPDs directory...
2499 else if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_FILE_ONLY
, !geteuid(),
2500 _cupsFileCheckFilter
, NULL
))
2504 * See if this file has been scanned before...
2507 strlcpy(key
.record
.filename
, name
, sizeof(key
.record
.filename
));
2508 strlcpy(key
.record
.name
, name
, sizeof(key
.record
.name
));
2510 ppd
= (ppd_info_t
*)cupsArrayFind(PPDsByName
, &key
);
2513 ppd
->record
.size
== dent
->fileinfo
.st_size
&&
2514 ppd
->record
.mtime
== dent
->fileinfo
.st_mtime
)
2517 * Rewind to the first entry for this file...
2520 while ((ppd
= (ppd_info_t
*)cupsArrayPrev(PPDsByName
)) != NULL
&&
2521 !strcmp(ppd
->record
.filename
, name
));
2524 * Then mark all of the matches for this file as found...
2527 while ((ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
)) != NULL
&&
2528 !strcmp(ppd
->record
.filename
, name
))
2535 * No, file is new/changed, so re-scan it...
2538 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2542 * Now see if this is a PPD file...
2546 cupsFileGets(fp
, line
, sizeof(line
));
2548 if (!strncmp(line
, "*PPD-Adobe:", 11))
2554 load_ppd(filename
, name
, "file", &dent
->fileinfo
, ppd
, fp
, 0);
2559 * Nope, treat it as a driver information file or archive...
2564 if ((ptr
= strstr(filename
, ".tar")) != NULL
&&
2565 (!strcmp(ptr
, ".tar") || !strcmp(ptr
, ".tar.gz")))
2566 load_tar(filename
, name
, fp
, dent
->fileinfo
.st_mtime
,
2567 dent
->fileinfo
.st_size
);
2569 load_drv(filename
, name
, fp
, dent
->fileinfo
.st_mtime
,
2570 dent
->fileinfo
.st_size
);
2587 * 'load_ppds_dat()' - Load the ppds.dat file.
2591 load_ppds_dat(char *filename
, /* I - Filename buffer */
2592 size_t filesize
, /* I - Size of filename buffer */
2593 int verbose
) /* I - Be verbose? */
2595 ppd_info_t
*ppd
; /* Current PPD file */
2596 cups_file_t
*fp
; /* ppds.dat file */
2597 struct stat fileinfo
; /* ppds.dat information */
2598 const char *cups_cachedir
; /* CUPS_CACHEDIR environment variable */
2601 PPDsByName
= cupsArrayNew((cups_array_func_t
)compare_names
, NULL
);
2602 PPDsByMakeModel
= cupsArrayNew((cups_array_func_t
)compare_ppds
, NULL
);
2607 if ((cups_cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
2608 cups_cachedir
= CUPS_CACHEDIR
;
2610 snprintf(filename
, filesize
, "%s/ppds.dat", cups_cachedir
);
2613 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
2616 * See if we have the right sync word...
2619 unsigned ppdsync
; /* Sync word */
2620 int num_ppds
; /* Number of PPDs */
2622 if (cupsFileRead(fp
, (char *)&ppdsync
, sizeof(ppdsync
)) == sizeof(ppdsync
) &&
2623 ppdsync
== PPD_SYNC
&&
2624 !stat(filename
, &fileinfo
) &&
2625 (((size_t)fileinfo
.st_size
- sizeof(ppdsync
)) % sizeof(ppd_rec_t
)) == 0 &&
2626 (num_ppds
= ((size_t)fileinfo
.st_size
- sizeof(ppdsync
)) / sizeof(ppd_rec_t
)) > 0)
2629 * We have a ppds.dat file, so read it!
2632 for (; num_ppds
> 0; num_ppds
--)
2634 if ((ppd
= (ppd_info_t
*)calloc(1, sizeof(ppd_info_t
))) == NULL
)
2637 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
2642 if (cupsFileRead(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
)) > 0)
2644 cupsArrayAdd(PPDsByName
, ppd
);
2645 cupsArrayAdd(PPDsByMakeModel
, ppd
);
2655 fprintf(stderr
, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
2656 filename
, cupsArrayCount(PPDsByName
));
2665 * 'load_tar()' - Load archived PPD files.
2668 static int /* O - 1 on success, 0 on failure */
2669 load_tar(const char *filename
, /* I - Actual filename */
2670 const char *name
, /* I - Name to the rest of the world */
2671 cups_file_t
*fp
, /* I - File to read from */
2672 time_t mtime
, /* I - Mod time of driver info file */
2673 off_t size
) /* I - Size of driver info file */
2675 char curname
[256], /* Current archive file name */
2676 uri
[1024]; /* Virtual file URI */
2677 const char *curext
; /* Extension on file */
2678 struct stat curinfo
; /* Current archive file information */
2679 off_t next
; /* Position for next header */
2683 * Add a dummy entry for the file...
2688 add_ppd(name
, name
, "", "", "", "", "", "", mtime
, (size_t)size
, 0, PPD_TYPE_ARCHIVE
, "file");
2692 * Scan for PPDs in the archive...
2695 while (read_tar(fp
, curname
, sizeof(curname
), &curinfo
))
2697 next
= cupsFileTell(fp
) + ((curinfo
.st_size
+ TAR_BLOCK
- 1) &
2700 if ((curext
= strrchr(curname
, '.')) != NULL
&&
2701 !_cups_strcasecmp(curext
, ".ppd"))
2703 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "file", "", "",
2704 0, "/%s/%s", name
, curname
);
2705 load_ppd(name
, uri
, "file", &curinfo
, NULL
, fp
, next
);
2708 if (cupsFileTell(fp
) != next
)
2709 cupsFileSeek(fp
, next
);
2717 * 'read_tar()' - Read a file header from an archive.
2719 * This function skips all directories and special files.
2722 static int /* O - 1 if found, 0 on EOF */
2723 read_tar(cups_file_t
*fp
, /* I - Archive to read */
2724 char *name
, /* I - Filename buffer */
2725 size_t namesize
, /* I - Size of filename buffer */
2726 struct stat
*info
) /* O - File information */
2728 tar_rec_t record
; /* Record from file */
2731 while (cupsFileRead(fp
, (char *)&record
, sizeof(record
)) == sizeof(record
))
2734 * Check for a valid tar header...
2737 if (memcmp(record
.header
.magic
, TAR_MAGIC
, 6) ||
2738 memcmp(record
.header
.version
, TAR_VERSION
, 2))
2740 if (record
.header
.magic
[0] ||
2741 memcmp(record
.header
.magic
, record
.header
.magic
+ 1, 5))
2742 fputs("ERROR: [cups-driverd] Bad tar magic/version.\n", stderr
);
2747 * Ignore non-files...
2750 if (record
.header
.linkflag
!= TAR_OLDNORMAL
&&
2751 record
.header
.linkflag
!= TAR_NORMAL
)
2755 * Grab size and name from tar header and return...
2758 if (record
.header
.prefix
[0])
2759 snprintf(name
, namesize
, "%s/%s", record
.header
.prefix
,
2760 record
.header
.pathname
);
2762 strlcpy(name
, record
.header
.pathname
, namesize
);
2764 info
->st_mtime
= strtol(record
.header
.mtime
, NULL
, 8);
2765 info
->st_size
= strtoll(record
.header
.size
, NULL
, 8);
2775 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2779 static regex_t
* /* O - Regular expression */
2780 regex_device_id(const char *device_id
) /* I - IEEE-1284 device ID */
2782 char res
[2048], /* Regular expression string */
2783 *ptr
; /* Pointer into string */
2784 regex_t
*re
; /* Regular expression */
2785 int cmd
; /* Command set string? */
2788 fprintf(stderr
, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id
);
2791 * Scan the device ID string and insert class, command set, manufacturer, and
2792 * model attributes to match. We assume that the device ID in the PPD and the
2793 * device ID reported by the device itself use the same attribute names and
2794 * order of attributes.
2799 while (*device_id
&& ptr
< (res
+ sizeof(res
) - 6))
2801 cmd
= !_cups_strncasecmp(device_id
, "COMMAND SET:", 12) ||
2802 !_cups_strncasecmp(device_id
, "CMD:", 4);
2804 if (cmd
|| !_cups_strncasecmp(device_id
, "MANUFACTURER:", 13) ||
2805 !_cups_strncasecmp(device_id
, "MFG:", 4) ||
2806 !_cups_strncasecmp(device_id
, "MFR:", 4) ||
2807 !_cups_strncasecmp(device_id
, "MODEL:", 6) ||
2808 !_cups_strncasecmp(device_id
, "MDL:", 4))
2818 while (*device_id
&& *device_id
!= ';' && ptr
< (res
+ sizeof(res
) - 8))
2820 if (strchr("[]{}().*\\|", *device_id
))
2822 if (*device_id
== ':')
2828 *ptr
++ = *device_id
++;
2833 *ptr
++ = *device_id
++;
2836 if (*device_id
== ';' || !*device_id
)
2850 else if ((device_id
= strchr(device_id
, ';')) == NULL
)
2858 fprintf(stderr
, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res
);
2861 * Compile the regular expression and return...
2864 if (res
[0] && (re
= (regex_t
*)calloc(1, sizeof(regex_t
))) != NULL
)
2866 if (!regcomp(re
, res
, REG_EXTENDED
| REG_ICASE
))
2868 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr
);
2880 * 'regex_string()' - Construct a regular expression to compare a simple string.
2883 static regex_t
* /* O - Regular expression */
2884 regex_string(const char *s
) /* I - String to compare */
2886 char res
[2048], /* Regular expression string */
2887 *ptr
; /* Pointer into string */
2888 regex_t
*re
; /* Regular expression */
2891 fprintf(stderr
, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s
);
2894 * Convert the string to a regular expression, escaping special characters
2900 while (*s
&& ptr
< (res
+ sizeof(res
) - 2))
2902 if (strchr("[]{}().*\\", *s
))
2910 fprintf(stderr
, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res
);
2913 * Create a case-insensitive regular expression...
2916 if (res
[0] && (re
= (regex_t
*)calloc(1, sizeof(regex_t
))) != NULL
)
2918 if (!regcomp(re
, res
, REG_ICASE
))
2920 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr
);