]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
4509bb49 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
4 | * PPD/driver support for the Common UNIX Printing System (CUPS). | |
5 | * | |
94da7e34 MS |
6 | * This program handles listing and installing static PPD files, PPD files |
7 | * created from driver information files, and dynamically generated PPD files | |
8 | * using driver helper programs. | |
ef416fc2 | 9 | * |
c168a833 | 10 | * Copyright 2007-2009 by Apple Inc. |
f899b121 | 11 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 12 | * |
13 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 18 | * |
19 | * Contents: | |
20 | * | |
58dc1933 MS |
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. | |
178cb736 | 26 | * compare_inodes() - Compare two inodes. |
58dc1933 MS |
27 | * compare_matches() - Compare PPD match scores for sorting. |
28 | * compare_names() - Compare PPD filenames for sorting. | |
29 | * compare_ppds() - Compare PPD file make and model names for sorting. | |
f0ab5bff | 30 | * dump_ppds_dat() - Dump the contents of the ppds.dat file. |
58dc1933 MS |
31 | * free_array() - Free an array of strings. |
32 | * list_ppds() - List PPD files. | |
33 | * load_ppds() - Load PPD files recursively. | |
34 | * load_drv() - Load the PPDs from a driver information file. | |
35 | * load_drivers() - Load driver-generated PPD files. | |
f0ab5bff | 36 | * load_ppds_dat() - Load the ppds.dat file. |
58dc1933 MS |
37 | * regex_device_id() - Compile a regular expression based on the 1284 device |
38 | * ID. | |
39 | * regex_string() - Construct a regular expression to compare a simple | |
40 | * string. | |
ef416fc2 | 41 | */ |
42 | ||
43 | /* | |
44 | * Include necessary headers... | |
45 | */ | |
46 | ||
47 | #include "util.h" | |
48 | #include <cups/dir.h> | |
80ca4592 | 49 | #include <cups/transcode.h> |
5eb9da71 | 50 | #include <cups/ppd-private.h> |
4509bb49 | 51 | #include <ppdc/ppdc.h> |
58dc1933 | 52 | #include <regex.h> |
ef416fc2 | 53 | |
54 | ||
b94498cf | 55 | /* |
56 | * Constants... | |
57 | */ | |
58 | ||
b0f6947b | 59 | #define PPD_SYNC 0x50504437 /* Sync word for ppds.dat (PPD7) */ |
b94498cf | 60 | #define PPD_MAX_LANG 32 /* Maximum languages */ |
b0f6947b MS |
61 | #define PPD_MAX_PROD 32 /* Maximum products */ |
62 | #define PPD_MAX_VERS 32 /* Maximum versions */ | |
b94498cf | 63 | |
3d8365b8 | 64 | #define PPD_TYPE_POSTSCRIPT 0 /* PostScript PPD */ |
65 | #define PPD_TYPE_PDF 1 /* PDF PPD */ | |
66 | #define PPD_TYPE_RASTER 2 /* CUPS raster PPD */ | |
67 | #define PPD_TYPE_FAX 3 /* Facsimile/MFD PPD */ | |
68 | #define PPD_TYPE_UNKNOWN 4 /* Other/hybrid PPD */ | |
66ab9486 | 69 | #define PPD_TYPE_DRV 5 /* Driver info file */ |
3d8365b8 | 70 | |
ac884b6a | 71 | static const char * const ppd_types[] = /* ppd-type values */ |
3d8365b8 | 72 | { |
73 | "postscript", | |
74 | "pdf", | |
75 | "raster", | |
76 | "fax", | |
94da7e34 MS |
77 | "unknown", |
78 | "drv" | |
3d8365b8 | 79 | }; |
80 | ||
b94498cf | 81 | |
ef416fc2 | 82 | /* |
83 | * PPD information structures... | |
84 | */ | |
85 | ||
86 | typedef struct /**** PPD record ****/ | |
87 | { | |
88 | time_t mtime; /* Modification time */ | |
749b1e90 | 89 | off_t size; /* Size in bytes */ |
3d8365b8 | 90 | int model_number; /* cupsModelNumber */ |
91 | int type; /* ppd-type */ | |
66ab9486 MS |
92 | char filename[512], /* Filename */ |
93 | name[512], /* PPD name */ | |
b94498cf | 94 | languages[PPD_MAX_LANG][6], |
95 | /* LanguageVersion/cupsLanguages */ | |
96 | products[PPD_MAX_PROD][128], | |
97 | /* Product strings */ | |
98 | psversions[PPD_MAX_VERS][32], | |
99 | /* PSVersion strings */ | |
ef416fc2 | 100 | make[128], /* Manufacturer */ |
f899b121 | 101 | make_and_model[128], /* NickName/ModelName */ |
ed6e7faf MS |
102 | device_id[256], /* IEEE 1284 Device ID */ |
103 | scheme[128]; /* PPD scheme */ | |
b94498cf | 104 | } ppd_rec_t; |
ef416fc2 | 105 | |
106 | typedef struct /**** In-memory record ****/ | |
107 | { | |
108 | int found; /* 1 if PPD is found */ | |
58dc1933 | 109 | int matches; /* Match count */ |
ef416fc2 | 110 | ppd_rec_t record; /* PPDs.dat record */ |
111 | } ppd_info_t; | |
112 | ||
113 | ||
114 | /* | |
115 | * Globals... | |
116 | */ | |
117 | ||
178cb736 MS |
118 | cups_array_t *Inodes = NULL, /* Inodes of directories we've visited */ |
119 | *PPDsByName = NULL, /* PPD files sorted by filename and name */ | |
66ab9486 | 120 | *PPDsByMakeModel = NULL;/* PPD files sorted by make and model */ |
ef416fc2 | 121 | int ChangedPPD; /* Did we change the PPD database? */ |
122 | ||
123 | ||
124 | /* | |
125 | * Local functions... | |
126 | */ | |
127 | ||
66ab9486 MS |
128 | static ppd_info_t *add_ppd(const char *filename, const char *name, |
129 | const char *language, const char *make, | |
130 | const char *make_and_model, | |
f899b121 | 131 | const char *device_id, const char *product, |
b94498cf | 132 | const char *psversion, time_t mtime, |
ed6e7faf MS |
133 | size_t size, int model_number, int type, |
134 | const char *scheme); | |
4509bb49 | 135 | static int cat_drv(const char *name, int request_id); |
b94498cf | 136 | static int cat_ppd(const char *name, int request_id); |
4509bb49 | 137 | static int cat_static(const char *name, int request_id); |
178cb736 | 138 | static int compare_inodes(struct stat *a, struct stat *b); |
58dc1933 MS |
139 | static int compare_matches(const ppd_info_t *p0, |
140 | const ppd_info_t *p1); | |
bd7854cb | 141 | static int compare_names(const ppd_info_t *p0, |
142 | const ppd_info_t *p1); | |
143 | static int compare_ppds(const ppd_info_t *p0, | |
144 | const ppd_info_t *p1); | |
f0ab5bff | 145 | static int dump_ppds_dat(void); |
b94498cf | 146 | static void free_array(cups_array_t *a); |
bd7854cb | 147 | static int list_ppds(int request_id, int limit, const char *opt); |
ed6e7faf MS |
148 | static int load_drivers(cups_array_t *include, |
149 | cups_array_t *exclude); | |
4509bb49 MS |
150 | static int load_drv(const char *filename, const char *name, |
151 | cups_file_t *fp, time_t mtime, off_t size); | |
b94498cf | 152 | static int load_ppds(const char *d, const char *p, int descend); |
f0ab5bff MS |
153 | static void load_ppds_dat(char *filename, size_t filesize, |
154 | int verbose); | |
58dc1933 MS |
155 | static regex_t *regex_device_id(const char *device_id); |
156 | static regex_t *regex_string(const char *s); | |
ef416fc2 | 157 | |
158 | ||
159 | /* | |
160 | * 'main()' - Scan for drivers and return an IPP response. | |
161 | * | |
162 | * Usage: | |
163 | * | |
164 | * cups-driverd request_id limit options | |
165 | */ | |
166 | ||
167 | int /* O - Exit code */ | |
168 | main(int argc, /* I - Number of command-line args */ | |
169 | char *argv[]) /* I - Command-line arguments */ | |
170 | { | |
171 | /* | |
172 | * Install or list PPDs... | |
173 | */ | |
174 | ||
175 | if (argc == 3 && !strcmp(argv[1], "cat")) | |
b94498cf | 176 | return (cat_ppd(argv[2], 0)); |
f0ab5bff MS |
177 | else if (argc == 2 && !strcmp(argv[1], "dump")) |
178 | return (dump_ppds_dat()); | |
b94498cf | 179 | else if (argc == 4 && !strcmp(argv[1], "get")) |
180 | return (cat_ppd(argv[3], atoi(argv[2]))); | |
ef416fc2 | 181 | else if (argc == 5 && !strcmp(argv[1], "list")) |
182 | return (list_ppds(atoi(argv[2]), atoi(argv[3]), argv[4])); | |
183 | else | |
184 | { | |
185 | fputs("Usage: cups-driverd cat ppd-name\n", stderr); | |
f0ab5bff | 186 | fputs("Usage: cups-driverd dump\n", stderr); |
b94498cf | 187 | fputs("Usage: cups-driverd get request_id ppd-name\n", stderr); |
ef416fc2 | 188 | fputs("Usage: cups-driverd list request_id limit options\n", stderr); |
189 | return (1); | |
190 | } | |
191 | } | |
192 | ||
193 | ||
194 | /* | |
195 | * 'add_ppd()' - Add a PPD file. | |
196 | */ | |
197 | ||
bd7854cb | 198 | static ppd_info_t * /* O - PPD */ |
66ab9486 MS |
199 | add_ppd(const char *filename, /* I - PPD filename */ |
200 | const char *name, /* I - PPD name */ | |
b94498cf | 201 | const char *language, /* I - LanguageVersion */ |
ef416fc2 | 202 | const char *make, /* I - Manufacturer */ |
f899b121 | 203 | const char *make_and_model, /* I - NickName/ModelName */ |
89d46774 | 204 | const char *device_id, /* I - 1284DeviceID */ |
f899b121 | 205 | const char *product, /* I - Product */ |
b94498cf | 206 | const char *psversion, /* I - PSVersion */ |
ef416fc2 | 207 | time_t mtime, /* I - Modification time */ |
3d8365b8 | 208 | size_t size, /* I - File size */ |
209 | int model_number, /* I - Model number */ | |
ed6e7faf MS |
210 | int type, /* I - Driver type */ |
211 | const char *scheme) /* I - PPD scheme */ | |
ef416fc2 | 212 | { |
213 | ppd_info_t *ppd; /* PPD */ | |
b86bc4cf | 214 | char *recommended; /* Foomatic driver string */ |
ef416fc2 | 215 | |
216 | ||
217 | /* | |
218 | * Add a new PPD file... | |
219 | */ | |
220 | ||
66ab9486 | 221 | if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL) |
ef416fc2 | 222 | { |
66ab9486 MS |
223 | fprintf(stderr, |
224 | "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n", | |
225 | cupsArrayCount(PPDsByName)); | |
226 | return (NULL); | |
ef416fc2 | 227 | } |
228 | ||
ef416fc2 | 229 | /* |
230 | * Zero-out the PPD data and copy the values over... | |
231 | */ | |
232 | ||
3d8365b8 | 233 | ppd->found = 1; |
234 | ppd->record.mtime = mtime; | |
235 | ppd->record.size = size; | |
236 | ppd->record.model_number = model_number; | |
237 | ppd->record.type = type; | |
ef416fc2 | 238 | |
66ab9486 | 239 | strlcpy(ppd->record.filename, filename, sizeof(ppd->record.filename)); |
ef416fc2 | 240 | strlcpy(ppd->record.name, name, sizeof(ppd->record.name)); |
b94498cf | 241 | strlcpy(ppd->record.languages[0], language, |
242 | sizeof(ppd->record.languages[0])); | |
243 | strlcpy(ppd->record.products[0], product, sizeof(ppd->record.products[0])); | |
244 | strlcpy(ppd->record.psversions[0], psversion, | |
245 | sizeof(ppd->record.psversions[0])); | |
ef416fc2 | 246 | strlcpy(ppd->record.make, make, sizeof(ppd->record.make)); |
247 | strlcpy(ppd->record.make_and_model, make_and_model, | |
248 | sizeof(ppd->record.make_and_model)); | |
bd7854cb | 249 | strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id)); |
ed6e7faf | 250 | strlcpy(ppd->record.scheme, scheme, sizeof(ppd->record.scheme)); |
ef416fc2 | 251 | |
b86bc4cf | 252 | /* |
253 | * Strip confusing (and often wrong) "recommended" suffix added by | |
254 | * Foomatic drivers... | |
255 | */ | |
256 | ||
b94498cf | 257 | if ((recommended = strstr(ppd->record.make_and_model, |
258 | " (recommended)")) != NULL) | |
b86bc4cf | 259 | *recommended = '\0'; |
260 | ||
66ab9486 MS |
261 | /* |
262 | * Add the PPD to the PPD arrays... | |
263 | */ | |
264 | ||
265 | cupsArrayAdd(PPDsByName, ppd); | |
266 | cupsArrayAdd(PPDsByMakeModel, ppd); | |
267 | ||
ef416fc2 | 268 | /* |
269 | * Return the new PPD pointer... | |
270 | */ | |
271 | ||
272 | return (ppd); | |
273 | } | |
274 | ||
275 | ||
4509bb49 MS |
276 | /* |
277 | * 'cat_drv()' - Generate a PPD from a driver info file. | |
278 | */ | |
279 | ||
280 | static int /* O - Exit code */ | |
281 | cat_drv(const char *name, /* I - PPD name */ | |
282 | int request_id) /* I - Request ID for response? */ | |
283 | { | |
284 | const char *datadir; // CUPS_DATADIR env var | |
285 | ppdcSource *src; // PPD source file data | |
286 | ppdcDriver *d; // Current driver | |
287 | cups_file_t *out; // Stdout via CUPS file API | |
288 | char message[2048], // status-message | |
289 | filename[1024], // Full path to .drv file(s) | |
290 | scheme[32], // URI scheme ("drv") | |
291 | userpass[256], // User/password info (unused) | |
292 | host[2], // Hostname (unused) | |
293 | resource[1024], // Resource path (/dir/to/filename.drv) | |
294 | *pc_file_name; // Filename portion of URI | |
295 | int port; // Port number (unused) | |
296 | ||
297 | ||
298 | // Determine where CUPS has installed the data files... | |
299 | if ((datadir = getenv("CUPS_DATADIR")) == NULL) | |
300 | datadir = CUPS_DATADIR; | |
301 | ||
302 | // Pull out the | |
303 | if (httpSeparateURI(HTTP_URI_CODING_ALL, name, scheme, sizeof(scheme), | |
304 | userpass, sizeof(userpass), host, sizeof(host), &port, | |
305 | resource, sizeof(resource)) < HTTP_URI_OK || | |
306 | strstr(resource, "../") || | |
307 | (pc_file_name = strrchr(resource, '/')) == NULL || | |
308 | pc_file_name == resource) | |
309 | { | |
310 | fprintf(stderr, "ERROR: Bad PPD name \"%s\"!\n", name); | |
311 | ||
312 | if (request_id) | |
313 | { | |
314 | snprintf(message, sizeof(message), "Bad PPD name \"%s\"!", name); | |
315 | ||
316 | cupsdSendIPPHeader(IPP_NOT_FOUND, request_id); | |
317 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
318 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
319 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", | |
320 | "en-US"); | |
321 | cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message); | |
322 | cupsdSendIPPTrailer(); | |
323 | } | |
324 | ||
325 | return (1); | |
326 | } | |
327 | ||
328 | *pc_file_name++ = '\0'; | |
329 | ||
330 | #ifdef __APPLE__ | |
18ecb428 MS |
331 | if (!strncmp(resource, "/Library/Printers/PPDs/Contents/Resources/", 42) || |
332 | !strncmp(resource, "/System/Library/Printers/PPDs/Contents/Resources/", 49)) | |
4509bb49 MS |
333 | strlcpy(filename, resource, sizeof(filename)); |
334 | else | |
335 | #endif // __APPLE__ | |
336 | { | |
337 | snprintf(filename, sizeof(filename), "%s/drv%s", datadir, resource); | |
338 | if (access(filename, 0)) | |
339 | snprintf(filename, sizeof(filename), "%s/model%s", datadir, resource); | |
340 | } | |
341 | ||
342 | src = new ppdcSource(filename); | |
343 | ||
344 | for (d = (ppdcDriver *)src->drivers->first(); | |
345 | d; | |
346 | d = (ppdcDriver *)src->drivers->next()) | |
d1c13e16 MS |
347 | if (!strcmp(pc_file_name, d->pc_file_name->value) || |
348 | (d->file_name && !strcmp(pc_file_name, d->file_name->value))) | |
4509bb49 MS |
349 | break; |
350 | ||
351 | if (d) | |
352 | { | |
353 | ppdcArray *locales; // Locale names | |
354 | ppdcCatalog *catalog; // Message catalog in .drv file | |
355 | ||
356 | ||
f0ab5bff | 357 | fprintf(stderr, "DEBUG2: [cups-driverd] %d locales defined in \"%s\"...\n", |
4509bb49 MS |
358 | src->po_files->count, filename); |
359 | ||
360 | locales = new ppdcArray(); | |
361 | for (catalog = (ppdcCatalog *)src->po_files->first(); | |
362 | catalog; | |
363 | catalog = (ppdcCatalog *)src->po_files->next()) | |
364 | { | |
f0ab5bff | 365 | fprintf(stderr, "DEBUG2: [cups-driverd] Adding locale \"%s\"...\n", |
4509bb49 | 366 | catalog->locale->value); |
97c9a8d7 | 367 | catalog->locale->retain(); |
4509bb49 MS |
368 | locales->add(catalog->locale); |
369 | } | |
370 | ||
371 | if (request_id) | |
372 | { | |
373 | cupsdSendIPPHeader(IPP_OK, request_id); | |
374 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
375 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
376 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", | |
377 | "en-US"); | |
378 | cupsdSendIPPTrailer(); | |
379 | fflush(stdout); | |
380 | } | |
381 | ||
382 | out = cupsFileStdout(); | |
383 | d->write_ppd_file(out, NULL, locales, src, PPDC_LFONLY); | |
384 | cupsFileClose(out); | |
385 | ||
e4572d57 | 386 | locales->release(); |
4509bb49 MS |
387 | } |
388 | else | |
389 | { | |
390 | fprintf(stderr, "ERROR: PPD \"%s\" not found!\n", name); | |
391 | ||
392 | if (request_id) | |
393 | { | |
394 | snprintf(message, sizeof(message), "PPD \"%s\" not found!", name); | |
395 | ||
396 | cupsdSendIPPHeader(IPP_NOT_FOUND, request_id); | |
397 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
398 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
399 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", | |
400 | "en-US"); | |
401 | cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message); | |
402 | cupsdSendIPPTrailer(); | |
403 | } | |
404 | } | |
405 | ||
e4572d57 | 406 | src->release(); |
4509bb49 MS |
407 | |
408 | return (!d); | |
409 | } | |
410 | ||
411 | ||
ef416fc2 | 412 | /* |
413 | * 'cat_ppd()' - Copy a PPD file to stdout. | |
414 | */ | |
415 | ||
bd7854cb | 416 | static int /* O - Exit code */ |
b94498cf | 417 | cat_ppd(const char *name, /* I - PPD name */ |
418 | int request_id) /* I - Request ID for response? */ | |
ef416fc2 | 419 | { |
420 | char scheme[256], /* Scheme from PPD name */ | |
4509bb49 MS |
421 | *sptr, /* Pointer into scheme */ |
422 | line[1024], /* Line/filename */ | |
423 | message[2048]; /* status-message */ | |
ef416fc2 | 424 | |
425 | ||
426 | /* | |
427 | * Figure out if this is a static or dynamic PPD file... | |
428 | */ | |
429 | ||
430 | strlcpy(scheme, name, sizeof(scheme)); | |
431 | if ((sptr = strchr(scheme, ':')) != NULL) | |
432 | { | |
433 | *sptr = '\0'; | |
434 | ||
435 | if (!strcmp(scheme, "file")) | |
436 | { | |
437 | /* | |
438 | * "file:name" == "name"... | |
439 | */ | |
440 | ||
441 | name += 5; | |
442 | scheme[0] = '\0'; | |
443 | } | |
444 | } | |
445 | else | |
446 | scheme[0] = '\0'; | |
447 | ||
09a101d6 | 448 | if (request_id > 0) |
449 | puts("Content-Type: application/ipp\n"); | |
b94498cf | 450 | |
4509bb49 MS |
451 | if (!scheme[0]) |
452 | return (cat_static(name, request_id)); | |
453 | else if (!strcmp(scheme, "drv")) | |
454 | return (cat_drv(name, request_id)); | |
455 | else | |
ef416fc2 | 456 | { |
457 | /* | |
458 | * Dynamic PPD, see if we have a driver program to support it... | |
459 | */ | |
460 | ||
461 | const char *serverbin; /* CUPS_SERVERBIN env var */ | |
c934a06c | 462 | char *argv[4]; /* Arguments for program */ |
ef416fc2 | 463 | |
464 | ||
465 | if ((serverbin = getenv("CUPS_SERVERBIN")) == NULL) | |
466 | serverbin = CUPS_SERVERBIN; | |
467 | ||
468 | snprintf(line, sizeof(line), "%s/driver/%s", serverbin, scheme); | |
469 | if (access(line, X_OK)) | |
470 | { | |
471 | /* | |
472 | * File does not exist or is not executable... | |
473 | */ | |
474 | ||
475 | fprintf(stderr, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n", | |
476 | line, strerror(errno)); | |
b94498cf | 477 | |
478 | if (request_id > 0) | |
479 | { | |
480 | snprintf(message, sizeof(message), "Unable to access \"%s\" - %s", | |
481 | line, strerror(errno)); | |
482 | ||
483 | cupsdSendIPPHeader(IPP_NOT_FOUND, request_id); | |
484 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
485 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
486 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", | |
487 | "en-US"); | |
488 | cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message); | |
489 | cupsdSendIPPTrailer(); | |
490 | } | |
491 | ||
ef416fc2 | 492 | return (1); |
493 | } | |
494 | ||
495 | /* | |
496 | * Yes, let it cat the PPD file... | |
497 | */ | |
498 | ||
b94498cf | 499 | if (request_id) |
500 | { | |
501 | cupsdSendIPPHeader(IPP_OK, request_id); | |
502 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
503 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
504 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", | |
505 | "en-US"); | |
506 | cupsdSendIPPTrailer(); | |
507 | } | |
508 | ||
c934a06c MS |
509 | argv[0] = scheme; |
510 | argv[1] = (char *)"cat"; | |
511 | argv[2] = (char *)name; | |
512 | argv[3] = NULL; | |
513 | ||
514 | if (cupsdExec(line, argv)) | |
ef416fc2 | 515 | { |
516 | /* | |
517 | * Unable to execute driver... | |
518 | */ | |
519 | ||
520 | fprintf(stderr, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n", | |
521 | line, strerror(errno)); | |
522 | return (1); | |
523 | } | |
524 | } | |
ef416fc2 | 525 | |
4509bb49 MS |
526 | /* |
527 | * Return with no errors... | |
528 | */ | |
ef416fc2 | 529 | |
4509bb49 MS |
530 | return (0); |
531 | } | |
ef416fc2 | 532 | |
b94498cf | 533 | |
4509bb49 MS |
534 | /* |
535 | * 'copy_static()' - Copy a static PPD file to stdout. | |
536 | */ | |
b94498cf | 537 | |
4509bb49 MS |
538 | static int /* O - Exit code */ |
539 | cat_static(const char *name, /* I - PPD name */ | |
540 | int request_id) /* I - Request ID for response? */ | |
541 | { | |
542 | cups_file_t *fp; /* PPD file */ | |
543 | const char *datadir; /* CUPS_DATADIR env var */ | |
544 | char line[1024], /* Line/filename */ | |
545 | message[2048]; /* status-message */ | |
b94498cf | 546 | |
ef416fc2 | 547 | |
4509bb49 MS |
548 | if (name[0] == '/' || strstr(name, "../") || strstr(name, "/..")) |
549 | { | |
ef416fc2 | 550 | /* |
4509bb49 | 551 | * Bad name... |
ef416fc2 | 552 | */ |
553 | ||
4509bb49 MS |
554 | fprintf(stderr, "ERROR: [cups-driverd] Bad PPD name \"%s\"!\n", name); |
555 | ||
556 | if (request_id) | |
b94498cf | 557 | { |
4509bb49 | 558 | snprintf(message, sizeof(message), "Bad PPD name \"%s\"!", name); |
b94498cf | 559 | |
4509bb49 MS |
560 | cupsdSendIPPHeader(IPP_NOT_FOUND, request_id); |
561 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
562 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
563 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", | |
564 | "en-US"); | |
565 | cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message); | |
566 | cupsdSendIPPTrailer(); | |
b94498cf | 567 | } |
b94498cf | 568 | |
4509bb49 MS |
569 | return (1); |
570 | } | |
b94498cf | 571 | |
4509bb49 MS |
572 | /* |
573 | * Try opening the file... | |
574 | */ | |
b94498cf | 575 | |
4509bb49 MS |
576 | #ifdef __APPLE__ |
577 | if (!strncmp(name, "System/Library/Printers/PPDs/Contents/Resources/", 48) || | |
578 | !strncmp(name, "Library/Printers/PPDs/Contents/Resources/", 41)) | |
579 | { | |
580 | /* | |
581 | * Map ppd-name to Mac OS X standard locations... | |
582 | */ | |
b94498cf | 583 | |
4509bb49 MS |
584 | snprintf(line, sizeof(line), "/%s", name); |
585 | } | |
586 | else | |
b94498cf | 587 | |
4509bb49 MS |
588 | #elif defined(__linux) |
589 | if (!strncmp(name, "lsb/usr/", 8)) | |
590 | { | |
591 | /* | |
592 | * Map ppd-name to LSB standard /usr/share/ppd location... | |
593 | */ | |
b94498cf | 594 | |
4509bb49 MS |
595 | snprintf(line, sizeof(line), "/usr/share/ppd/%s", name + 8); |
596 | } | |
597 | else if (!strncmp(name, "lsb/opt/", 8)) | |
598 | { | |
599 | /* | |
600 | * Map ppd-name to LSB standard /opt/share/ppd location... | |
601 | */ | |
ef416fc2 | 602 | |
4509bb49 MS |
603 | snprintf(line, sizeof(line), "/opt/share/ppd/%s", name + 8); |
604 | } | |
605 | else if (!strncmp(name, "lsb/local/", 10)) | |
606 | { | |
607 | /* | |
608 | * Map ppd-name to LSB standard /usr/local/share/ppd location... | |
609 | */ | |
b94498cf | 610 | |
4509bb49 MS |
611 | snprintf(line, sizeof(line), "/usr/local/share/ppd/%s", name + 10); |
612 | } | |
613 | else | |
b94498cf | 614 | |
4509bb49 MS |
615 | #endif /* __APPLE__ */ |
616 | { | |
617 | if ((datadir = getenv("CUPS_DATADIR")) == NULL) | |
618 | datadir = CUPS_DATADIR; | |
b94498cf | 619 | |
4509bb49 MS |
620 | snprintf(line, sizeof(line), "%s/model/%s", datadir, name); |
621 | } | |
622 | ||
623 | if ((fp = cupsFileOpen(line, "r")) == NULL) | |
624 | { | |
625 | fprintf(stderr, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n", | |
626 | line, strerror(errno)); | |
ef416fc2 | 627 | |
b94498cf | 628 | if (request_id) |
629 | { | |
4509bb49 MS |
630 | snprintf(message, sizeof(message), "Unable to open \"%s\" - %s", |
631 | line, strerror(errno)); | |
632 | ||
633 | cupsdSendIPPHeader(IPP_NOT_FOUND, request_id); | |
b94498cf | 634 | cupsdSendIPPGroup(IPP_TAG_OPERATION); |
635 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
636 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", | |
4509bb49 MS |
637 | "en-US"); |
638 | cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message); | |
b94498cf | 639 | cupsdSendIPPTrailer(); |
640 | } | |
641 | ||
4509bb49 MS |
642 | return (1); |
643 | } | |
ef416fc2 | 644 | |
4509bb49 MS |
645 | if (request_id) |
646 | { | |
647 | cupsdSendIPPHeader(IPP_OK, request_id); | |
648 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
649 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
650 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", | |
651 | "en-US"); | |
652 | cupsdSendIPPTrailer(); | |
ef416fc2 | 653 | } |
654 | ||
655 | /* | |
4509bb49 | 656 | * Now copy the file to stdout... |
ef416fc2 | 657 | */ |
658 | ||
4509bb49 MS |
659 | while (cupsFileGets(fp, line, sizeof(line))) |
660 | puts(line); | |
661 | ||
662 | cupsFileClose(fp); | |
663 | ||
ef416fc2 | 664 | return (0); |
665 | } | |
666 | ||
667 | ||
178cb736 MS |
668 | /* |
669 | * 'compare_inodes()' - Compare two inodes. | |
670 | */ | |
671 | ||
672 | static int /* O - Result of comparison */ | |
673 | compare_inodes(struct stat *a, /* I - First inode */ | |
674 | struct stat *b) /* I - Second inode */ | |
675 | { | |
676 | if (a->st_dev != b->st_dev) | |
677 | return (a->st_dev - b->st_dev); | |
678 | else | |
679 | return (a->st_ino - b->st_ino); | |
680 | } | |
681 | ||
682 | ||
58dc1933 MS |
683 | /* |
684 | * 'compare_matches()' - Compare PPD match scores for sorting. | |
685 | */ | |
686 | ||
687 | static int | |
688 | compare_matches(const ppd_info_t *p0, /* I - First PPD */ | |
689 | const ppd_info_t *p1) /* I - Second PPD */ | |
690 | { | |
691 | if (p1->matches != p0->matches) | |
692 | return (p1->matches - p0->matches); | |
693 | else | |
e4572d57 MS |
694 | return (cupsdCompareNames(p0->record.make_and_model, |
695 | p1->record.make_and_model)); | |
58dc1933 MS |
696 | } |
697 | ||
698 | ||
ef416fc2 | 699 | /* |
700 | * 'compare_names()' - Compare PPD filenames for sorting. | |
701 | */ | |
702 | ||
bd7854cb | 703 | static int /* O - Result of comparison */ |
ef416fc2 | 704 | compare_names(const ppd_info_t *p0, /* I - First PPD file */ |
705 | const ppd_info_t *p1) /* I - Second PPD file */ | |
706 | { | |
66ab9486 MS |
707 | int diff; /* Difference between strings */ |
708 | ||
709 | ||
d1c13e16 | 710 | if ((diff = strcmp(p0->record.filename, p1->record.filename)) != 0) |
66ab9486 MS |
711 | return (diff); |
712 | else | |
d1c13e16 | 713 | return (strcmp(p0->record.name, p1->record.name)); |
ef416fc2 | 714 | } |
715 | ||
716 | ||
717 | /* | |
718 | * 'compare_ppds()' - Compare PPD file make and model names for sorting. | |
719 | */ | |
720 | ||
bd7854cb | 721 | static int /* O - Result of comparison */ |
ef416fc2 | 722 | compare_ppds(const ppd_info_t *p0, /* I - First PPD file */ |
723 | const ppd_info_t *p1) /* I - Second PPD file */ | |
724 | { | |
725 | int diff; /* Difference between strings */ | |
726 | ||
bd7854cb | 727 | |
ef416fc2 | 728 | /* |
729 | * First compare manufacturers... | |
730 | */ | |
731 | ||
732 | if ((diff = strcasecmp(p0->record.make, p1->record.make)) != 0) | |
733 | return (diff); | |
734 | else if ((diff = cupsdCompareNames(p0->record.make_and_model, | |
735 | p1->record.make_and_model)) != 0) | |
736 | return (diff); | |
737 | else | |
d1c13e16 | 738 | return (strcmp(p0->record.languages[0], p1->record.languages[0])); |
b94498cf | 739 | } |
740 | ||
741 | ||
f0ab5bff MS |
742 | /* |
743 | * 'dump_ppds_dat()' - Dump the contents of the ppds.dat file. | |
744 | */ | |
745 | ||
746 | static int /* O - Exit status */ | |
747 | dump_ppds_dat(void) | |
748 | { | |
749 | char filename[1024]; /* ppds.dat filename */ | |
750 | ppd_info_t *ppd; /* Current PPD */ | |
751 | ||
752 | ||
753 | /* | |
754 | * See if we a PPD database file... | |
755 | */ | |
756 | ||
757 | load_ppds_dat(filename, sizeof(filename), 0); | |
758 | ||
759 | puts("mtime,size,model_number,type,filename,name,languages0,products0," | |
760 | "psversions0,make,make_and_model,device_id,scheme"); | |
761 | for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName); | |
762 | ppd; | |
763 | ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) | |
764 | printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"," | |
765 | "\"%s\",\"%s\"\n", | |
766 | (int)ppd->record.mtime, (long)ppd->record.size, | |
767 | ppd->record.model_number, ppd->record.type, ppd->record.filename, | |
768 | ppd->record.name, ppd->record.languages[0], ppd->record.products[0], | |
769 | ppd->record.psversions[0], ppd->record.make, | |
770 | ppd->record.make_and_model, ppd->record.device_id, | |
771 | ppd->record.scheme); | |
772 | ||
773 | return (0); | |
774 | } | |
775 | ||
776 | ||
b94498cf | 777 | /* |
778 | * 'free_array()' - Free an array of strings. | |
779 | */ | |
780 | ||
781 | static void | |
782 | free_array(cups_array_t *a) /* I - Array to free */ | |
783 | { | |
784 | char *ptr; /* Pointer to string */ | |
785 | ||
786 | ||
787 | for (ptr = (char *)cupsArrayFirst(a); | |
788 | ptr; | |
789 | ptr = (char *)cupsArrayNext(a)) | |
790 | free(ptr); | |
791 | ||
792 | cupsArrayDelete(a); | |
ef416fc2 | 793 | } |
794 | ||
795 | ||
796 | /* | |
797 | * 'list_ppds()' - List PPD files. | |
798 | */ | |
799 | ||
bd7854cb | 800 | static int /* O - Exit code */ |
ef416fc2 | 801 | list_ppds(int request_id, /* I - Request ID */ |
802 | int limit, /* I - Limit */ | |
803 | const char *opt) /* I - Option argument */ | |
804 | { | |
66ab9486 | 805 | int i; /* Looping vars */ |
ef416fc2 | 806 | int count; /* Number of PPDs to send */ |
807 | ppd_info_t *ppd; /* Current PPD file */ | |
808 | cups_file_t *fp; /* ppds.dat file */ | |
ef416fc2 | 809 | char filename[1024], /* ppds.dat filename */ |
810 | model[1024]; /* Model directory */ | |
ef416fc2 | 811 | const char *cups_datadir; /* CUPS_DATADIR environment variable */ |
812 | int num_options; /* Number of options */ | |
813 | cups_option_t *options; /* Options */ | |
ed6e7faf MS |
814 | cups_array_t *requested, /* requested-attributes values */ |
815 | *include, /* PPD schemes to include */ | |
816 | *exclude; /* PPD schemes to exclude */ | |
817 | const char *device_id, /* ppd-device-id option */ | |
b94498cf | 818 | *language, /* ppd-natural-language option */ |
819 | *make, /* ppd-make option */ | |
820 | *make_and_model, /* ppd-make-and-model option */ | |
3d8365b8 | 821 | *model_number_str, /* ppd-model-number option */ |
b94498cf | 822 | *product, /* ppd-product option */ |
3d8365b8 | 823 | *psversion, /* ppd-psversion option */ |
824 | *type_str; /* ppd-type option */ | |
825 | int model_number, /* ppd-model-number value */ | |
826 | type, /* ppd-type value */ | |
58dc1933 MS |
827 | make_and_model_len, /* Length of ppd-make-and-model */ |
828 | product_len, /* Length of ppd-product */ | |
3d8365b8 | 829 | send_device_id, /* Send ppd-device-id? */ |
b94498cf | 830 | send_make, /* Send ppd-make? */ |
831 | send_make_and_model, /* Send ppd-make-and-model? */ | |
3d8365b8 | 832 | send_model_number, /* Send ppd-model-number? */ |
b94498cf | 833 | send_name, /* Send ppd-name? */ |
3d8365b8 | 834 | send_natural_language, /* Send ppd-natural-language? */ |
b94498cf | 835 | send_product, /* Send ppd-product? */ |
836 | send_psversion, /* Send ppd-psversion? */ | |
3d8365b8 | 837 | send_type, /* Send ppd-type? */ |
b94498cf | 838 | sent_header; /* Sent the IPP header? */ |
58dc1933 MS |
839 | regex_t *device_id_re, /* Regular expression for matching device ID */ |
840 | *make_and_model_re; /* Regular expression for matching make and model */ | |
841 | regmatch_t re_matches[6]; /* Regular expression matches */ | |
842 | cups_array_t *matches; /* Matching PPDs */ | |
b94498cf | 843 | |
844 | ||
845 | fprintf(stderr, | |
846 | "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, " | |
847 | "opt=\"%s\"\n", request_id, limit, opt); | |
ef416fc2 | 848 | |
849 | /* | |
850 | * See if we a PPD database file... | |
851 | */ | |
852 | ||
f0ab5bff | 853 | load_ppds_dat(filename, sizeof(filename), 1); |
ef416fc2 | 854 | |
ef416fc2 | 855 | /* |
856 | * Load all PPDs in the specified directory and below... | |
857 | */ | |
858 | ||
ef416fc2 | 859 | if ((cups_datadir = getenv("CUPS_DATADIR")) == NULL) |
860 | cups_datadir = CUPS_DATADIR; | |
861 | ||
178cb736 MS |
862 | Inodes = cupsArrayNew((cups_array_func_t)compare_inodes, NULL); |
863 | ||
ef416fc2 | 864 | snprintf(model, sizeof(model), "%s/model", cups_datadir); |
b94498cf | 865 | load_ppds(model, "", 1); |
866 | ||
4509bb49 MS |
867 | snprintf(model, sizeof(model), "%s/drv", cups_datadir); |
868 | load_ppds(model, "", 1); | |
869 | ||
b94498cf | 870 | #ifdef __APPLE__ |
871 | /* | |
872 | * Load PPDs from standard Mac OS X locations... | |
873 | */ | |
874 | ||
875 | load_ppds("/Library/Printers/PPDs/Contents/Resources", | |
876 | "Library/Printers/PPDs/Contents/Resources", 0); | |
877 | load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj", | |
878 | "Library/Printers/PPDs/Contents/Resources/en.lproj", 0); | |
879 | load_ppds("/System/Library/Printers/PPDs/Contents/Resources", | |
880 | "System/Library/Printers/PPDs/Contents/Resources", 0); | |
881 | load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj", | |
882 | "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0); | |
883 | ||
884 | #elif defined(__linux) | |
885 | /* | |
886 | * Load PPDs from LSB-defined locations... | |
887 | */ | |
888 | ||
568fa3fa MS |
889 | if (!access("/usr/local/share/ppd", 0)) |
890 | load_ppds("/usr/local/share/ppd", "lsb/local", 1); | |
891 | if (!access("/usr/share/ppd", 0)) | |
892 | load_ppds("/usr/share/ppd", "lsb/usr", 1); | |
893 | if (!access("/opt/share/ppd", 0)) | |
894 | load_ppds("/opt/share/ppd", "lsb/opt", 1); | |
b94498cf | 895 | #endif /* __APPLE__ */ |
ef416fc2 | 896 | |
897 | /* | |
898 | * Cull PPD files that are no longer present... | |
899 | */ | |
900 | ||
66ab9486 MS |
901 | for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName); |
902 | ppd; | |
903 | ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) | |
ef416fc2 | 904 | if (!ppd->found) |
905 | { | |
906 | /* | |
907 | * Remove this PPD file from the list... | |
908 | */ | |
909 | ||
66ab9486 MS |
910 | cupsArrayRemove(PPDsByName, ppd); |
911 | cupsArrayRemove(PPDsByMakeModel, ppd); | |
912 | free(ppd); | |
f0ab5bff MS |
913 | |
914 | ChangedPPD = 1; | |
ef416fc2 | 915 | } |
916 | ||
ef416fc2 | 917 | /* |
918 | * Write the new ppds.dat file... | |
919 | */ | |
920 | ||
f0ab5bff MS |
921 | fprintf(stderr, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD); |
922 | ||
ef416fc2 | 923 | if (ChangedPPD) |
924 | { | |
925 | if ((fp = cupsFileOpen(filename, "w")) != NULL) | |
926 | { | |
b94498cf | 927 | unsigned ppdsync = PPD_SYNC; /* Sync word */ |
928 | ||
929 | ||
930 | cupsFileWrite(fp, (char *)&ppdsync, sizeof(ppdsync)); | |
931 | ||
66ab9486 MS |
932 | for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName); |
933 | ppd; | |
934 | ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) | |
ef416fc2 | 935 | cupsFileWrite(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)); |
936 | ||
937 | cupsFileClose(fp); | |
938 | ||
939 | fprintf(stderr, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n", | |
66ab9486 | 940 | filename, cupsArrayCount(PPDsByName)); |
ef416fc2 | 941 | } |
942 | else | |
943 | fprintf(stderr, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n", | |
944 | filename, strerror(errno)); | |
945 | } | |
946 | else | |
947 | fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr); | |
948 | ||
949 | /* | |
950 | * Scan for dynamic PPD files... | |
951 | */ | |
952 | ||
ed6e7faf MS |
953 | num_options = cupsParseOptions(opt, 0, &options); |
954 | exclude = cupsdCreateStringsArray(cupsGetOption("exclude-schemes", | |
955 | num_options, options)); | |
8b450588 | 956 | include = cupsdCreateStringsArray(cupsGetOption("include-schemes", |
ed6e7faf MS |
957 | num_options, options)); |
958 | ||
959 | load_drivers(include, exclude); | |
ef416fc2 | 960 | |
961 | /* | |
962 | * Add the raw driver... | |
963 | */ | |
964 | ||
66ab9486 | 965 | add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0, |
ed6e7faf | 966 | PPD_TYPE_UNKNOWN, "raw"); |
ef416fc2 | 967 | |
ef416fc2 | 968 | /* |
969 | * Send IPP attributes... | |
970 | */ | |
971 | ||
ed6e7faf MS |
972 | requested = cupsdCreateStringsArray( |
973 | cupsGetOption("requested-attributes", num_options, | |
974 | options)); | |
3d8365b8 | 975 | device_id = cupsGetOption("ppd-device-id", num_options, options); |
976 | language = cupsGetOption("ppd-natural-language", num_options, options); | |
977 | make = cupsGetOption("ppd-make", num_options, options); | |
978 | make_and_model = cupsGetOption("ppd-make-and-model", num_options, options); | |
979 | model_number_str = cupsGetOption("ppd-model-number", num_options, options); | |
980 | product = cupsGetOption("ppd-product", num_options, options); | |
981 | psversion = cupsGetOption("ppd-psversion", num_options, options); | |
982 | type_str = cupsGetOption("ppd-type", num_options, options); | |
b94498cf | 983 | |
984 | if (make_and_model) | |
58dc1933 | 985 | make_and_model_len = strlen(make_and_model); |
b94498cf | 986 | else |
58dc1933 | 987 | make_and_model_len = 0; |
ef416fc2 | 988 | |
58dc1933 MS |
989 | if (product) |
990 | product_len = strlen(product); | |
b94498cf | 991 | else |
58dc1933 | 992 | product_len = 0; |
b94498cf | 993 | |
3d8365b8 | 994 | if (model_number_str) |
995 | model_number = atoi(model_number_str); | |
996 | else | |
997 | model_number = 0; | |
998 | ||
999 | if (type_str) | |
1000 | { | |
1001 | for (type = 0; | |
1002 | type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0])); | |
1003 | type ++) | |
1004 | if (!strcmp(type_str, ppd_types[type])) | |
1005 | break; | |
1006 | ||
1007 | if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0]))) | |
1008 | { | |
1009 | fprintf(stderr, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n", | |
1010 | type_str); | |
1011 | type_str = NULL; | |
1012 | } | |
1013 | } | |
bc44d920 | 1014 | else |
1015 | type = 0; | |
3d8365b8 | 1016 | |
ed6e7faf | 1017 | for (i = 0; i < num_options; i ++) |
f0ab5bff | 1018 | fprintf(stderr, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options[i].name, |
ed6e7faf | 1019 | options[i].value); |
ef416fc2 | 1020 | |
ed6e7faf | 1021 | if (!requested || cupsArrayFind(requested, (void *)"all") != NULL) |
ef416fc2 | 1022 | { |
1023 | send_name = 1; | |
1024 | send_make = 1; | |
1025 | send_make_and_model = 1; | |
3d8365b8 | 1026 | send_model_number = 1; |
ef416fc2 | 1027 | send_natural_language = 1; |
bd7854cb | 1028 | send_device_id = 1; |
f899b121 | 1029 | send_product = 1; |
b94498cf | 1030 | send_psversion = 1; |
3d8365b8 | 1031 | send_type = 1; |
ef416fc2 | 1032 | } |
1033 | else | |
1034 | { | |
ed6e7faf MS |
1035 | send_name = cupsArrayFind(requested, |
1036 | (void *)"ppd-name") != NULL; | |
1037 | send_make = cupsArrayFind(requested, | |
1038 | (void *)"ppd-make") != NULL; | |
1039 | send_make_and_model = cupsArrayFind(requested, | |
1040 | (void *)"ppd-make-and-model") != NULL; | |
1041 | send_model_number = cupsArrayFind(requested, | |
1042 | (void *)"ppd-model-number") != NULL; | |
1043 | send_natural_language = cupsArrayFind(requested, | |
1044 | (void *)"ppd-natural-language") != NULL; | |
1045 | send_device_id = cupsArrayFind(requested, | |
1046 | (void *)"ppd-device-id") != NULL; | |
1047 | send_product = cupsArrayFind(requested, | |
1048 | (void *)"ppd-product") != NULL; | |
1049 | send_psversion = cupsArrayFind(requested, | |
1050 | (void *)"ppd-psversion") != NULL; | |
1051 | send_type = cupsArrayFind(requested, | |
1052 | (void *)"ppd-type") != NULL; | |
ef416fc2 | 1053 | } |
1054 | ||
f0ab5bff MS |
1055 | /* |
1056 | * Send the content type header to the scheduler; request_id can only be | |
1057 | * 0 when run manually since the scheduler enforces the IPP requirement for | |
1058 | * a request ID from 1 to 2^31-1... | |
1059 | */ | |
1060 | ||
1061 | if (request_id > 0) | |
1062 | puts("Content-Type: application/ipp\n"); | |
ef416fc2 | 1063 | |
b94498cf | 1064 | sent_header = 0; |
ef416fc2 | 1065 | |
66ab9486 MS |
1066 | if (limit <= 0 || limit > cupsArrayCount(PPDsByMakeModel)) |
1067 | count = cupsArrayCount(PPDsByMakeModel); | |
ef416fc2 | 1068 | else |
1069 | count = limit; | |
1070 | ||
58dc1933 | 1071 | if (device_id || language || make || make_and_model || model_number_str || |
8b450588 | 1072 | product) |
b94498cf | 1073 | { |
58dc1933 | 1074 | matches = cupsArrayNew((cups_array_func_t)compare_matches, NULL); |
b94498cf | 1075 | |
58dc1933 MS |
1076 | if (device_id) |
1077 | device_id_re = regex_device_id(device_id); | |
1078 | else | |
1079 | device_id_re = NULL; | |
66ab9486 | 1080 | |
58dc1933 MS |
1081 | if (make_and_model) |
1082 | make_and_model_re = regex_string(make_and_model); | |
1083 | else | |
1084 | make_and_model_re = NULL; | |
b94498cf | 1085 | |
58dc1933 MS |
1086 | for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel); |
1087 | ppd; | |
1088 | ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel)) | |
ef416fc2 | 1089 | { |
58dc1933 MS |
1090 | /* |
1091 | * Filter PPDs based on make, model, product, language, model number, | |
1092 | * and/or device ID using the "matches" score value. An exact match | |
1093 | * for product, make-and-model, or device-id adds 3 to the score. | |
1094 | * Partial matches for make-and-model yield 1 or 2 points, and matches | |
1095 | * for the make and language add a single point. Results are then sorted | |
1096 | * by score, highest score first. | |
1097 | */ | |
b94498cf | 1098 | |
58dc1933 MS |
1099 | if (ppd->record.type < PPD_TYPE_POSTSCRIPT || |
1100 | ppd->record.type >= PPD_TYPE_DRV) | |
b94498cf | 1101 | continue; |
b94498cf | 1102 | |
ed6e7faf MS |
1103 | if (cupsArrayFind(exclude, ppd->record.scheme) || |
1104 | (include && !cupsArrayFind(include, ppd->record.scheme))) | |
1105 | continue; | |
1106 | ||
58dc1933 | 1107 | ppd->matches = 0; |
b94498cf | 1108 | |
58dc1933 MS |
1109 | if (device_id_re && |
1110 | !regexec(device_id_re, ppd->record.device_id, | |
1111 | (int)(sizeof(re_matches) / sizeof(re_matches[0])), | |
1112 | re_matches, 0)) | |
1113 | { | |
1114 | /* | |
1115 | * Add the number of matching values from the device ID - it will be | |
1116 | * at least 2 (manufacturer and model), and as much as 3 (command set). | |
1117 | */ | |
b94498cf | 1118 | |
58dc1933 MS |
1119 | for (i = 1; i < (int)(sizeof(re_matches) / sizeof(re_matches[0])); i ++) |
1120 | if (re_matches[i].rm_so >= 0) | |
1121 | ppd->matches ++; | |
1122 | } | |
3d8365b8 | 1123 | |
58dc1933 MS |
1124 | if (language) |
1125 | { | |
1126 | for (i = 0; i < PPD_MAX_LANG; i ++) | |
238c3832 MS |
1127 | if (!ppd->record.languages[i][0]) |
1128 | break; | |
1129 | else if (!strcmp(ppd->record.languages[i], language)) | |
58dc1933 MS |
1130 | { |
1131 | ppd->matches ++; | |
1132 | break; | |
1133 | } | |
1134 | } | |
ef416fc2 | 1135 | |
58dc1933 MS |
1136 | if (make && !strcasecmp(ppd->record.make, make)) |
1137 | ppd->matches ++; | |
ef416fc2 | 1138 | |
58dc1933 MS |
1139 | if (make_and_model_re && |
1140 | !regexec(make_and_model_re, ppd->record.make_and_model, | |
1141 | (int)(sizeof(re_matches) / sizeof(re_matches[0])), | |
1142 | re_matches, 0)) | |
1143 | { | |
1144 | // See how much of the make-and-model string we matched... | |
1145 | if (re_matches[0].rm_so == 0) | |
1146 | { | |
1147 | if (re_matches[0].rm_eo == make_and_model_len) | |
1148 | ppd->matches += 3; // Exact match | |
1149 | else | |
1150 | ppd->matches += 2; // Prefix match | |
1151 | } | |
1152 | else | |
1153 | ppd->matches ++; // Infix match | |
1154 | } | |
ef416fc2 | 1155 | |
58dc1933 MS |
1156 | if (model_number_str && ppd->record.model_number == model_number) |
1157 | ppd->matches ++; | |
1158 | ||
1159 | if (product) | |
1160 | { | |
1161 | for (i = 0; i < PPD_MAX_PROD; i ++) | |
238c3832 MS |
1162 | if (!ppd->record.products[i][0]) |
1163 | break; | |
1164 | else if (!strcasecmp(ppd->record.products[i], product)) | |
58dc1933 MS |
1165 | { |
1166 | ppd->matches += 3; | |
1167 | break; | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | if (psversion) | |
1172 | { | |
1173 | for (i = 0; i < PPD_MAX_VERS; i ++) | |
238c3832 MS |
1174 | if (!ppd->record.psversions[i][0]) |
1175 | break; | |
1176 | else if (!strcasecmp(ppd->record.psversions[i], psversion)) | |
58dc1933 MS |
1177 | { |
1178 | ppd->matches ++; | |
1179 | break; | |
1180 | } | |
1181 | } | |
1182 | ||
1183 | if (type_str && ppd->record.type == type) | |
1184 | ppd->matches ++; | |
1185 | ||
1186 | if (ppd->matches) | |
1187 | { | |
f0ab5bff | 1188 | fprintf(stderr, "DEBUG2: [cups-driverd] %s matches with score %d!\n", |
58dc1933 MS |
1189 | ppd->record.name, ppd->matches); |
1190 | cupsArrayAdd(matches, ppd); | |
1191 | } | |
b94498cf | 1192 | } |
58dc1933 | 1193 | } |
8b450588 MS |
1194 | else if (include || exclude) |
1195 | { | |
1196 | matches = cupsArrayNew((cups_array_func_t)compare_ppds, NULL); | |
1197 | ||
1198 | for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel); | |
1199 | ppd; | |
1200 | ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel)) | |
1201 | { | |
1202 | /* | |
1203 | * Filter PPDs based on the include/exclude lists. | |
1204 | */ | |
1205 | ||
1206 | if (ppd->record.type < PPD_TYPE_POSTSCRIPT || | |
1207 | ppd->record.type >= PPD_TYPE_DRV) | |
1208 | continue; | |
1209 | ||
1210 | if (cupsArrayFind(exclude, ppd->record.scheme) || | |
1211 | (include && !cupsArrayFind(include, ppd->record.scheme))) | |
1212 | continue; | |
1213 | ||
1214 | cupsArrayAdd(matches, ppd); | |
1215 | } | |
1216 | } | |
58dc1933 MS |
1217 | else |
1218 | matches = PPDsByMakeModel; | |
ef416fc2 | 1219 | |
58dc1933 MS |
1220 | for (ppd = (ppd_info_t *)cupsArrayFirst(matches); |
1221 | count > 0 && ppd; | |
1222 | ppd = (ppd_info_t *)cupsArrayNext(matches)) | |
1223 | { | |
1224 | /* | |
1225 | * Skip invalid PPDs... | |
1226 | */ | |
1227 | ||
1228 | if (ppd->record.type < PPD_TYPE_POSTSCRIPT || | |
1229 | ppd->record.type >= PPD_TYPE_DRV) | |
3d8365b8 | 1230 | continue; |
1231 | ||
b94498cf | 1232 | /* |
1233 | * Send this PPD... | |
1234 | */ | |
ef416fc2 | 1235 | |
b94498cf | 1236 | if (!sent_header) |
1237 | { | |
1238 | sent_header = 1; | |
ef416fc2 | 1239 | |
b94498cf | 1240 | cupsdSendIPPHeader(IPP_OK, request_id); |
1241 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
1242 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
58dc1933 MS |
1243 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", |
1244 | "en-US"); | |
b94498cf | 1245 | } |
ef416fc2 | 1246 | |
f0ab5bff | 1247 | fprintf(stderr, "DEBUG2: [cups-driverd] Sending %s (%s)...\n", |
b94498cf | 1248 | ppd->record.name, ppd->record.make_and_model); |
ef416fc2 | 1249 | |
b94498cf | 1250 | count --; |
bd7854cb | 1251 | |
b94498cf | 1252 | cupsdSendIPPGroup(IPP_TAG_PRINTER); |
f899b121 | 1253 | |
b94498cf | 1254 | if (send_name) |
1255 | cupsdSendIPPString(IPP_TAG_NAME, "ppd-name", ppd->record.name); | |
ef416fc2 | 1256 | |
b94498cf | 1257 | if (send_natural_language) |
1258 | { | |
1259 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "ppd-natural-language", | |
1260 | ppd->record.languages[0]); | |
ef416fc2 | 1261 | |
66ab9486 MS |
1262 | for (i = 1; i < PPD_MAX_LANG && ppd->record.languages[i][0]; i ++) |
1263 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "", ppd->record.languages[i]); | |
b94498cf | 1264 | } |
ef416fc2 | 1265 | |
b94498cf | 1266 | if (send_make) |
1267 | cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make", ppd->record.make); | |
ef416fc2 | 1268 | |
b94498cf | 1269 | if (send_make_and_model) |
1270 | cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make-and-model", | |
1271 | ppd->record.make_and_model); | |
1272 | ||
1273 | if (send_device_id) | |
1274 | cupsdSendIPPString(IPP_TAG_TEXT, "ppd-device-id", | |
1275 | ppd->record.device_id); | |
1276 | ||
1277 | if (send_product) | |
1278 | { | |
1279 | cupsdSendIPPString(IPP_TAG_TEXT, "ppd-product", | |
1280 | ppd->record.products[0]); | |
1281 | ||
66ab9486 MS |
1282 | for (i = 1; i < PPD_MAX_PROD && ppd->record.products[i][0]; i ++) |
1283 | cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.products[i]); | |
b94498cf | 1284 | } |
1285 | ||
1286 | if (send_psversion) | |
1287 | { | |
1288 | cupsdSendIPPString(IPP_TAG_TEXT, "ppd-psversion", | |
1289 | ppd->record.psversions[0]); | |
1290 | ||
66ab9486 MS |
1291 | for (i = 1; i < PPD_MAX_VERS && ppd->record.psversions[i][0]; i ++) |
1292 | cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.psversions[i]); | |
b94498cf | 1293 | } |
1294 | ||
3d8365b8 | 1295 | if (send_type) |
1296 | cupsdSendIPPString(IPP_TAG_KEYWORD, "ppd-type", | |
1297 | ppd_types[ppd->record.type]); | |
1298 | ||
1299 | if (send_model_number) | |
1300 | cupsdSendIPPInteger(IPP_TAG_INTEGER, "ppd-model-number", | |
1301 | ppd->record.model_number); | |
1302 | ||
b94498cf | 1303 | /* |
1304 | * If we have only requested the ppd-make attribute, then skip | |
1305 | * the remaining PPDs with this make... | |
1306 | */ | |
1307 | ||
ed6e7faf MS |
1308 | if (cupsArrayFind(requested, (void *)"ppd-make") && |
1309 | cupsArrayCount(requested) == 1) | |
b94498cf | 1310 | { |
1311 | const char *this_make; /* This ppd-make */ | |
1312 | ||
1313 | ||
66ab9486 | 1314 | for (this_make = ppd->record.make, |
58dc1933 | 1315 | ppd = (ppd_info_t *)cupsArrayNext(matches); |
66ab9486 | 1316 | ppd; |
58dc1933 | 1317 | ppd = (ppd_info_t *)cupsArrayNext(matches)) |
b94498cf | 1318 | if (strcasecmp(this_make, ppd->record.make)) |
1319 | break; | |
1320 | ||
58dc1933 | 1321 | cupsArrayPrev(matches); |
ef416fc2 | 1322 | } |
b94498cf | 1323 | } |
1324 | ||
1325 | if (!sent_header) | |
1326 | { | |
1327 | cupsdSendIPPHeader(IPP_NOT_FOUND, request_id); | |
1328 | cupsdSendIPPGroup(IPP_TAG_OPERATION); | |
1329 | cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8"); | |
1330 | cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US"); | |
1331 | } | |
ef416fc2 | 1332 | |
1333 | cupsdSendIPPTrailer(); | |
1334 | ||
1335 | return (0); | |
1336 | } | |
1337 | ||
1338 | ||
1339 | /* | |
1340 | * 'load_ppds()' - Load PPD files recursively. | |
1341 | */ | |
1342 | ||
bd7854cb | 1343 | static int /* O - 1 on success, 0 on failure */ |
ef416fc2 | 1344 | load_ppds(const char *d, /* I - Actual directory */ |
b94498cf | 1345 | const char *p, /* I - Virtual path in name */ |
1346 | int descend) /* I - Descend into directories? */ | |
ef416fc2 | 1347 | { |
178cb736 MS |
1348 | struct stat dinfo, /* Directory information */ |
1349 | *dinfoptr; /* Pointer to match */ | |
ef416fc2 | 1350 | int i; /* Looping var */ |
1351 | cups_file_t *fp; /* Pointer to file */ | |
1352 | cups_dir_t *dir; /* Directory pointer */ | |
1353 | cups_dentry_t *dent; /* Directory entry */ | |
1354 | char filename[1024], /* Name of PPD or directory */ | |
1355 | line[256], /* Line from backend */ | |
1356 | *ptr, /* Pointer into name */ | |
1357 | name[128], /* Name of PPD file */ | |
80ca4592 | 1358 | lang_version[64], /* PPD LanguageVersion */ |
1359 | lang_encoding[64], /* PPD LanguageEncoding */ | |
ef416fc2 | 1360 | country[64], /* Country code */ |
1361 | manufacturer[256], /* Manufacturer */ | |
1362 | make_model[256], /* Make and Model */ | |
1363 | model_name[256], /* ModelName */ | |
bd7854cb | 1364 | nick_name[256], /* NickName */ |
f899b121 | 1365 | device_id[256], /* 1284DeviceID */ |
b94498cf | 1366 | product[256], /* Product */ |
5eb9da71 MS |
1367 | psversion[256], /* PSVersion */ |
1368 | temp[512]; /* Temporary make and model */ | |
3d8365b8 | 1369 | int model_number, /* cupsModelNumber */ |
1370 | type; /* ppd-type */ | |
b94498cf | 1371 | cups_array_t *products, /* Product array */ |
1372 | *psversions, /* PSVersion array */ | |
1373 | *cups_languages; /* cupsLanguages array */ | |
ef416fc2 | 1374 | ppd_info_t *ppd, /* New PPD file */ |
1375 | key; /* Search key */ | |
1376 | int new_ppd; /* Is this a new PPD? */ | |
1377 | struct /* LanguageVersion translation table */ | |
1378 | { | |
1379 | const char *version, /* LanguageVersion string */ | |
1380 | *language; /* Language code */ | |
1381 | } languages[] = | |
1382 | { | |
dd1abb6b MS |
1383 | { "chinese", "zh" }, |
1384 | { "czech", "cs" }, | |
1385 | { "danish", "da" }, | |
1386 | { "dutch", "nl" }, | |
1387 | { "english", "en" }, | |
1388 | { "finnish", "fi" }, | |
1389 | { "french", "fr" }, | |
1390 | { "german", "de" }, | |
1391 | { "greek", "el" }, | |
1392 | { "hungarian", "hu" }, | |
1393 | { "italian", "it" }, | |
1394 | { "japanese", "ja" }, | |
1395 | { "korean", "ko" }, | |
1396 | { "norwegian", "no" }, | |
1397 | { "polish", "pl" }, | |
1398 | { "portuguese", "pt" }, | |
1399 | { "russian", "ru" }, | |
1400 | { "simplified chinese", "zh_CN" }, | |
1401 | { "slovak", "sk" }, | |
1402 | { "spanish", "es" }, | |
1403 | { "swedish", "sv" }, | |
1404 | { "traditional chinese", "zh_TW" }, | |
1405 | { "turkish", "tr" } | |
ef416fc2 | 1406 | }; |
1407 | ||
1408 | ||
178cb736 MS |
1409 | /* |
1410 | * See if we've loaded this directory before... | |
1411 | */ | |
1412 | ||
1413 | if (stat(d, &dinfo)) | |
1414 | { | |
1415 | if (errno != ENOENT) | |
1416 | fprintf(stderr, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d, | |
1417 | strerror(errno)); | |
1418 | ||
1419 | return (0); | |
1420 | } | |
1421 | else if (cupsArrayFind(Inodes, &dinfo)) | |
1422 | { | |
1423 | fprintf(stderr, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n", | |
1424 | d); | |
1425 | return (0); | |
1426 | } | |
1427 | ||
1428 | /* | |
1429 | * Nope, add it to the Inodes array and continue... | |
1430 | */ | |
1431 | ||
1432 | dinfoptr = (struct stat *)malloc(sizeof(struct stat)); | |
1433 | memcpy(dinfoptr, &dinfo, sizeof(struct stat)); | |
1434 | cupsArrayAdd(Inodes, dinfoptr); | |
4509bb49 | 1435 | |
ef416fc2 | 1436 | if ((dir = cupsDirOpen(d)) == NULL) |
1437 | { | |
839a51c8 MS |
1438 | if (errno != ENOENT) |
1439 | fprintf(stderr, | |
1440 | "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n", | |
1441 | d, strerror(errno)); | |
1442 | ||
ef416fc2 | 1443 | return (0); |
1444 | } | |
1445 | ||
178cb736 MS |
1446 | fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d); |
1447 | ||
ef416fc2 | 1448 | while ((dent = cupsDirRead(dir)) != NULL) |
1449 | { | |
bd7854cb | 1450 | /* |
1451 | * Skip files/directories starting with "."... | |
1452 | */ | |
1453 | ||
1454 | if (dent->filename[0] == '.') | |
1455 | continue; | |
1456 | ||
ef416fc2 | 1457 | /* |
1458 | * See if this is a file... | |
1459 | */ | |
1460 | ||
1461 | snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename); | |
1462 | ||
1463 | if (p[0]) | |
1464 | snprintf(name, sizeof(name), "%s/%s", p, dent->filename); | |
1465 | else | |
1466 | strlcpy(name, dent->filename, sizeof(name)); | |
1467 | ||
1468 | if (S_ISDIR(dent->fileinfo.st_mode)) | |
1469 | { | |
1470 | /* | |
1471 | * Do subdirectory... | |
1472 | */ | |
1473 | ||
b94498cf | 1474 | if (descend) |
1475 | if (!load_ppds(filename, name, 1)) | |
1476 | { | |
1477 | cupsDirClose(dir); | |
1478 | return (1); | |
1479 | } | |
ef416fc2 | 1480 | |
1481 | continue; | |
1482 | } | |
1483 | ||
1484 | /* | |
1485 | * See if this file has been scanned before... | |
1486 | */ | |
1487 | ||
66ab9486 MS |
1488 | strcpy(key.record.filename, name); |
1489 | strcpy(key.record.name, name); | |
ef416fc2 | 1490 | |
66ab9486 | 1491 | ppd = (ppd_info_t *)cupsArrayFind(PPDsByName, &key); |
ef416fc2 | 1492 | |
66ab9486 MS |
1493 | if (ppd && |
1494 | ppd->record.size == dent->fileinfo.st_size && | |
1495 | ppd->record.mtime == dent->fileinfo.st_mtime) | |
1496 | { | |
7a0cbd5e MS |
1497 | /* |
1498 | * Rewind to the first entry for this file... | |
1499 | */ | |
1500 | ||
1501 | while ((ppd = (ppd_info_t *)cupsArrayPrev(PPDsByName)) != NULL && | |
d1c13e16 | 1502 | !strcmp(ppd->record.filename, name)); |
66ab9486 | 1503 | |
7a0cbd5e MS |
1504 | /* |
1505 | * Then mark all of the matches for this file as found... | |
1506 | */ | |
1507 | ||
1508 | while ((ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) != NULL && | |
1509 | !strcmp(ppd->record.filename, name)) | |
1510 | ppd->found = 1; | |
1511 | ||
66ab9486 | 1512 | continue; |
ef416fc2 | 1513 | } |
ef416fc2 | 1514 | |
1515 | /* | |
1516 | * No, file is new/changed, so re-scan it... | |
1517 | */ | |
1518 | ||
1519 | if ((fp = cupsFileOpen(filename, "r")) == NULL) | |
1520 | continue; | |
1521 | ||
1522 | /* | |
1523 | * Now see if this is a PPD file... | |
1524 | */ | |
1525 | ||
1526 | line[0] = '\0'; | |
1527 | cupsFileGets(fp, line, sizeof(line)); | |
1528 | ||
1529 | if (strncmp(line, "*PPD-Adobe:", 11)) | |
1530 | { | |
1531 | /* | |
4509bb49 | 1532 | * Nope, treat it as a driver information file... |
ef416fc2 | 1533 | */ |
1534 | ||
4509bb49 MS |
1535 | load_drv(filename, name, fp, dent->fileinfo.st_mtime, |
1536 | dent->fileinfo.st_size); | |
ef416fc2 | 1537 | continue; |
1538 | } | |
1539 | ||
1540 | /* | |
1541 | * Now read until we get the NickName field... | |
1542 | */ | |
1543 | ||
b94498cf | 1544 | cups_languages = cupsArrayNew(NULL, NULL); |
1545 | products = cupsArrayNew(NULL, NULL); | |
1546 | psversions = cupsArrayNew(NULL, NULL); | |
f899b121 | 1547 | |
80ca4592 | 1548 | model_name[0] = '\0'; |
1549 | nick_name[0] = '\0'; | |
1550 | manufacturer[0] = '\0'; | |
1551 | device_id[0] = '\0'; | |
1552 | lang_encoding[0] = '\0'; | |
1553 | strcpy(lang_version, "en"); | |
3d8365b8 | 1554 | model_number = 0; |
1555 | type = PPD_TYPE_POSTSCRIPT; | |
ef416fc2 | 1556 | |
1557 | while (cupsFileGets(fp, line, sizeof(line)) != NULL) | |
1558 | { | |
1559 | if (!strncmp(line, "*Manufacturer:", 14)) | |
1560 | sscanf(line, "%*[^\"]\"%255[^\"]", manufacturer); | |
1561 | else if (!strncmp(line, "*ModelName:", 11)) | |
1562 | sscanf(line, "%*[^\"]\"%127[^\"]", model_name); | |
80ca4592 | 1563 | else if (!strncmp(line, "*LanguageEncoding:", 18)) |
1564 | sscanf(line, "%*[^:]:%63s", lang_encoding); | |
ef416fc2 | 1565 | else if (!strncmp(line, "*LanguageVersion:", 17)) |
80ca4592 | 1566 | sscanf(line, "%*[^:]:%63s", lang_version); |
ef416fc2 | 1567 | else if (!strncmp(line, "*NickName:", 10)) |
1568 | sscanf(line, "%*[^\"]\"%255[^\"]", nick_name); | |
89d46774 | 1569 | else if (!strncasecmp(line, "*1284DeviceID:", 14)) |
58dc1933 | 1570 | { |
bd7854cb | 1571 | sscanf(line, "%*[^\"]\"%255[^\"]", device_id); |
58dc1933 MS |
1572 | |
1573 | // Make sure device ID ends with a semicolon... | |
1574 | if (device_id[0] && device_id[strlen(device_id) - 1] != ';') | |
1575 | strlcat(device_id, ";", sizeof(device_id)); | |
1576 | } | |
3d8365b8 | 1577 | else if (!strncmp(line, "*Product:", 9)) |
f899b121 | 1578 | { |
c168a833 MS |
1579 | if (sscanf(line, "%*[^\"]\"(%255[^\"]", product) == 1) |
1580 | { | |
1581 | /* | |
1582 | * Make sure the value ends with a right parenthesis - can't stop at | |
1583 | * the first right paren since the product name may contain escaped | |
1584 | * parenthesis... | |
1585 | */ | |
1586 | ||
1587 | ptr = product + strlen(product) - 1; | |
1588 | if (ptr > product && *ptr == ')') | |
1589 | { | |
1590 | /* | |
1591 | * Yes, ends with a parenthesis, so remove it from the end and | |
1592 | * add the product to the list... | |
1593 | */ | |
1594 | ||
1595 | *ptr = '\0'; | |
1596 | cupsArrayAdd(products, strdup(product)); | |
1597 | } | |
1598 | } | |
f899b121 | 1599 | } |
3d8365b8 | 1600 | else if (!strncmp(line, "*PSVersion:", 11)) |
b94498cf | 1601 | { |
1602 | sscanf(line, "%*[^\"]\"%255[^\"]", psversion); | |
1603 | cupsArrayAdd(psversions, strdup(psversion)); | |
1604 | } | |
3d8365b8 | 1605 | else if (!strncmp(line, "*cupsLanguages:", 15)) |
b94498cf | 1606 | { |
1607 | char *start; /* Start of language */ | |
1608 | ||
1609 | ||
1610 | for (start = line + 15; *start && isspace(*start & 255); start ++); | |
1611 | ||
1612 | if (*start++ == '\"') | |
1613 | { | |
1614 | while (*start) | |
1615 | { | |
1616 | for (ptr = start + 1; | |
1617 | *ptr && *ptr != '\"' && !isspace(*ptr & 255); | |
1618 | ptr ++); | |
1619 | ||
1620 | if (*ptr) | |
1621 | { | |
1622 | *ptr++ = '\0'; | |
1623 | ||
1624 | while (isspace(*ptr & 255)) | |
1625 | *ptr++ = '\0'; | |
1626 | } | |
1627 | ||
1628 | cupsArrayAdd(cups_languages, strdup(start)); | |
1629 | start = ptr; | |
1630 | } | |
1631 | } | |
1632 | } | |
3d8365b8 | 1633 | else if (!strncmp(line, "*cupsFax:", 9)) |
1634 | { | |
1635 | for (ptr = line + 9; isspace(*ptr & 255); ptr ++); | |
1636 | ||
1637 | if (!strncasecmp(ptr, "true", 4)) | |
1638 | type = PPD_TYPE_FAX; | |
1639 | } | |
a0f6818e | 1640 | else if (!strncmp(line, "*cupsFilter:", 12) && type == PPD_TYPE_POSTSCRIPT) |
3d8365b8 | 1641 | { |
1642 | if (strstr(line + 12, "application/vnd.cups-raster")) | |
1643 | type = PPD_TYPE_RASTER; | |
1644 | else if (strstr(line + 12, "application/vnd.cups-pdf")) | |
1645 | type = PPD_TYPE_PDF; | |
3d8365b8 | 1646 | } |
1647 | else if (!strncmp(line, "*cupsModelNumber:", 17)) | |
1648 | sscanf(line, "*cupsModelNumber:%d", &model_number); | |
ef416fc2 | 1649 | else if (!strncmp(line, "*OpenUI", 7)) |
1650 | { | |
1651 | /* | |
1652 | * Stop early if we have a NickName or ModelName attributes | |
1653 | * before the first OpenUI... | |
1654 | */ | |
1655 | ||
b94498cf | 1656 | if ((model_name[0] || nick_name[0]) && cupsArrayCount(products) > 0 && |
1657 | cupsArrayCount(psversions) > 0) | |
ef416fc2 | 1658 | break; |
1659 | } | |
ef416fc2 | 1660 | } |
1661 | ||
1662 | /* | |
1663 | * Close the file... | |
1664 | */ | |
1665 | ||
1666 | cupsFileClose(fp); | |
1667 | ||
1668 | /* | |
1669 | * See if we got all of the required info... | |
1670 | */ | |
1671 | ||
1672 | if (nick_name[0]) | |
80ca4592 | 1673 | cupsCharsetToUTF8((cups_utf8_t *)make_model, nick_name, |
1674 | sizeof(make_model), _ppdGetEncoding(lang_encoding)); | |
ef416fc2 | 1675 | else |
1676 | strcpy(make_model, model_name); | |
1677 | ||
1678 | while (isspace(make_model[0] & 255)) | |
1679 | _cups_strcpy(make_model, make_model + 1); | |
1680 | ||
b94498cf | 1681 | if (!make_model[0] || cupsArrayCount(products) == 0 || |
1682 | cupsArrayCount(psversions) == 0) | |
f899b121 | 1683 | { |
1684 | /* | |
1685 | * We don't have all the info needed, so skip this file... | |
1686 | */ | |
1687 | ||
1688 | if (!make_model[0]) | |
1689 | fprintf(stderr, "WARNING: Missing NickName and ModelName in %s!\n", | |
1690 | filename); | |
1691 | ||
1692 | if (cupsArrayCount(products) == 0) | |
1693 | fprintf(stderr, "WARNING: Missing Product in %s!\n", filename); | |
1694 | ||
b94498cf | 1695 | if (cupsArrayCount(psversions) == 0) |
1696 | fprintf(stderr, "WARNING: Missing PSVersion in %s!\n", filename); | |
f899b121 | 1697 | |
b94498cf | 1698 | free_array(products); |
1699 | free_array(psversions); | |
1700 | free_array(cups_languages); | |
f899b121 | 1701 | |
1702 | continue; | |
1703 | } | |
ef416fc2 | 1704 | |
3d8365b8 | 1705 | if (model_name[0]) |
1706 | cupsArrayAdd(products, strdup(model_name)); | |
1707 | ||
ef416fc2 | 1708 | /* |
5eb9da71 | 1709 | * Normalize the make and model string... |
ef416fc2 | 1710 | */ |
1711 | ||
1712 | while (isspace(manufacturer[0] & 255)) | |
1713 | _cups_strcpy(manufacturer, manufacturer + 1); | |
1714 | ||
5eb9da71 MS |
1715 | if (!strncasecmp(make_model, manufacturer, strlen(manufacturer))) |
1716 | strlcpy(temp, make_model, sizeof(temp)); | |
1717 | else | |
1718 | snprintf(temp, sizeof(temp), "%s %s", manufacturer, make_model); | |
1719 | ||
1720 | _ppdNormalizeMakeAndModel(temp, make_model, sizeof(make_model)); | |
1721 | ||
1722 | /* | |
1723 | * See if we got a manufacturer... | |
1724 | */ | |
1725 | ||
ef416fc2 | 1726 | if (!manufacturer[0] || !strcmp(manufacturer, "ESP")) |
1727 | { | |
1728 | /* | |
1729 | * Nope, copy the first part of the make and model then... | |
1730 | */ | |
1731 | ||
1732 | strlcpy(manufacturer, make_model, sizeof(manufacturer)); | |
1733 | ||
1734 | /* | |
1735 | * Truncate at the first space, dash, or slash, or make the | |
1736 | * manufacturer "Other"... | |
1737 | */ | |
1738 | ||
1739 | for (ptr = manufacturer; *ptr; ptr ++) | |
1740 | if (*ptr == ' ' || *ptr == '-' || *ptr == '/') | |
1741 | break; | |
1742 | ||
1743 | if (*ptr && ptr > manufacturer) | |
1744 | *ptr = '\0'; | |
ef416fc2 | 1745 | else |
1746 | strcpy(manufacturer, "Other"); | |
ef416fc2 | 1747 | } |
1748 | else if (!strncasecmp(manufacturer, "LHAG", 4) || | |
1749 | !strncasecmp(manufacturer, "linotype", 8)) | |
1750 | strcpy(manufacturer, "LHAG"); | |
5eb9da71 MS |
1751 | else if (!strncasecmp(manufacturer, "Hewlett", 7)) |
1752 | strcpy(manufacturer, "HP"); | |
ef416fc2 | 1753 | |
1754 | /* | |
80ca4592 | 1755 | * Fix the lang_version as needed... |
ef416fc2 | 1756 | */ |
1757 | ||
80ca4592 | 1758 | if ((ptr = strchr(lang_version, '-')) != NULL) |
ef416fc2 | 1759 | *ptr++ = '\0'; |
80ca4592 | 1760 | else if ((ptr = strchr(lang_version, '_')) != NULL) |
ef416fc2 | 1761 | *ptr++ = '\0'; |
1762 | ||
1763 | if (ptr) | |
1764 | { | |
1765 | /* | |
1766 | * Setup the country suffix... | |
1767 | */ | |
1768 | ||
1769 | country[0] = '_'; | |
1770 | _cups_strcpy(country + 1, ptr); | |
1771 | } | |
1772 | else | |
1773 | { | |
1774 | /* | |
1775 | * No country suffix... | |
1776 | */ | |
1777 | ||
1778 | country[0] = '\0'; | |
1779 | } | |
1780 | ||
1781 | for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++) | |
8b116e60 | 1782 | if (!strcasecmp(languages[i].version, lang_version)) |
ef416fc2 | 1783 | break; |
1784 | ||
1785 | if (i < (int)(sizeof(languages) / sizeof(languages[0]))) | |
1786 | { | |
1787 | /* | |
1788 | * Found a known language... | |
1789 | */ | |
1790 | ||
80ca4592 | 1791 | snprintf(lang_version, sizeof(lang_version), "%s%s", |
1792 | languages[i].language, country); | |
ef416fc2 | 1793 | } |
1794 | else | |
1795 | { | |
1796 | /* | |
1797 | * Unknown language; use "xx"... | |
1798 | */ | |
1799 | ||
80ca4592 | 1800 | strcpy(lang_version, "xx"); |
ef416fc2 | 1801 | } |
1802 | ||
1803 | /* | |
5eb9da71 | 1804 | * Record the PPD file... |
ef416fc2 | 1805 | */ |
1806 | ||
1807 | new_ppd = !ppd; | |
1808 | ||
1809 | if (new_ppd) | |
1810 | { | |
1811 | /* | |
1812 | * Add new PPD file... | |
1813 | */ | |
1814 | ||
f0ab5bff | 1815 | fprintf(stderr, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name); |
ef416fc2 | 1816 | |
66ab9486 MS |
1817 | ppd = add_ppd(name, name, lang_version, manufacturer, make_model, |
1818 | device_id, (char *)cupsArrayFirst(products), | |
b94498cf | 1819 | (char *)cupsArrayFirst(psversions), |
3d8365b8 | 1820 | dent->fileinfo.st_mtime, dent->fileinfo.st_size, |
ed6e7faf | 1821 | model_number, type, "file"); |
b94498cf | 1822 | |
1823 | if (!ppd) | |
ef416fc2 | 1824 | { |
1825 | cupsDirClose(dir); | |
1826 | return (0); | |
1827 | } | |
1828 | } | |
1829 | else | |
1830 | { | |
1831 | /* | |
1832 | * Update existing record... | |
1833 | */ | |
1834 | ||
f0ab5bff | 1835 | fprintf(stderr, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name); |
ef416fc2 | 1836 | |
1837 | memset(ppd, 0, sizeof(ppd_info_t)); | |
1838 | ||
3d8365b8 | 1839 | ppd->found = 1; |
1840 | ppd->record.mtime = dent->fileinfo.st_mtime; | |
1841 | ppd->record.size = dent->fileinfo.st_size; | |
1842 | ppd->record.model_number = model_number; | |
1843 | ppd->record.type = type; | |
ef416fc2 | 1844 | |
1845 | strlcpy(ppd->record.name, name, sizeof(ppd->record.name)); | |
1846 | strlcpy(ppd->record.make, manufacturer, sizeof(ppd->record.make)); | |
1847 | strlcpy(ppd->record.make_and_model, make_model, | |
1848 | sizeof(ppd->record.make_and_model)); | |
b94498cf | 1849 | strlcpy(ppd->record.languages[0], lang_version, |
1850 | sizeof(ppd->record.languages[0])); | |
1851 | strlcpy(ppd->record.products[0], (char *)cupsArrayFirst(products), | |
1852 | sizeof(ppd->record.products[0])); | |
1853 | strlcpy(ppd->record.psversions[0], (char *)cupsArrayFirst(psversions), | |
1854 | sizeof(ppd->record.psversions[0])); | |
bd7854cb | 1855 | strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id)); |
ef416fc2 | 1856 | } |
1857 | ||
f899b121 | 1858 | /* |
b94498cf | 1859 | * Add remaining products, versions, and languages... |
f899b121 | 1860 | */ |
1861 | ||
b94498cf | 1862 | for (i = 1; |
1863 | i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL; | |
1864 | i ++) | |
1865 | strlcpy(ppd->record.products[i], ptr, | |
1866 | sizeof(ppd->record.products[0])); | |
1867 | ||
1868 | for (i = 1; | |
1869 | i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL; | |
1870 | i ++) | |
1871 | strlcpy(ppd->record.psversions[i], ptr, | |
1872 | sizeof(ppd->record.psversions[0])); | |
f899b121 | 1873 | |
b94498cf | 1874 | for (i = 1, ptr = (char *)cupsArrayFirst(cups_languages); |
1875 | i < PPD_MAX_LANG && ptr; | |
1876 | i ++, ptr = (char *)cupsArrayNext(cups_languages)) | |
1877 | strlcpy(ppd->record.languages[i], ptr, | |
1878 | sizeof(ppd->record.languages[0])); | |
1879 | ||
1880 | /* | |
1881 | * Free products, versions, and languages... | |
1882 | */ | |
1883 | ||
1884 | free_array(cups_languages); | |
1885 | free_array(products); | |
1886 | free_array(psversions); | |
f899b121 | 1887 | |
ef416fc2 | 1888 | ChangedPPD = 1; |
1889 | } | |
1890 | ||
1891 | cupsDirClose(dir); | |
1892 | ||
1893 | return (1); | |
1894 | } | |
1895 | ||
1896 | ||
4509bb49 MS |
1897 | /* |
1898 | * 'load_drv()' - Load the PPDs from a driver information file. | |
1899 | */ | |
1900 | ||
1901 | static int /* O - 1 on success, 0 on failure */ | |
1902 | load_drv(const char *filename, /* I - Actual filename */ | |
1903 | const char *name, /* I - Name to the rest of the world */ | |
1904 | cups_file_t *fp, /* I - File to read from */ | |
1905 | time_t mtime, /* I - Mod time of driver info file */ | |
1906 | off_t size) /* I - Size of driver info file */ | |
1907 | { | |
1908 | ppdcSource *src; // Driver information file | |
1909 | ppdcDriver *d; // Current driver | |
1910 | ppdcAttr *device_id, // 1284DeviceID attribute | |
1911 | *product, // Current product value | |
1912 | *ps_version, // PSVersion attribute | |
1913 | *cups_fax, // cupsFax attribute | |
1914 | *nick_name; // NickName attribute | |
1915 | ppdcFilter *filter; // Current filter | |
7a0cbd5e MS |
1916 | ppd_info_t *ppd; // Current PPD |
1917 | int products_found; // Number of products found | |
4509bb49 MS |
1918 | char uri[1024], // Driver URI |
1919 | make_model[1024]; // Make and model | |
1920 | int type; // Driver type | |
1921 | ||
1922 | ||
66ab9486 MS |
1923 | /* |
1924 | * Load the driver info file... | |
1925 | */ | |
1926 | ||
4509bb49 MS |
1927 | src = new ppdcSource(filename, fp); |
1928 | ||
1929 | if (src->drivers->count == 0) | |
1930 | { | |
1931 | fprintf(stderr, | |
1932 | "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n", | |
1933 | filename); | |
e4572d57 | 1934 | src->release(); |
4509bb49 MS |
1935 | return (0); |
1936 | } | |
1937 | ||
66ab9486 MS |
1938 | /* |
1939 | * Add a dummy entry for the file... | |
1940 | */ | |
1941 | ||
7a0cbd5e | 1942 | add_ppd(name, name, "", "", "", "", "", "", mtime, size, 0, |
ed6e7faf | 1943 | PPD_TYPE_DRV, "drv"); |
f0ab5bff | 1944 | ChangedPPD = 1; |
66ab9486 MS |
1945 | |
1946 | /* | |
1947 | * Then the drivers in the file... | |
1948 | */ | |
1949 | ||
4509bb49 MS |
1950 | for (d = (ppdcDriver *)src->drivers->first(); |
1951 | d; | |
1952 | d = (ppdcDriver *)src->drivers->next()) | |
1953 | { | |
1954 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0, | |
1955 | "/%s/%s", name, | |
1956 | d->file_name ? d->file_name->value : | |
1957 | d->pc_file_name->value); | |
1958 | ||
1959 | device_id = d->find_attr("1284DeviceID", NULL); | |
1960 | ps_version = d->find_attr("PSVersion", NULL); | |
1961 | nick_name = d->find_attr("NickName", NULL); | |
1962 | ||
1963 | if (nick_name) | |
1964 | strlcpy(make_model, nick_name->value->value, sizeof(make_model)); | |
1965 | else if (strncasecmp(d->model_name->value, d->manufacturer->value, | |
1966 | strlen(d->manufacturer->value))) | |
1967 | snprintf(make_model, sizeof(make_model), "%s %s, %s", | |
1968 | d->manufacturer->value, d->model_name->value, | |
1969 | d->version->value); | |
1970 | else | |
1971 | snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value, | |
1972 | d->version->value); | |
1973 | ||
1974 | if ((cups_fax = d->find_attr("cupsFax", NULL)) != NULL && | |
1975 | !strcasecmp(cups_fax->value->value, "true")) | |
1976 | type = PPD_TYPE_FAX; | |
1977 | else if (d->type == PPDC_DRIVER_PS) | |
1978 | type = PPD_TYPE_POSTSCRIPT; | |
1979 | else if (d->type != PPDC_DRIVER_CUSTOM) | |
1980 | type = PPD_TYPE_RASTER; | |
1981 | else | |
1982 | { | |
1983 | for (filter = (ppdcFilter *)d->filters->first(), | |
1984 | type = PPD_TYPE_POSTSCRIPT; | |
1985 | filter; | |
1986 | filter = (ppdcFilter *)d->filters->next()) | |
1987 | if (strcasecmp(filter->mime_type->value, "application/vnd.cups-raster")) | |
1988 | type = PPD_TYPE_RASTER; | |
1989 | else if (strcasecmp(filter->mime_type->value, | |
1990 | "application/vnd.cups-pdf")) | |
1991 | type = PPD_TYPE_PDF; | |
1992 | } | |
1993 | ||
f8b3a85b MS |
1994 | for (product = (ppdcAttr *)d->attrs->first(), products_found = 0, |
1995 | ppd = NULL; | |
4509bb49 MS |
1996 | product; |
1997 | product = (ppdcAttr *)d->attrs->next()) | |
1998 | if (!strcmp(product->name->value, "Product")) | |
1999 | { | |
7a0cbd5e MS |
2000 | if (!products_found) |
2001 | ppd = add_ppd(name, uri, "en", d->manufacturer->value, make_model, | |
2002 | device_id ? device_id->value->value : "", | |
2003 | product->value->value, | |
2004 | ps_version ? ps_version->value->value : "(3010) 0", | |
2005 | mtime, size, d->model_number, type, "drv"); | |
2006 | else if (products_found < PPD_MAX_PROD) | |
2007 | strlcpy(ppd->record.products[products_found], product->value->value, | |
2008 | sizeof(ppd->record.products[0])); | |
2009 | else | |
2010 | break; | |
4509bb49 | 2011 | |
7a0cbd5e | 2012 | products_found ++; |
4509bb49 MS |
2013 | } |
2014 | ||
7a0cbd5e MS |
2015 | if (!products_found) |
2016 | add_ppd(name, uri, "en", d->manufacturer->value, make_model, | |
4509bb49 MS |
2017 | device_id ? device_id->value->value : "", |
2018 | d->model_name->value, | |
2019 | ps_version ? ps_version->value->value : "(3010) 0", | |
ed6e7faf | 2020 | mtime, size, d->model_number, type, "drv"); |
4509bb49 MS |
2021 | } |
2022 | ||
e4572d57 | 2023 | src->release(); |
4509bb49 MS |
2024 | |
2025 | return (1); | |
2026 | } | |
2027 | ||
2028 | ||
ef416fc2 | 2029 | /* |
2030 | * 'load_drivers()' - Load driver-generated PPD files. | |
2031 | */ | |
2032 | ||
bd7854cb | 2033 | static int /* O - 1 on success, 0 on failure */ |
ed6e7faf MS |
2034 | load_drivers(cups_array_t *include, /* I - Drivers to include */ |
2035 | cups_array_t *exclude) /* I - Drivers to exclude */ | |
ef416fc2 | 2036 | { |
b94498cf | 2037 | int i; /* Looping var */ |
2038 | char *start, /* Start of value */ | |
2039 | *ptr; /* Pointer into string */ | |
8b450588 MS |
2040 | const char *server_bin, /* CUPS_SERVERBIN env variable */ |
2041 | *scheme, /* Scheme for this driver */ | |
2042 | *scheme_end; /* Pointer to end of scheme */ | |
ef416fc2 | 2043 | char drivers[1024]; /* Location of driver programs */ |
c934a06c MS |
2044 | int pid; /* Process ID for driver program */ |
2045 | cups_file_t *fp; /* Pipe to driver program */ | |
ef416fc2 | 2046 | cups_dir_t *dir; /* Directory pointer */ |
2047 | cups_dentry_t *dent; /* Directory entry */ | |
c934a06c MS |
2048 | char *argv[3], /* Arguments for command */ |
2049 | filename[1024], /* Name of driver */ | |
ef416fc2 | 2050 | line[2048], /* Line from driver */ |
2051 | name[512], /* ppd-name */ | |
ef416fc2 | 2052 | make[128], /* ppd-make */ |
b94498cf | 2053 | make_and_model[128], /* ppd-make-and-model */ |
2054 | device_id[128], /* ppd-device-id */ | |
2055 | languages[128], /* ppd-natural-language */ | |
2056 | product[128], /* ppd-product */ | |
3d8365b8 | 2057 | psversion[128], /* ppd-psversion */ |
2058 | type_str[128]; /* ppd-type */ | |
2059 | int type; /* PPD type */ | |
b94498cf | 2060 | ppd_info_t *ppd; /* Newly added PPD */ |
ef416fc2 | 2061 | |
2062 | ||
2063 | /* | |
2064 | * Try opening the driver directory... | |
2065 | */ | |
2066 | ||
2067 | if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL) | |
2068 | server_bin = CUPS_SERVERBIN; | |
2069 | ||
2070 | snprintf(drivers, sizeof(drivers), "%s/driver", server_bin); | |
2071 | ||
2072 | if ((dir = cupsDirOpen(drivers)) == NULL) | |
2073 | { | |
2074 | fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory " | |
839a51c8 MS |
2075 | "\"%s\": %s\n", |
2076 | drivers, strerror(errno)); | |
ef416fc2 | 2077 | return (0); |
2078 | } | |
2079 | ||
2080 | /* | |
2081 | * Loop through all of the device drivers... | |
2082 | */ | |
2083 | ||
c934a06c MS |
2084 | argv[1] = (char *)"list"; |
2085 | argv[2] = NULL; | |
2086 | ||
ef416fc2 | 2087 | while ((dent = cupsDirRead(dir)) != NULL) |
2088 | { | |
2089 | /* | |
2090 | * Only look at executable files... | |
2091 | */ | |
2092 | ||
2093 | if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode)) | |
2094 | continue; | |
2095 | ||
ed6e7faf MS |
2096 | /* |
2097 | * Include/exclude specific drivers... | |
2098 | */ | |
2099 | ||
8b450588 MS |
2100 | if (exclude) |
2101 | { | |
2102 | /* | |
2103 | * Look for "scheme" or "scheme*" (prefix match), and skip any matches. | |
2104 | */ | |
2105 | ||
2106 | for (scheme = (char *)cupsArrayFirst(exclude); | |
2107 | scheme; | |
2108 | scheme = (char *)cupsArrayNext(exclude)) | |
2109 | { | |
2110 | fprintf(stderr, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n", | |
2111 | dent->filename, scheme); | |
2112 | scheme_end = scheme + strlen(scheme) - 1; | |
2113 | ||
2114 | if ((scheme_end > scheme && *scheme_end == '*' && | |
2115 | !strncmp(scheme, dent->filename, scheme_end - scheme)) || | |
2116 | !strcmp(scheme, dent->filename)) | |
2117 | { | |
2118 | fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr); | |
2119 | break; | |
2120 | } | |
2121 | } | |
2122 | ||
2123 | if (scheme) | |
2124 | continue; | |
2125 | } | |
2126 | ||
2127 | if (include) | |
2128 | { | |
2129 | /* | |
2130 | * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches. | |
2131 | */ | |
2132 | ||
2133 | for (scheme = (char *)cupsArrayFirst(include); | |
2134 | scheme; | |
2135 | scheme = (char *)cupsArrayNext(include)) | |
2136 | { | |
2137 | fprintf(stderr, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n", | |
2138 | dent->filename, scheme); | |
2139 | scheme_end = scheme + strlen(scheme) - 1; | |
2140 | ||
2141 | if ((scheme_end > scheme && *scheme_end == '*' && | |
2142 | !strncmp(scheme, dent->filename, scheme_end - scheme)) || | |
2143 | !strcmp(scheme, dent->filename)) | |
2144 | { | |
2145 | fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr); | |
2146 | break; | |
2147 | } | |
2148 | } | |
2149 | ||
2150 | if (!scheme) | |
2151 | continue; | |
2152 | } | |
2153 | else | |
2154 | scheme = dent->filename; | |
ed6e7faf | 2155 | |
ef416fc2 | 2156 | /* |
2157 | * Run the driver with no arguments and collect the output... | |
2158 | */ | |
2159 | ||
c934a06c MS |
2160 | argv[0] = dent->filename; |
2161 | snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename); | |
2162 | ||
2163 | if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL) | |
ef416fc2 | 2164 | { |
c934a06c | 2165 | while (cupsFileGets(fp, line, sizeof(line))) |
ef416fc2 | 2166 | { |
2167 | /* | |
2168 | * Each line is of the form: | |
2169 | * | |
f899b121 | 2170 | * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \ |
b94498cf | 2171 | * "ppd-device-id" "ppd-product" "ppd-psversion" |
ef416fc2 | 2172 | */ |
2173 | ||
bd7854cb | 2174 | device_id[0] = '\0'; |
f899b121 | 2175 | product[0] = '\0'; |
b94498cf | 2176 | psversion[0] = '\0'; |
3d8365b8 | 2177 | strcpy(type_str, "postscript"); |
bd7854cb | 2178 | |
2179 | if (sscanf(line, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\"" | |
b94498cf | 2180 | "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\"" |
3d8365b8 | 2181 | "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\"" |
2182 | "%*[ \t]\"%127[^\"]\"", | |
b94498cf | 2183 | name, languages, make, make_and_model, |
3d8365b8 | 2184 | device_id, product, psversion, type_str) < 4) |
ef416fc2 | 2185 | { |
2186 | /* | |
2187 | * Bad format; strip trailing newline and write an error message. | |
2188 | */ | |
2189 | ||
2190 | if (line[strlen(line) - 1] == '\n') | |
2191 | line[strlen(line) - 1] = '\0'; | |
2192 | ||
2193 | fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n", | |
2194 | dent->filename, line); | |
2195 | break; | |
2196 | } | |
2197 | else | |
2198 | { | |
2199 | /* | |
2200 | * Add the device to the array of available devices... | |
2201 | */ | |
2202 | ||
b94498cf | 2203 | if ((start = strchr(languages, ',')) != NULL) |
2204 | *start++ = '\0'; | |
2205 | ||
3d8365b8 | 2206 | for (type = 0; |
2207 | type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0])); | |
2208 | type ++) | |
2209 | if (!strcmp(type_str, ppd_types[type])) | |
2210 | break; | |
2211 | ||
2212 | if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0]))) | |
2213 | { | |
c934a06c MS |
2214 | fprintf(stderr, |
2215 | "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n", | |
3d8365b8 | 2216 | type_str); |
2217 | type = PPD_TYPE_UNKNOWN; | |
2218 | } | |
2219 | ||
f0ab5bff MS |
2220 | ppd = add_ppd(filename, name, languages, make, make_and_model, |
2221 | device_id, product, psversion, 0, 0, 0, type, scheme); | |
b94498cf | 2222 | |
2223 | if (!ppd) | |
ef416fc2 | 2224 | { |
2225 | cupsDirClose(dir); | |
c934a06c | 2226 | cupsFileClose(fp); |
ef416fc2 | 2227 | return (0); |
2228 | } | |
2229 | ||
b94498cf | 2230 | if (start && *start) |
2231 | { | |
2232 | for (i = 1; i < PPD_MAX_LANG && *start; i ++) | |
2233 | { | |
2234 | if ((ptr = strchr(start, ',')) != NULL) | |
2235 | *ptr++ = '\0'; | |
2236 | else | |
2237 | ptr = start + strlen(start); | |
2238 | ||
2239 | strlcpy(ppd->record.languages[i], start, | |
2240 | sizeof(ppd->record.languages[0])); | |
2241 | ||
2242 | start = ptr; | |
2243 | } | |
2244 | } | |
2245 | ||
f0ab5bff | 2246 | fprintf(stderr, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n", |
ef416fc2 | 2247 | name); |
2248 | } | |
2249 | } | |
2250 | ||
c934a06c | 2251 | cupsFileClose(fp); |
ef416fc2 | 2252 | } |
2253 | else | |
2254 | fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n", | |
2255 | filename, strerror(errno)); | |
2256 | } | |
2257 | ||
2258 | cupsDirClose(dir); | |
2259 | ||
2260 | return (1); | |
2261 | } | |
2262 | ||
2263 | ||
f0ab5bff MS |
2264 | /* |
2265 | * 'load_ppds_dat()' - Load the ppds.dat file. | |
2266 | */ | |
2267 | ||
2268 | static void | |
2269 | load_ppds_dat(char *filename, /* I - Filename buffer */ | |
2270 | size_t filesize, /* I - Size of filename buffer */ | |
2271 | int verbose) /* I - Be verbose? */ | |
2272 | { | |
2273 | ppd_info_t *ppd; /* Current PPD file */ | |
2274 | cups_file_t *fp; /* ppds.dat file */ | |
2275 | struct stat fileinfo; /* ppds.dat information */ | |
2276 | const char *cups_cachedir; /* CUPS_CACHEDIR environment variable */ | |
2277 | ||
2278 | ||
2279 | PPDsByName = cupsArrayNew((cups_array_func_t)compare_names, NULL); | |
2280 | PPDsByMakeModel = cupsArrayNew((cups_array_func_t)compare_ppds, NULL); | |
2281 | ChangedPPD = 0; | |
2282 | ||
2283 | if ((cups_cachedir = getenv("CUPS_CACHEDIR")) == NULL) | |
2284 | cups_cachedir = CUPS_CACHEDIR; | |
2285 | ||
2286 | snprintf(filename, filesize, "%s/ppds.dat", cups_cachedir); | |
2287 | if ((fp = cupsFileOpen(filename, "r")) != NULL) | |
2288 | { | |
2289 | /* | |
2290 | * See if we have the right sync word... | |
2291 | */ | |
2292 | ||
2293 | unsigned ppdsync; /* Sync word */ | |
2294 | int num_ppds; /* Number of PPDs */ | |
2295 | ||
2296 | ||
2297 | if (cupsFileRead(fp, (char *)&ppdsync, sizeof(ppdsync)) | |
2298 | == sizeof(ppdsync) && | |
2299 | ppdsync == PPD_SYNC && | |
2300 | !stat(filename, &fileinfo) && | |
2301 | ((fileinfo.st_size - sizeof(ppdsync)) % sizeof(ppd_rec_t)) == 0 && | |
2302 | (num_ppds = (fileinfo.st_size - sizeof(ppdsync)) / | |
2303 | sizeof(ppd_rec_t)) > 0) | |
2304 | { | |
2305 | /* | |
2306 | * We have a ppds.dat file, so read it! | |
2307 | */ | |
2308 | ||
2309 | for (; num_ppds > 0; num_ppds --) | |
2310 | { | |
2311 | if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL) | |
2312 | { | |
2313 | if (verbose) | |
2314 | fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n", | |
2315 | stderr); | |
2316 | exit(1); | |
2317 | } | |
2318 | ||
2319 | if (cupsFileRead(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)) > 0) | |
2320 | { | |
2321 | cupsArrayAdd(PPDsByName, ppd); | |
2322 | cupsArrayAdd(PPDsByMakeModel, ppd); | |
2323 | } | |
2324 | else | |
2325 | { | |
2326 | free(ppd); | |
2327 | break; | |
2328 | } | |
2329 | } | |
2330 | ||
2331 | if (verbose) | |
2332 | fprintf(stderr, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n", | |
2333 | filename, cupsArrayCount(PPDsByName)); | |
2334 | } | |
2335 | ||
2336 | cupsFileClose(fp); | |
2337 | } | |
2338 | } | |
2339 | ||
2340 | ||
58dc1933 MS |
2341 | /* |
2342 | * 'regex_device_id()' - Compile a regular expression based on the 1284 device | |
2343 | * ID. | |
2344 | */ | |
2345 | ||
2346 | static regex_t * /* O - Regular expression */ | |
2347 | regex_device_id(const char *device_id) /* I - IEEE-1284 device ID */ | |
2348 | { | |
2349 | char res[2048], /* Regular expression string */ | |
2350 | *ptr; /* Pointer into string */ | |
2351 | regex_t *re; /* Regular expression */ | |
2352 | int cmd; /* Command set string? */ | |
2353 | ||
2354 | ||
2355 | fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id); | |
2356 | ||
2357 | /* | |
2358 | * Scan the device ID string and insert class, command set, manufacturer, and | |
2359 | * model attributes to match. We assume that the device ID in the PPD and the | |
2360 | * device ID reported by the device itself use the same attribute names and | |
2361 | * order of attributes. | |
2362 | */ | |
2363 | ||
2364 | ptr = res; | |
2365 | ||
2366 | while (*device_id && ptr < (res + sizeof(res) - 6)) | |
2367 | { | |
2368 | cmd = !strncasecmp(device_id, "COMMAND SET:", 12) || | |
2369 | !strncasecmp(device_id, "CMD:", 4); | |
2370 | ||
2371 | if (cmd || !strncasecmp(device_id, "MANUFACTURER:", 13) || | |
2372 | !strncasecmp(device_id, "MFG:", 4) || | |
2373 | !strncasecmp(device_id, "MFR:", 4) || | |
2374 | !strncasecmp(device_id, "MODEL:", 6) || | |
2375 | !strncasecmp(device_id, "MDL:", 4)) | |
2376 | { | |
2377 | if (ptr > res) | |
2378 | { | |
2379 | *ptr++ = '.'; | |
2380 | *ptr++ = '*'; | |
2381 | } | |
2382 | ||
2383 | *ptr++ = '('; | |
2384 | ||
2385 | while (*device_id && *device_id != ';' && ptr < (res + sizeof(res) - 4)) | |
2386 | { | |
2387 | if (strchr("[]{}().*\\|", *device_id)) | |
2388 | *ptr++ = '\\'; | |
2389 | *ptr++ = *device_id++; | |
2390 | } | |
2391 | ||
2392 | if (*device_id == ';' || !*device_id) | |
2393 | *ptr++ = ';'; | |
2394 | *ptr++ = ')'; | |
2395 | if (cmd) | |
2396 | *ptr++ = '?'; | |
2397 | } | |
2398 | else if ((device_id = strchr(device_id, ';')) == NULL) | |
2399 | break; | |
2400 | else | |
2401 | device_id ++; | |
2402 | } | |
2403 | ||
2404 | *ptr = '\0'; | |
2405 | ||
2406 | fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res); | |
2407 | ||
2408 | /* | |
2409 | * Compile the regular expression and return... | |
2410 | */ | |
2411 | ||
2412 | if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL) | |
2413 | { | |
2414 | if (!regcomp(re, res, REG_EXTENDED | REG_ICASE)) | |
2415 | { | |
2416 | fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr); | |
2417 | return (re); | |
2418 | } | |
2419 | ||
2420 | free(re); | |
2421 | } | |
2422 | ||
2423 | return (NULL); | |
2424 | } | |
2425 | ||
2426 | ||
2427 | /* | |
2428 | * 'regex_string()' - Construct a regular expression to compare a simple string. | |
2429 | */ | |
2430 | ||
2431 | static regex_t * /* O - Regular expression */ | |
2432 | regex_string(const char *s) /* I - String to compare */ | |
2433 | { | |
2434 | char res[2048], /* Regular expression string */ | |
2435 | *ptr; /* Pointer into string */ | |
2436 | regex_t *re; /* Regular expression */ | |
2437 | ||
2438 | ||
2439 | fprintf(stderr, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s); | |
2440 | ||
2441 | /* | |
2442 | * Convert the string to a regular expression, escaping special characters | |
2443 | * as needed. | |
2444 | */ | |
2445 | ||
2446 | ptr = res; | |
2447 | ||
2448 | while (*s && ptr < (res + sizeof(res) - 2)) | |
2449 | { | |
2450 | if (strchr("[]{}().*\\", *s)) | |
2451 | *ptr++ = '\\'; | |
2452 | ||
2453 | *ptr++ = *s++; | |
2454 | } | |
2455 | ||
2456 | *ptr = '\0'; | |
2457 | ||
2458 | fprintf(stderr, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res); | |
2459 | ||
2460 | /* | |
2461 | * Create a case-insensitive regular expression... | |
2462 | */ | |
2463 | ||
2464 | if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL) | |
2465 | { | |
2466 | if (!regcomp(re, res, REG_ICASE)) | |
2467 | { | |
2468 | fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr); | |
2469 | return (re); | |
2470 | } | |
2471 | ||
2472 | free(re); | |
2473 | } | |
2474 | ||
2475 | return (NULL); | |
2476 | } | |
2477 | ||
2478 | ||
ef416fc2 | 2479 | /* |
4509bb49 | 2480 | * End of "$Id$". |
ef416fc2 | 2481 | */ |