]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-driverd.cxx
4 * PPD/driver support for the Common UNIX Printing System (CUPS).
6 * This program handles listing and installing both static PPD files
7 * in CUPS_DATADIR/model and dynamically generated PPD files using
8 * the driver helper programs in CUPS_SERVERBIN/driver.
10 * Copyright 2007-2008 by Apple Inc.
11 * Copyright 1997-2007 by Easy Software Products.
13 * These coded instructions, statements, and computer programs are the
14 * property of Apple Inc. and are protected by Federal copyright
15 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
16 * which should have been included with this file. If this file is
17 * file is missing or damaged, see the license at "http://www.cups.org/".
21 * main() - Scan for drivers and return an IPP response.
22 * add_ppd() - Add a PPD file.
23 * cat_drv() - Generate a PPD from a driver info file.
24 * cat_ppd() - Copy a PPD file to stdout.
25 * copy_static() - Copy a static PPD file to stdout.
26 * compare_names() - Compare PPD filenames for sorting.
27 * compare_ppds() - Compare PPD file make and model names for sorting.
28 * free_array() - Free an array of strings.
29 * list_ppds() - List PPD files.
30 * load_ppds() - Load PPD files recursively.
31 * load_drv() - Load the PPDs from a driver information file.
32 * load_drivers() - Load driver-generated PPD files.
36 * Include necessary headers...
41 #include <cups/transcode.h>
42 #include <cups/ppd-private.h>
43 #include <ppdc/ppdc.h>
50 #define PPD_SYNC 0x50504435 /* Sync word for ppds.dat (PPD5) */
51 #define PPD_MAX_LANG 32 /* Maximum languages */
52 #define PPD_MAX_PROD 8 /* Maximum products */
53 #define PPD_MAX_VERS 8 /* Maximum versions */
55 #define PPD_TYPE_POSTSCRIPT 0 /* PostScript PPD */
56 #define PPD_TYPE_PDF 1 /* PDF PPD */
57 #define PPD_TYPE_RASTER 2 /* CUPS raster PPD */
58 #define PPD_TYPE_FAX 3 /* Facsimile/MFD PPD */
59 #define PPD_TYPE_UNKNOWN 4 /* Other/hybrid PPD */
60 #define PPD_TYPE_DRV 5 /* Driver info file */
62 static const char * const ppd_types
[] = /* ppd-type values */
73 * PPD information structures...
76 typedef struct /**** PPD record ****/
78 time_t mtime
; /* Modification time */
79 off_t size
; /* Size in bytes */
80 int model_number
; /* cupsModelNumber */
81 int type
; /* ppd-type */
82 char filename
[512], /* Filename */
83 name
[512], /* PPD name */
84 languages
[PPD_MAX_LANG
][6],
85 /* LanguageVersion/cupsLanguages */
86 products
[PPD_MAX_PROD
][128],
88 psversions
[PPD_MAX_VERS
][32],
89 /* PSVersion strings */
90 make
[128], /* Manufacturer */
91 make_and_model
[128], /* NickName/ModelName */
92 device_id
[256]; /* IEEE 1284 Device ID */
95 typedef struct /**** In-memory record ****/
97 int found
; /* 1 if PPD is found */
98 ppd_rec_t record
; /* PPDs.dat record */
106 cups_array_t
*PPDsByName
= NULL
, /* PPD files sorted by filename and name */
107 *PPDsByMakeModel
= NULL
;/* PPD files sorted by make and model */
108 int ChangedPPD
; /* Did we change the PPD database? */
115 static ppd_info_t
*add_ppd(const char *filename
, const char *name
,
116 const char *language
, const char *make
,
117 const char *make_and_model
,
118 const char *device_id
, const char *product
,
119 const char *psversion
, time_t mtime
,
120 size_t size
, int model_number
, int type
);
121 static int cat_drv(const char *name
, int request_id
);
122 static int cat_ppd(const char *name
, int request_id
);
123 static int cat_static(const char *name
, int request_id
);
124 static int compare_names(const ppd_info_t
*p0
,
125 const ppd_info_t
*p1
);
126 static int compare_ppds(const ppd_info_t
*p0
,
127 const ppd_info_t
*p1
);
128 static void free_array(cups_array_t
*a
);
129 static int list_ppds(int request_id
, int limit
, const char *opt
);
130 static int load_drivers(void);
131 static int load_drv(const char *filename
, const char *name
,
132 cups_file_t
*fp
, time_t mtime
, off_t size
);
133 static int load_ppds(const char *d
, const char *p
, int descend
);
137 * 'main()' - Scan for drivers and return an IPP response.
141 * cups-driverd request_id limit options
144 int /* O - Exit code */
145 main(int argc
, /* I - Number of command-line args */
146 char *argv
[]) /* I - Command-line arguments */
149 * Install or list PPDs...
152 if (argc
== 3 && !strcmp(argv
[1], "cat"))
153 return (cat_ppd(argv
[2], 0));
154 else if (argc
== 4 && !strcmp(argv
[1], "get"))
155 return (cat_ppd(argv
[3], atoi(argv
[2])));
156 else if (argc
== 5 && !strcmp(argv
[1], "list"))
157 return (list_ppds(atoi(argv
[2]), atoi(argv
[3]), argv
[4]));
160 fputs("Usage: cups-driverd cat ppd-name\n", stderr
);
161 fputs("Usage: cups-driverd get request_id ppd-name\n", stderr
);
162 fputs("Usage: cups-driverd list request_id limit options\n", stderr
);
169 * 'add_ppd()' - Add a PPD file.
172 static ppd_info_t
* /* O - PPD */
173 add_ppd(const char *filename
, /* I - PPD filename */
174 const char *name
, /* I - PPD name */
175 const char *language
, /* I - LanguageVersion */
176 const char *make
, /* I - Manufacturer */
177 const char *make_and_model
, /* I - NickName/ModelName */
178 const char *device_id
, /* I - 1284DeviceID */
179 const char *product
, /* I - Product */
180 const char *psversion
, /* I - PSVersion */
181 time_t mtime
, /* I - Modification time */
182 size_t size
, /* I - File size */
183 int model_number
, /* I - Model number */
184 int type
) /* I - Driver type */
186 ppd_info_t
*ppd
; /* PPD */
187 char *recommended
; /* Foomatic driver string */
191 * Add a new PPD file...
194 if ((ppd
= (ppd_info_t
*)calloc(1, sizeof(ppd_info_t
))) == NULL
)
197 "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n",
198 cupsArrayCount(PPDsByName
));
203 * Zero-out the PPD data and copy the values over...
207 ppd
->record
.mtime
= mtime
;
208 ppd
->record
.size
= size
;
209 ppd
->record
.model_number
= model_number
;
210 ppd
->record
.type
= type
;
212 strlcpy(ppd
->record
.filename
, filename
, sizeof(ppd
->record
.filename
));
213 strlcpy(ppd
->record
.name
, name
, sizeof(ppd
->record
.name
));
214 strlcpy(ppd
->record
.languages
[0], language
,
215 sizeof(ppd
->record
.languages
[0]));
216 strlcpy(ppd
->record
.products
[0], product
, sizeof(ppd
->record
.products
[0]));
217 strlcpy(ppd
->record
.psversions
[0], psversion
,
218 sizeof(ppd
->record
.psversions
[0]));
219 strlcpy(ppd
->record
.make
, make
, sizeof(ppd
->record
.make
));
220 strlcpy(ppd
->record
.make_and_model
, make_and_model
,
221 sizeof(ppd
->record
.make_and_model
));
222 strlcpy(ppd
->record
.device_id
, device_id
, sizeof(ppd
->record
.device_id
));
225 * Strip confusing (and often wrong) "recommended" suffix added by
226 * Foomatic drivers...
229 if ((recommended
= strstr(ppd
->record
.make_and_model
,
230 " (recommended)")) != NULL
)
234 * Add the PPD to the PPD arrays...
237 cupsArrayAdd(PPDsByName
, ppd
);
238 cupsArrayAdd(PPDsByMakeModel
, ppd
);
241 * Return the new PPD pointer...
249 * 'cat_drv()' - Generate a PPD from a driver info file.
252 static int /* O - Exit code */
253 cat_drv(const char *name
, /* I - PPD name */
254 int request_id
) /* I - Request ID for response? */
256 const char *datadir
; // CUPS_DATADIR env var
257 ppdcSource
*src
; // PPD source file data
258 ppdcDriver
*d
; // Current driver
259 cups_file_t
*out
; // Stdout via CUPS file API
260 char message
[2048], // status-message
261 filename
[1024], // Full path to .drv file(s)
262 scheme
[32], // URI scheme ("drv")
263 userpass
[256], // User/password info (unused)
264 host
[2], // Hostname (unused)
265 resource
[1024], // Resource path (/dir/to/filename.drv)
266 *pc_file_name
; // Filename portion of URI
267 int port
; // Port number (unused)
270 // Determine where CUPS has installed the data files...
271 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
272 datadir
= CUPS_DATADIR
;
275 if (httpSeparateURI(HTTP_URI_CODING_ALL
, name
, scheme
, sizeof(scheme
),
276 userpass
, sizeof(userpass
), host
, sizeof(host
), &port
,
277 resource
, sizeof(resource
)) < HTTP_URI_OK
||
278 strstr(resource
, "../") ||
279 (pc_file_name
= strrchr(resource
, '/')) == NULL
||
280 pc_file_name
== resource
)
282 fprintf(stderr
, "ERROR: Bad PPD name \"%s\"!\n", name
);
286 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\"!", name
);
288 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
289 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
290 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
291 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
293 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
294 cupsdSendIPPTrailer();
300 *pc_file_name
++ = '\0';
303 if (!strncmp(resource
, "/Library/Printers/PPDs.drv/", 27))
304 strlcpy(filename
, resource
, sizeof(filename
));
308 snprintf(filename
, sizeof(filename
), "%s/drv%s", datadir
, resource
);
309 if (access(filename
, 0))
310 snprintf(filename
, sizeof(filename
), "%s/model%s", datadir
, resource
);
313 src
= new ppdcSource(filename
);
315 for (d
= (ppdcDriver
*)src
->drivers
->first();
317 d
= (ppdcDriver
*)src
->drivers
->next())
318 if (!strcasecmp(pc_file_name
, d
->pc_file_name
->value
) ||
319 (d
->file_name
&& !strcasecmp(pc_file_name
, d
->file_name
->value
)))
324 ppdcArray
*locales
; // Locale names
325 ppdcCatalog
*catalog
; // Message catalog in .drv file
328 fprintf(stderr
, "DEBUG: %d locales defined in \"%s\"...\n",
329 src
->po_files
->count
, filename
);
331 locales
= new ppdcArray();
332 for (catalog
= (ppdcCatalog
*)src
->po_files
->first();
334 catalog
= (ppdcCatalog
*)src
->po_files
->next())
336 fprintf(stderr
, "DEBUG: Adding locale \"%s\"...\n",
337 catalog
->locale
->value
);
338 locales
->add(catalog
->locale
);
343 cupsdSendIPPHeader(IPP_OK
, request_id
);
344 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
345 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
346 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
348 cupsdSendIPPTrailer();
352 out
= cupsFileStdout();
353 d
->write_ppd_file(out
, NULL
, locales
, src
, PPDC_LFONLY
);
360 fprintf(stderr
, "ERROR: PPD \"%s\" not found!\n", name
);
364 snprintf(message
, sizeof(message
), "PPD \"%s\" not found!", name
);
366 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
367 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
368 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
369 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
371 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
372 cupsdSendIPPTrailer();
383 * 'cat_ppd()' - Copy a PPD file to stdout.
386 static int /* O - Exit code */
387 cat_ppd(const char *name
, /* I - PPD name */
388 int request_id
) /* I - Request ID for response? */
390 char scheme
[256], /* Scheme from PPD name */
391 *sptr
, /* Pointer into scheme */
392 line
[1024], /* Line/filename */
393 message
[2048]; /* status-message */
397 * Figure out if this is a static or dynamic PPD file...
400 strlcpy(scheme
, name
, sizeof(scheme
));
401 if ((sptr
= strchr(scheme
, ':')) != NULL
)
405 if (!strcmp(scheme
, "file"))
408 * "file:name" == "name"...
419 puts("Content-Type: application/ipp\n");
422 return (cat_static(name
, request_id
));
423 else if (!strcmp(scheme
, "drv"))
424 return (cat_drv(name
, request_id
));
428 * Dynamic PPD, see if we have a driver program to support it...
431 const char *serverbin
; /* CUPS_SERVERBIN env var */
432 char *argv
[4]; /* Arguments for program */
435 if ((serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
436 serverbin
= CUPS_SERVERBIN
;
438 snprintf(line
, sizeof(line
), "%s/driver/%s", serverbin
, scheme
);
439 if (access(line
, X_OK
))
442 * File does not exist or is not executable...
445 fprintf(stderr
, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n",
446 line
, strerror(errno
));
450 snprintf(message
, sizeof(message
), "Unable to access \"%s\" - %s",
451 line
, strerror(errno
));
453 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
454 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
455 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
456 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
458 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
459 cupsdSendIPPTrailer();
466 * Yes, let it cat the PPD file...
471 cupsdSendIPPHeader(IPP_OK
, request_id
);
472 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
473 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
474 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
476 cupsdSendIPPTrailer();
480 argv
[1] = (char *)"cat";
481 argv
[2] = (char *)name
;
484 if (cupsdExec(line
, argv
))
487 * Unable to execute driver...
490 fprintf(stderr
, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n",
491 line
, strerror(errno
));
497 * Return with no errors...
505 * 'copy_static()' - Copy a static PPD file to stdout.
508 static int /* O - Exit code */
509 cat_static(const char *name
, /* I - PPD name */
510 int request_id
) /* I - Request ID for response? */
512 cups_file_t
*fp
; /* PPD file */
513 const char *datadir
; /* CUPS_DATADIR env var */
514 char line
[1024], /* Line/filename */
515 message
[2048]; /* status-message */
518 if (name
[0] == '/' || strstr(name
, "../") || strstr(name
, "/.."))
524 fprintf(stderr
, "ERROR: [cups-driverd] Bad PPD name \"%s\"!\n", name
);
528 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\"!", name
);
530 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
531 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
532 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
533 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
535 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
536 cupsdSendIPPTrailer();
543 * Try opening the file...
547 if (!strncmp(name
, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
548 !strncmp(name
, "Library/Printers/PPDs/Contents/Resources/", 41))
551 * Map ppd-name to Mac OS X standard locations...
554 snprintf(line
, sizeof(line
), "/%s", name
);
558 #elif defined(__linux)
559 if (!strncmp(name
, "lsb/usr/", 8))
562 * Map ppd-name to LSB standard /usr/share/ppd location...
565 snprintf(line
, sizeof(line
), "/usr/share/ppd/%s", name
+ 8);
567 else if (!strncmp(name
, "lsb/opt/", 8))
570 * Map ppd-name to LSB standard /opt/share/ppd location...
573 snprintf(line
, sizeof(line
), "/opt/share/ppd/%s", name
+ 8);
575 else if (!strncmp(name
, "lsb/local/", 10))
578 * Map ppd-name to LSB standard /usr/local/share/ppd location...
581 snprintf(line
, sizeof(line
), "/usr/local/share/ppd/%s", name
+ 10);
585 #endif /* __APPLE__ */
587 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
588 datadir
= CUPS_DATADIR
;
590 snprintf(line
, sizeof(line
), "%s/model/%s", datadir
, name
);
593 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
595 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n",
596 line
, strerror(errno
));
600 snprintf(message
, sizeof(message
), "Unable to open \"%s\" - %s",
601 line
, strerror(errno
));
603 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
604 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
605 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
606 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
608 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
609 cupsdSendIPPTrailer();
617 cupsdSendIPPHeader(IPP_OK
, request_id
);
618 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
619 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
620 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
622 cupsdSendIPPTrailer();
626 * Now copy the file to stdout...
629 while (cupsFileGets(fp
, line
, sizeof(line
)))
639 * 'compare_names()' - Compare PPD filenames for sorting.
642 static int /* O - Result of comparison */
643 compare_names(const ppd_info_t
*p0
, /* I - First PPD file */
644 const ppd_info_t
*p1
) /* I - Second PPD file */
646 int diff
; /* Difference between strings */
649 if ((diff
= strcasecmp(p0
->record
.filename
, p1
->record
.filename
)) != 0)
652 return (strcasecmp(p0
->record
.name
, p1
->record
.name
));
657 * 'compare_ppds()' - Compare PPD file make and model names for sorting.
660 static int /* O - Result of comparison */
661 compare_ppds(const ppd_info_t
*p0
, /* I - First PPD file */
662 const ppd_info_t
*p1
) /* I - Second PPD file */
664 int diff
; /* Difference between strings */
668 * First compare manufacturers...
671 if ((diff
= strcasecmp(p0
->record
.make
, p1
->record
.make
)) != 0)
673 else if ((diff
= cupsdCompareNames(p0
->record
.make_and_model
,
674 p1
->record
.make_and_model
)) != 0)
677 return (strcasecmp(p0
->record
.languages
[0],
678 p1
->record
.languages
[0]));
683 * 'free_array()' - Free an array of strings.
687 free_array(cups_array_t
*a
) /* I - Array to free */
689 char *ptr
; /* Pointer to string */
692 for (ptr
= (char *)cupsArrayFirst(a
);
694 ptr
= (char *)cupsArrayNext(a
))
702 * 'list_ppds()' - List PPD files.
705 static int /* O - Exit code */
706 list_ppds(int request_id
, /* I - Request ID */
707 int limit
, /* I - Limit */
708 const char *opt
) /* I - Option argument */
710 int i
; /* Looping vars */
711 int count
; /* Number of PPDs to send */
712 ppd_info_t
*ppd
; /* Current PPD file */
713 cups_file_t
*fp
; /* ppds.dat file */
714 struct stat fileinfo
; /* ppds.dat information */
715 char filename
[1024], /* ppds.dat filename */
716 model
[1024]; /* Model directory */
717 const char *cups_cachedir
; /* CUPS_CACHEDIR environment variable */
718 const char *cups_datadir
; /* CUPS_DATADIR environment variable */
719 int num_options
; /* Number of options */
720 cups_option_t
*options
; /* Options */
721 const char *requested
, /* requested-attributes option */
722 *device_id
, /* ppd-device-id option */
723 *language
, /* ppd-natural-language option */
724 *make
, /* ppd-make option */
725 *make_and_model
, /* ppd-make-and-model option */
726 *model_number_str
, /* ppd-model-number option */
727 *product
, /* ppd-product option */
728 *psversion
, /* ppd-psversion option */
729 *type_str
; /* ppd-type option */
730 int model_number
, /* ppd-model-number value */
731 type
, /* ppd-type value */
732 mam_len
, /* Length of ppd-make-and-model */
733 device_id_len
, /* Length of ppd-device-id */
734 send_device_id
, /* Send ppd-device-id? */
735 send_make
, /* Send ppd-make? */
736 send_make_and_model
, /* Send ppd-make-and-model? */
737 send_model_number
, /* Send ppd-model-number? */
738 send_name
, /* Send ppd-name? */
739 send_natural_language
, /* Send ppd-natural-language? */
740 send_product
, /* Send ppd-product? */
741 send_psversion
, /* Send ppd-psversion? */
742 send_type
, /* Send ppd-type? */
743 sent_header
; /* Sent the IPP header? */
747 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
748 "opt=\"%s\"\n", request_id
, limit
, opt
);
751 * See if we a PPD database file...
754 PPDsByName
= cupsArrayNew((cups_array_func_t
)compare_names
, NULL
);
755 PPDsByMakeModel
= cupsArrayNew((cups_array_func_t
)compare_ppds
, NULL
);
758 if ((cups_cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
759 cups_cachedir
= CUPS_CACHEDIR
;
761 snprintf(filename
, sizeof(filename
), "%s/ppds.dat", cups_cachedir
);
762 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
765 * See if we have the right sync word...
768 unsigned ppdsync
; /* Sync word */
769 int num_ppds
; /* Number of PPDs */
772 if (cupsFileRead(fp
, (char *)&ppdsync
, sizeof(ppdsync
))
773 == sizeof(ppdsync
) &&
774 ppdsync
== PPD_SYNC
&&
775 !stat(filename
, &fileinfo
) &&
776 ((fileinfo
.st_size
- sizeof(ppdsync
)) % sizeof(ppd_rec_t
)) == 0 &&
777 (num_ppds
= (fileinfo
.st_size
- sizeof(ppdsync
)) /
778 sizeof(ppd_rec_t
)) > 0)
781 * We have a ppds.dat file, so read it!
784 for (; num_ppds
> 0; num_ppds
--)
786 if ((ppd
= (ppd_info_t
*)calloc(1, sizeof(ppd_info_t
))) == NULL
)
788 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
793 if (cupsFileRead(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
)) > 0)
795 cupsArrayAdd(PPDsByName
, ppd
);
796 cupsArrayAdd(PPDsByMakeModel
, ppd
);
805 fprintf(stderr
, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
806 filename
, cupsArrayCount(PPDsByName
));
813 * Load all PPDs in the specified directory and below...
816 if ((cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
817 cups_datadir
= CUPS_DATADIR
;
819 snprintf(model
, sizeof(model
), "%s/model", cups_datadir
);
820 load_ppds(model
, "", 1);
822 snprintf(model
, sizeof(model
), "%s/drv", cups_datadir
);
823 load_ppds(model
, "", 1);
827 * Load PPDs from standard Mac OS X locations...
830 load_ppds("/Library/Printers/PPDs/Contents/Resources",
831 "Library/Printers/PPDs/Contents/Resources", 0);
832 load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
833 "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
834 load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
835 "System/Library/Printers/PPDs/Contents/Resources", 0);
836 load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
837 "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
839 #elif defined(__linux)
841 * Load PPDs from LSB-defined locations...
844 if (!access("/usr/local/share/ppd", 0))
845 load_ppds("/usr/local/share/ppd", "lsb/local", 1);
846 if (!access("/usr/share/ppd", 0))
847 load_ppds("/usr/share/ppd", "lsb/usr", 1);
848 if (!access("/opt/share/ppd", 0))
849 load_ppds("/opt/share/ppd", "lsb/opt", 1);
850 #endif /* __APPLE__ */
853 * Cull PPD files that are no longer present...
856 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
858 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
862 * Remove this PPD file from the list...
865 cupsArrayRemove(PPDsByName
, ppd
);
866 cupsArrayRemove(PPDsByMakeModel
, ppd
);
871 * Write the new ppds.dat file...
876 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
878 unsigned ppdsync
= PPD_SYNC
; /* Sync word */
881 cupsFileWrite(fp
, (char *)&ppdsync
, sizeof(ppdsync
));
883 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByName
);
885 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
))
886 cupsFileWrite(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
));
890 fprintf(stderr
, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
891 filename
, cupsArrayCount(PPDsByName
));
894 fprintf(stderr
, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
895 filename
, strerror(errno
));
898 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr
);
901 * Scan for dynamic PPD files...
907 * Add the raw driver...
910 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
914 * Send IPP attributes...
917 num_options
= cupsParseOptions(opt
, 0, &options
);
918 requested
= cupsGetOption("requested-attributes", num_options
, options
);
919 device_id
= cupsGetOption("ppd-device-id", num_options
, options
);
920 language
= cupsGetOption("ppd-natural-language", num_options
, options
);
921 make
= cupsGetOption("ppd-make", num_options
, options
);
922 make_and_model
= cupsGetOption("ppd-make-and-model", num_options
, options
);
923 model_number_str
= cupsGetOption("ppd-model-number", num_options
, options
);
924 product
= cupsGetOption("ppd-product", num_options
, options
);
925 psversion
= cupsGetOption("ppd-psversion", num_options
, options
);
926 type_str
= cupsGetOption("ppd-type", num_options
, options
);
929 mam_len
= strlen(make_and_model
);
934 device_id_len
= strlen(device_id
);
938 if (model_number_str
)
939 model_number
= atoi(model_number_str
);
946 type
< (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0]));
948 if (!strcmp(type_str
, ppd_types
[type
]))
951 if (type
>= (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0])))
953 fprintf(stderr
, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
962 fprintf(stderr
, "DEBUG: [cups-driverd] requested-attributes=\"%s\"\n",
965 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-device-id=\"%s\"\n",
968 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-natural-language=\"%s\"\n",
971 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-make=\"%s\"\n",
974 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-make-and-model=\"%s\"\n",
976 if (model_number_str
)
977 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-model-number=\"%s\"\n",
980 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-product=\"%s\"\n",
983 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-psversion=\"%s\"\n",
986 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-type=\"%s\"\n", type_str
);
988 if (!requested
|| strstr(requested
, "all"))
992 send_make_and_model
= 1;
993 send_model_number
= 1;
994 send_natural_language
= 1;
1002 send_name
= strstr(requested
, "ppd-name") != NULL
;
1003 send_make
= strstr(requested
, "ppd-make,") != NULL
||
1004 strstr(requested
, ",ppd-make") != NULL
||
1005 !strcmp(requested
, "ppd-make");
1006 send_make_and_model
= strstr(requested
, "ppd-make-and-model") != NULL
;
1007 send_model_number
= strstr(requested
, "ppd-model-number") != NULL
;
1008 send_natural_language
= strstr(requested
, "ppd-natural-language") != NULL
;
1009 send_device_id
= strstr(requested
, "ppd-device-id") != NULL
;
1010 send_product
= strstr(requested
, "ppd-product") != NULL
;
1011 send_psversion
= strstr(requested
, "ppd-psversion") != NULL
;
1012 send_type
= strstr(requested
, "ppd-type") != NULL
;
1015 puts("Content-Type: application/ipp\n");
1019 if (limit
<= 0 || limit
> cupsArrayCount(PPDsByMakeModel
))
1020 count
= cupsArrayCount(PPDsByMakeModel
);
1024 for (ppd
= (ppd_info_t
*)cupsArrayFirst(PPDsByMakeModel
);
1026 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
1029 * Filter PPDs based on make, model, or device ID...
1032 if (ppd
->record
.type
< PPD_TYPE_POSTSCRIPT
||
1033 ppd
->record
.type
>= PPD_TYPE_DRV
)
1036 if (device_id
&& strncasecmp(ppd
->record
.device_id
, device_id
,
1038 continue; /* TODO: implement smart compare */
1042 for (i
= 0; i
< PPD_MAX_LANG
; i
++)
1043 if (!ppd
->record
.languages
[i
][0] ||
1044 !strcasecmp(ppd
->record
.languages
[i
], language
))
1047 if (i
>= PPD_MAX_LANG
|| !ppd
->record
.languages
[i
][0])
1051 if (make
&& strcasecmp(ppd
->record
.make
, make
))
1054 if (make_and_model
&& strncasecmp(ppd
->record
.make_and_model
,
1055 make_and_model
, mam_len
))
1058 if (model_number_str
&& ppd
->record
.model_number
!= model_number
)
1063 for (i
= 0; i
< PPD_MAX_PROD
; i
++)
1064 if (!ppd
->record
.products
[i
][0] ||
1065 !strcasecmp(ppd
->record
.products
[i
], product
))
1068 if (i
>= PPD_MAX_PROD
|| !ppd
->record
.products
[i
][0])
1074 for (i
= 0; i
< PPD_MAX_VERS
; i
++)
1075 if (!ppd
->record
.psversions
[i
][0] ||
1076 !strcasecmp(ppd
->record
.psversions
[i
], psversion
))
1079 if (i
>= PPD_MAX_VERS
|| !ppd
->record
.psversions
[i
][0])
1083 if (type_str
&& ppd
->record
.type
!= type
)
1094 cupsdSendIPPHeader(IPP_OK
, request_id
);
1095 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
1096 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
1097 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
1100 fprintf(stderr
, "DEBUG: [cups-driverd] Sending %s (%s)...\n",
1101 ppd
->record
.name
, ppd
->record
.make_and_model
);
1105 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
1108 cupsdSendIPPString(IPP_TAG_NAME
, "ppd-name", ppd
->record
.name
);
1110 if (send_natural_language
)
1112 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "ppd-natural-language",
1113 ppd
->record
.languages
[0]);
1115 for (i
= 1; i
< PPD_MAX_LANG
&& ppd
->record
.languages
[i
][0]; i
++)
1116 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "", ppd
->record
.languages
[i
]);
1120 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make", ppd
->record
.make
);
1122 if (send_make_and_model
)
1123 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make-and-model",
1124 ppd
->record
.make_and_model
);
1127 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-device-id",
1128 ppd
->record
.device_id
);
1132 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-product",
1133 ppd
->record
.products
[0]);
1135 for (i
= 1; i
< PPD_MAX_PROD
&& ppd
->record
.products
[i
][0]; i
++)
1136 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.products
[i
]);
1141 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-psversion",
1142 ppd
->record
.psversions
[0]);
1144 for (i
= 1; i
< PPD_MAX_VERS
&& ppd
->record
.psversions
[i
][0]; i
++)
1145 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.psversions
[i
]);
1149 cupsdSendIPPString(IPP_TAG_KEYWORD
, "ppd-type",
1150 ppd_types
[ppd
->record
.type
]);
1152 if (send_model_number
)
1153 cupsdSendIPPInteger(IPP_TAG_INTEGER
, "ppd-model-number",
1154 ppd
->record
.model_number
);
1157 * If we have only requested the ppd-make attribute, then skip
1158 * the remaining PPDs with this make...
1161 if (requested
&& !strcmp(requested
, "ppd-make"))
1163 const char *this_make
; /* This ppd-make */
1166 for (this_make
= ppd
->record
.make
,
1167 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
);
1169 ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByMakeModel
))
1170 if (strcasecmp(this_make
, ppd
->record
.make
))
1173 cupsArrayPrev(PPDsByMakeModel
);
1179 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
1180 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
1181 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
1182 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
1185 cupsdSendIPPTrailer();
1192 * 'load_ppds()' - Load PPD files recursively.
1195 static int /* O - 1 on success, 0 on failure */
1196 load_ppds(const char *d
, /* I - Actual directory */
1197 const char *p
, /* I - Virtual path in name */
1198 int descend
) /* I - Descend into directories? */
1200 int i
; /* Looping var */
1201 cups_file_t
*fp
; /* Pointer to file */
1202 cups_dir_t
*dir
; /* Directory pointer */
1203 cups_dentry_t
*dent
; /* Directory entry */
1204 char filename
[1024], /* Name of PPD or directory */
1205 line
[256], /* Line from backend */
1206 *ptr
, /* Pointer into name */
1207 name
[128], /* Name of PPD file */
1208 lang_version
[64], /* PPD LanguageVersion */
1209 lang_encoding
[64], /* PPD LanguageEncoding */
1210 country
[64], /* Country code */
1211 manufacturer
[256], /* Manufacturer */
1212 make_model
[256], /* Make and Model */
1213 model_name
[256], /* ModelName */
1214 nick_name
[256], /* NickName */
1215 device_id
[256], /* 1284DeviceID */
1216 product
[256], /* Product */
1217 psversion
[256], /* PSVersion */
1218 temp
[512]; /* Temporary make and model */
1219 int model_number
, /* cupsModelNumber */
1220 type
; /* ppd-type */
1221 cups_array_t
*products
, /* Product array */
1222 *psversions
, /* PSVersion array */
1223 *cups_languages
; /* cupsLanguages array */
1224 ppd_info_t
*ppd
, /* New PPD file */
1225 key
; /* Search key */
1226 int new_ppd
; /* Is this a new PPD? */
1227 struct /* LanguageVersion translation table */
1229 const char *version
, /* LanguageVersion string */
1230 *language
; /* Language code */
1233 { "chinese", "zh" },
1237 { "english", "en" },
1238 { "finnish", "fi" },
1242 { "hungarian", "hu" },
1243 { "italian", "it" },
1244 { "japanese", "ja" },
1246 { "norwegian", "no" },
1248 { "portuguese", "pt" },
1249 { "russian", "ru" },
1250 { "simplified chinese", "zh_CN" },
1252 { "spanish", "es" },
1253 { "swedish", "sv" },
1254 { "traditional chinese", "zh_TW" },
1259 fprintf(stderr
, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d
);
1261 if ((dir
= cupsDirOpen(d
)) == NULL
)
1263 if (errno
!= ENOENT
)
1265 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1266 d
, strerror(errno
));
1271 while ((dent
= cupsDirRead(dir
)) != NULL
)
1274 * Skip files/directories starting with "."...
1277 if (dent
->filename
[0] == '.')
1281 * See if this is a file...
1284 snprintf(filename
, sizeof(filename
), "%s/%s", d
, dent
->filename
);
1287 snprintf(name
, sizeof(name
), "%s/%s", p
, dent
->filename
);
1289 strlcpy(name
, dent
->filename
, sizeof(name
));
1291 if (S_ISDIR(dent
->fileinfo
.st_mode
))
1294 * Do subdirectory...
1298 if (!load_ppds(filename
, name
, 1))
1308 * See if this file has been scanned before...
1311 strcpy(key
.record
.filename
, name
);
1312 strcpy(key
.record
.name
, name
);
1314 ppd
= (ppd_info_t
*)cupsArrayFind(PPDsByName
, &key
);
1317 ppd
->record
.size
== dent
->fileinfo
.st_size
&&
1318 ppd
->record
.mtime
== dent
->fileinfo
.st_mtime
)
1324 while ((ppd
= (ppd_info_t
*)cupsArrayNext(PPDsByName
)) != NULL
&&
1325 !strcasecmp(ppd
->record
.filename
, name
));
1331 * No, file is new/changed, so re-scan it...
1334 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1338 * Now see if this is a PPD file...
1342 cupsFileGets(fp
, line
, sizeof(line
));
1344 if (strncmp(line
, "*PPD-Adobe:", 11))
1347 * Nope, treat it as a driver information file...
1350 load_drv(filename
, name
, fp
, dent
->fileinfo
.st_mtime
,
1351 dent
->fileinfo
.st_size
);
1356 * Now read until we get the NickName field...
1359 cups_languages
= cupsArrayNew(NULL
, NULL
);
1360 products
= cupsArrayNew(NULL
, NULL
);
1361 psversions
= cupsArrayNew(NULL
, NULL
);
1363 model_name
[0] = '\0';
1364 nick_name
[0] = '\0';
1365 manufacturer
[0] = '\0';
1366 device_id
[0] = '\0';
1367 lang_encoding
[0] = '\0';
1368 strcpy(lang_version
, "en");
1370 type
= PPD_TYPE_POSTSCRIPT
;
1372 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
1374 if (!strncmp(line
, "*Manufacturer:", 14))
1375 sscanf(line
, "%*[^\"]\"%255[^\"]", manufacturer
);
1376 else if (!strncmp(line
, "*ModelName:", 11))
1377 sscanf(line
, "%*[^\"]\"%127[^\"]", model_name
);
1378 else if (!strncmp(line
, "*LanguageEncoding:", 18))
1379 sscanf(line
, "%*[^:]:%63s", lang_encoding
);
1380 else if (!strncmp(line
, "*LanguageVersion:", 17))
1381 sscanf(line
, "%*[^:]:%63s", lang_version
);
1382 else if (!strncmp(line
, "*NickName:", 10))
1383 sscanf(line
, "%*[^\"]\"%255[^\"]", nick_name
);
1384 else if (!strncasecmp(line
, "*1284DeviceID:", 14))
1385 sscanf(line
, "%*[^\"]\"%255[^\"]", device_id
);
1386 else if (!strncmp(line
, "*Product:", 9))
1388 if (sscanf(line
, "%*[^\"]\"(%255[^)]", product
) == 1)
1389 cupsArrayAdd(products
, strdup(product
));
1391 else if (!strncmp(line
, "*PSVersion:", 11))
1393 sscanf(line
, "%*[^\"]\"%255[^\"]", psversion
);
1394 cupsArrayAdd(psversions
, strdup(psversion
));
1396 else if (!strncmp(line
, "*cupsLanguages:", 15))
1398 char *start
; /* Start of language */
1401 for (start
= line
+ 15; *start
&& isspace(*start
& 255); start
++);
1403 if (*start
++ == '\"')
1407 for (ptr
= start
+ 1;
1408 *ptr
&& *ptr
!= '\"' && !isspace(*ptr
& 255);
1415 while (isspace(*ptr
& 255))
1419 cupsArrayAdd(cups_languages
, strdup(start
));
1424 else if (!strncmp(line
, "*cupsFax:", 9))
1426 for (ptr
= line
+ 9; isspace(*ptr
& 255); ptr
++);
1428 if (!strncasecmp(ptr
, "true", 4))
1429 type
= PPD_TYPE_FAX
;
1431 else if (!strncmp(line
, "*cupsFilter:", 12) && type
== PPD_TYPE_POSTSCRIPT
)
1433 if (strstr(line
+ 12, "application/vnd.cups-raster"))
1434 type
= PPD_TYPE_RASTER
;
1435 else if (strstr(line
+ 12, "application/vnd.cups-pdf"))
1436 type
= PPD_TYPE_PDF
;
1438 else if (!strncmp(line
, "*cupsModelNumber:", 17))
1439 sscanf(line
, "*cupsModelNumber:%d", &model_number
);
1440 else if (!strncmp(line
, "*OpenUI", 7))
1443 * Stop early if we have a NickName or ModelName attributes
1444 * before the first OpenUI...
1447 if ((model_name
[0] || nick_name
[0]) && cupsArrayCount(products
) > 0 &&
1448 cupsArrayCount(psversions
) > 0)
1460 * See if we got all of the required info...
1464 cupsCharsetToUTF8((cups_utf8_t
*)make_model
, nick_name
,
1465 sizeof(make_model
), _ppdGetEncoding(lang_encoding
));
1467 strcpy(make_model
, model_name
);
1469 while (isspace(make_model
[0] & 255))
1470 _cups_strcpy(make_model
, make_model
+ 1);
1472 if (!make_model
[0] || cupsArrayCount(products
) == 0 ||
1473 cupsArrayCount(psversions
) == 0)
1476 * We don't have all the info needed, so skip this file...
1480 fprintf(stderr
, "WARNING: Missing NickName and ModelName in %s!\n",
1483 if (cupsArrayCount(products
) == 0)
1484 fprintf(stderr
, "WARNING: Missing Product in %s!\n", filename
);
1486 if (cupsArrayCount(psversions
) == 0)
1487 fprintf(stderr
, "WARNING: Missing PSVersion in %s!\n", filename
);
1489 free_array(products
);
1490 free_array(psversions
);
1491 free_array(cups_languages
);
1497 cupsArrayAdd(products
, strdup(model_name
));
1500 * Normalize the make and model string...
1503 while (isspace(manufacturer
[0] & 255))
1504 _cups_strcpy(manufacturer
, manufacturer
+ 1);
1506 if (!strncasecmp(make_model
, manufacturer
, strlen(manufacturer
)))
1507 strlcpy(temp
, make_model
, sizeof(temp
));
1509 snprintf(temp
, sizeof(temp
), "%s %s", manufacturer
, make_model
);
1511 _ppdNormalizeMakeAndModel(temp
, make_model
, sizeof(make_model
));
1514 * See if we got a manufacturer...
1517 if (!manufacturer
[0] || !strcmp(manufacturer
, "ESP"))
1520 * Nope, copy the first part of the make and model then...
1523 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
1526 * Truncate at the first space, dash, or slash, or make the
1527 * manufacturer "Other"...
1530 for (ptr
= manufacturer
; *ptr
; ptr
++)
1531 if (*ptr
== ' ' || *ptr
== '-' || *ptr
== '/')
1534 if (*ptr
&& ptr
> manufacturer
)
1537 strcpy(manufacturer
, "Other");
1539 else if (!strncasecmp(manufacturer
, "LHAG", 4) ||
1540 !strncasecmp(manufacturer
, "linotype", 8))
1541 strcpy(manufacturer
, "LHAG");
1542 else if (!strncasecmp(manufacturer
, "Hewlett", 7))
1543 strcpy(manufacturer
, "HP");
1546 * Fix the lang_version as needed...
1549 if ((ptr
= strchr(lang_version
, '-')) != NULL
)
1551 else if ((ptr
= strchr(lang_version
, '_')) != NULL
)
1557 * Setup the country suffix...
1561 _cups_strcpy(country
+ 1, ptr
);
1566 * No country suffix...
1572 for (i
= 0; i
< (int)(sizeof(languages
) / sizeof(languages
[0])); i
++)
1573 if (!strcasecmp(languages
[i
].version
, lang_version
))
1576 if (i
< (int)(sizeof(languages
) / sizeof(languages
[0])))
1579 * Found a known language...
1582 snprintf(lang_version
, sizeof(lang_version
), "%s%s",
1583 languages
[i
].language
, country
);
1588 * Unknown language; use "xx"...
1591 strcpy(lang_version
, "xx");
1595 * Record the PPD file...
1603 * Add new PPD file...
1606 fprintf(stderr
, "DEBUG: [cups-driverd] Adding ppd \"%s\"...\n", name
);
1608 ppd
= add_ppd(name
, name
, lang_version
, manufacturer
, make_model
,
1609 device_id
, (char *)cupsArrayFirst(products
),
1610 (char *)cupsArrayFirst(psversions
),
1611 dent
->fileinfo
.st_mtime
, dent
->fileinfo
.st_size
,
1612 model_number
, type
);
1623 * Update existing record...
1626 fprintf(stderr
, "DEBUG: [cups-driverd] Updating ppd \"%s\"...\n", name
);
1628 memset(ppd
, 0, sizeof(ppd_info_t
));
1631 ppd
->record
.mtime
= dent
->fileinfo
.st_mtime
;
1632 ppd
->record
.size
= dent
->fileinfo
.st_size
;
1633 ppd
->record
.model_number
= model_number
;
1634 ppd
->record
.type
= type
;
1636 strlcpy(ppd
->record
.name
, name
, sizeof(ppd
->record
.name
));
1637 strlcpy(ppd
->record
.make
, manufacturer
, sizeof(ppd
->record
.make
));
1638 strlcpy(ppd
->record
.make_and_model
, make_model
,
1639 sizeof(ppd
->record
.make_and_model
));
1640 strlcpy(ppd
->record
.languages
[0], lang_version
,
1641 sizeof(ppd
->record
.languages
[0]));
1642 strlcpy(ppd
->record
.products
[0], (char *)cupsArrayFirst(products
),
1643 sizeof(ppd
->record
.products
[0]));
1644 strlcpy(ppd
->record
.psversions
[0], (char *)cupsArrayFirst(psversions
),
1645 sizeof(ppd
->record
.psversions
[0]));
1646 strlcpy(ppd
->record
.device_id
, device_id
, sizeof(ppd
->record
.device_id
));
1650 * Add remaining products, versions, and languages...
1654 i
< PPD_MAX_PROD
&& (ptr
= (char *)cupsArrayNext(products
)) != NULL
;
1656 strlcpy(ppd
->record
.products
[i
], ptr
,
1657 sizeof(ppd
->record
.products
[0]));
1660 i
< PPD_MAX_VERS
&& (ptr
= (char *)cupsArrayNext(psversions
)) != NULL
;
1662 strlcpy(ppd
->record
.psversions
[i
], ptr
,
1663 sizeof(ppd
->record
.psversions
[0]));
1665 for (i
= 1, ptr
= (char *)cupsArrayFirst(cups_languages
);
1666 i
< PPD_MAX_LANG
&& ptr
;
1667 i
++, ptr
= (char *)cupsArrayNext(cups_languages
))
1668 strlcpy(ppd
->record
.languages
[i
], ptr
,
1669 sizeof(ppd
->record
.languages
[0]));
1672 * Free products, versions, and languages...
1675 free_array(cups_languages
);
1676 free_array(products
);
1677 free_array(psversions
);
1689 * 'load_drv()' - Load the PPDs from a driver information file.
1692 static int /* O - 1 on success, 0 on failure */
1693 load_drv(const char *filename
, /* I - Actual filename */
1694 const char *name
, /* I - Name to the rest of the world */
1695 cups_file_t
*fp
, /* I - File to read from */
1696 time_t mtime
, /* I - Mod time of driver info file */
1697 off_t size
) /* I - Size of driver info file */
1699 ppdcSource
*src
; // Driver information file
1700 ppdcDriver
*d
; // Current driver
1701 ppdcAttr
*device_id
, // 1284DeviceID attribute
1702 *product
, // Current product value
1703 *ps_version
, // PSVersion attribute
1704 *cups_fax
, // cupsFax attribute
1705 *nick_name
; // NickName attribute
1706 ppdcFilter
*filter
; // Current filter
1707 bool product_found
; // Found product?
1708 char uri
[1024], // Driver URI
1709 make_model
[1024]; // Make and model
1710 int type
; // Driver type
1714 * Load the driver info file...
1717 src
= new ppdcSource(filename
, fp
);
1719 if (src
->drivers
->count
== 0)
1722 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1729 * Add a dummy entry for the file...
1732 add_ppd(filename
, filename
, "", "", "", "", "", "", mtime
, size
, 0,
1736 * Then the drivers in the file...
1739 for (d
= (ppdcDriver
*)src
->drivers
->first();
1741 d
= (ppdcDriver
*)src
->drivers
->next())
1743 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "drv", "", "", 0,
1745 d
->file_name
? d
->file_name
->value
:
1746 d
->pc_file_name
->value
);
1748 device_id
= d
->find_attr("1284DeviceID", NULL
);
1749 ps_version
= d
->find_attr("PSVersion", NULL
);
1750 nick_name
= d
->find_attr("NickName", NULL
);
1753 strlcpy(make_model
, nick_name
->value
->value
, sizeof(make_model
));
1754 else if (strncasecmp(d
->model_name
->value
, d
->manufacturer
->value
,
1755 strlen(d
->manufacturer
->value
)))
1756 snprintf(make_model
, sizeof(make_model
), "%s %s, %s",
1757 d
->manufacturer
->value
, d
->model_name
->value
,
1760 snprintf(make_model
, sizeof(make_model
), "%s, %s", d
->model_name
->value
,
1763 if ((cups_fax
= d
->find_attr("cupsFax", NULL
)) != NULL
&&
1764 !strcasecmp(cups_fax
->value
->value
, "true"))
1765 type
= PPD_TYPE_FAX
;
1766 else if (d
->type
== PPDC_DRIVER_PS
)
1767 type
= PPD_TYPE_POSTSCRIPT
;
1768 else if (d
->type
!= PPDC_DRIVER_CUSTOM
)
1769 type
= PPD_TYPE_RASTER
;
1772 for (filter
= (ppdcFilter
*)d
->filters
->first(),
1773 type
= PPD_TYPE_POSTSCRIPT
;
1775 filter
= (ppdcFilter
*)d
->filters
->next())
1776 if (strcasecmp(filter
->mime_type
->value
, "application/vnd.cups-raster"))
1777 type
= PPD_TYPE_RASTER
;
1778 else if (strcasecmp(filter
->mime_type
->value
,
1779 "application/vnd.cups-pdf"))
1780 type
= PPD_TYPE_PDF
;
1783 for (product
= (ppdcAttr
*)d
->attrs
->first(), product_found
= false;
1785 product
= (ppdcAttr
*)d
->attrs
->next())
1786 if (!strcmp(product
->name
->value
, "Product"))
1788 product_found
= true;
1790 add_ppd(filename
, uri
, "en", d
->manufacturer
->value
, make_model
,
1791 device_id
? device_id
->value
->value
: "",
1792 product
->value
->value
,
1793 ps_version
? ps_version
->value
->value
: "(3010) 0",
1794 mtime
, size
, d
->model_number
, type
);
1798 add_ppd(filename
, uri
, "en", d
->manufacturer
->value
, make_model
,
1799 device_id
? device_id
->value
->value
: "",
1800 d
->model_name
->value
,
1801 ps_version
? ps_version
->value
->value
: "(3010) 0",
1802 mtime
, size
, d
->model_number
, type
);
1812 * 'load_drivers()' - Load driver-generated PPD files.
1815 static int /* O - 1 on success, 0 on failure */
1818 int i
; /* Looping var */
1819 char *start
, /* Start of value */
1820 *ptr
; /* Pointer into string */
1821 const char *server_bin
; /* CUPS_SERVERBIN env variable */
1822 char drivers
[1024]; /* Location of driver programs */
1823 int pid
; /* Process ID for driver program */
1824 cups_file_t
*fp
; /* Pipe to driver program */
1825 cups_dir_t
*dir
; /* Directory pointer */
1826 cups_dentry_t
*dent
; /* Directory entry */
1827 char *argv
[3], /* Arguments for command */
1828 filename
[1024], /* Name of driver */
1829 line
[2048], /* Line from driver */
1830 name
[512], /* ppd-name */
1831 make
[128], /* ppd-make */
1832 make_and_model
[128], /* ppd-make-and-model */
1833 device_id
[128], /* ppd-device-id */
1834 languages
[128], /* ppd-natural-language */
1835 product
[128], /* ppd-product */
1836 psversion
[128], /* ppd-psversion */
1837 type_str
[128]; /* ppd-type */
1838 int type
; /* PPD type */
1839 ppd_info_t
*ppd
; /* Newly added PPD */
1843 * Try opening the driver directory...
1846 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
1847 server_bin
= CUPS_SERVERBIN
;
1849 snprintf(drivers
, sizeof(drivers
), "%s/driver", server_bin
);
1851 if ((dir
= cupsDirOpen(drivers
)) == NULL
)
1853 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open driver directory "
1855 drivers
, strerror(errno
));
1860 * Loop through all of the device drivers...
1863 argv
[1] = (char *)"list";
1866 while ((dent
= cupsDirRead(dir
)) != NULL
)
1869 * Only look at executable files...
1872 if (!(dent
->fileinfo
.st_mode
& 0111) || !S_ISREG(dent
->fileinfo
.st_mode
))
1876 * Run the driver with no arguments and collect the output...
1879 argv
[0] = dent
->filename
;
1880 snprintf(filename
, sizeof(filename
), "%s/%s", drivers
, dent
->filename
);
1882 if ((fp
= cupsdPipeCommand(&pid
, filename
, argv
, 0)) != NULL
)
1884 while (cupsFileGets(fp
, line
, sizeof(line
)))
1887 * Each line is of the form:
1889 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
1890 * "ppd-device-id" "ppd-product" "ppd-psversion"
1893 device_id
[0] = '\0';
1895 psversion
[0] = '\0';
1896 strcpy(type_str
, "postscript");
1898 if (sscanf(line
, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
1899 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1900 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1901 "%*[ \t]\"%127[^\"]\"",
1902 name
, languages
, make
, make_and_model
,
1903 device_id
, product
, psversion
, type_str
) < 4)
1906 * Bad format; strip trailing newline and write an error message.
1909 if (line
[strlen(line
) - 1] == '\n')
1910 line
[strlen(line
) - 1] = '\0';
1912 fprintf(stderr
, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1913 dent
->filename
, line
);
1919 * Add the device to the array of available devices...
1922 if ((start
= strchr(languages
, ',')) != NULL
)
1926 type
< (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0]));
1928 if (!strcmp(type_str
, ppd_types
[type
]))
1931 if (type
>= (int)(sizeof(ppd_types
) / sizeof(ppd_types
[0])))
1934 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
1936 type
= PPD_TYPE_UNKNOWN
;
1939 ppd
= add_ppd("", name
, languages
, make
, make_and_model
, device_id
,
1940 product
, psversion
, 0, 0, 0, type
);
1949 if (start
&& *start
)
1951 for (i
= 1; i
< PPD_MAX_LANG
&& *start
; i
++)
1953 if ((ptr
= strchr(start
, ',')) != NULL
)
1956 ptr
= start
+ strlen(start
);
1958 strlcpy(ppd
->record
.languages
[i
], start
,
1959 sizeof(ppd
->record
.languages
[0]));
1965 fprintf(stderr
, "DEBUG: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1973 fprintf(stderr
, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1974 filename
, strerror(errno
));