]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/cups-driverd.cxx
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / scheduler / cups-driverd.cxx
CommitLineData
ef416fc2 1/*
4509bb49 2 * "$Id$"
ef416fc2 3 *
4 * PPD/driver support for the Common UNIX Printing System (CUPS).
5 *
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.
9 *
91c84a35 10 * Copyright 2007-2008 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 *
21 * main() - Scan for drivers and return an IPP response.
22 * add_ppd() - Add a PPD file.
4509bb49 23 * cat_drv() - Generate a PPD from a driver info file.
ef416fc2 24 * cat_ppd() - Copy a PPD file to stdout.
4509bb49 25 * copy_static() - Copy a static PPD file to stdout.
ef416fc2 26 * compare_names() - Compare PPD filenames for sorting.
27 * compare_ppds() - Compare PPD file make and model names for sorting.
b94498cf 28 * free_array() - Free an array of strings.
ef416fc2 29 * list_ppds() - List PPD files.
30 * load_ppds() - Load PPD files recursively.
4509bb49 31 * load_drv() - Load the PPDs from a driver information file.
ef416fc2 32 * load_drivers() - Load driver-generated PPD files.
33 */
34
35/*
36 * Include necessary headers...
37 */
38
39#include "util.h"
40#include <cups/dir.h>
80ca4592 41#include <cups/transcode.h>
5eb9da71 42#include <cups/ppd-private.h>
4509bb49 43#include <ppdc/ppdc.h>
ef416fc2 44
45
b94498cf 46/*
47 * Constants...
48 */
49
66ab9486 50#define PPD_SYNC 0x50504435 /* Sync word for ppds.dat (PPD5) */
b94498cf 51#define PPD_MAX_LANG 32 /* Maximum languages */
52#define PPD_MAX_PROD 8 /* Maximum products */
53#define PPD_MAX_VERS 8 /* Maximum versions */
54
3d8365b8 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 */
66ab9486 60#define PPD_TYPE_DRV 5 /* Driver info file */
3d8365b8 61
ac884b6a 62static const char * const ppd_types[] = /* ppd-type values */
3d8365b8 63{
64 "postscript",
65 "pdf",
66 "raster",
67 "fax",
68 "unknown"
69};
70
b94498cf 71
ef416fc2 72/*
73 * PPD information structures...
74 */
75
76typedef struct /**** PPD record ****/
77{
78 time_t mtime; /* Modification time */
749b1e90 79 off_t size; /* Size in bytes */
3d8365b8 80 int model_number; /* cupsModelNumber */
81 int type; /* ppd-type */
66ab9486
MS
82 char filename[512], /* Filename */
83 name[512], /* PPD name */
b94498cf 84 languages[PPD_MAX_LANG][6],
85 /* LanguageVersion/cupsLanguages */
86 products[PPD_MAX_PROD][128],
87 /* Product strings */
88 psversions[PPD_MAX_VERS][32],
89 /* PSVersion strings */
ef416fc2 90 make[128], /* Manufacturer */
f899b121 91 make_and_model[128], /* NickName/ModelName */
66ab9486 92 device_id[256]; /* IEEE 1284 Device ID */
b94498cf 93} ppd_rec_t;
ef416fc2 94
95typedef struct /**** In-memory record ****/
96{
97 int found; /* 1 if PPD is found */
98 ppd_rec_t record; /* PPDs.dat record */
99} ppd_info_t;
100
101
102/*
103 * Globals...
104 */
105
66ab9486
MS
106cups_array_t *PPDsByName = NULL, /* PPD files sorted by filename and name */
107 *PPDsByMakeModel = NULL;/* PPD files sorted by make and model */
ef416fc2 108int ChangedPPD; /* Did we change the PPD database? */
109
110
111/*
112 * Local functions...
113 */
114
66ab9486
MS
115static ppd_info_t *add_ppd(const char *filename, const char *name,
116 const char *language, const char *make,
117 const char *make_and_model,
f899b121 118 const char *device_id, const char *product,
b94498cf 119 const char *psversion, time_t mtime,
3d8365b8 120 size_t size, int model_number, int type);
4509bb49 121static int cat_drv(const char *name, int request_id);
b94498cf 122static int cat_ppd(const char *name, int request_id);
4509bb49 123static int cat_static(const char *name, int request_id);
bd7854cb 124static int compare_names(const ppd_info_t *p0,
125 const ppd_info_t *p1);
126static int compare_ppds(const ppd_info_t *p0,
127 const ppd_info_t *p1);
b94498cf 128static void free_array(cups_array_t *a);
bd7854cb 129static int list_ppds(int request_id, int limit, const char *opt);
130static int load_drivers(void);
4509bb49
MS
131static int load_drv(const char *filename, const char *name,
132 cups_file_t *fp, time_t mtime, off_t size);
b94498cf 133static int load_ppds(const char *d, const char *p, int descend);
ef416fc2 134
135
136/*
137 * 'main()' - Scan for drivers and return an IPP response.
138 *
139 * Usage:
140 *
141 * cups-driverd request_id limit options
142 */
143
144int /* O - Exit code */
145main(int argc, /* I - Number of command-line args */
146 char *argv[]) /* I - Command-line arguments */
147{
148 /*
149 * Install or list PPDs...
150 */
151
152 if (argc == 3 && !strcmp(argv[1], "cat"))
b94498cf 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])));
ef416fc2 156 else if (argc == 5 && !strcmp(argv[1], "list"))
157 return (list_ppds(atoi(argv[2]), atoi(argv[3]), argv[4]));
158 else
159 {
160 fputs("Usage: cups-driverd cat ppd-name\n", stderr);
b94498cf 161 fputs("Usage: cups-driverd get request_id ppd-name\n", stderr);
ef416fc2 162 fputs("Usage: cups-driverd list request_id limit options\n", stderr);
163 return (1);
164 }
165}
166
167
168/*
169 * 'add_ppd()' - Add a PPD file.
170 */
171
bd7854cb 172static ppd_info_t * /* O - PPD */
66ab9486
MS
173add_ppd(const char *filename, /* I - PPD filename */
174 const char *name, /* I - PPD name */
b94498cf 175 const char *language, /* I - LanguageVersion */
ef416fc2 176 const char *make, /* I - Manufacturer */
f899b121 177 const char *make_and_model, /* I - NickName/ModelName */
89d46774 178 const char *device_id, /* I - 1284DeviceID */
f899b121 179 const char *product, /* I - Product */
b94498cf 180 const char *psversion, /* I - PSVersion */
ef416fc2 181 time_t mtime, /* I - Modification time */
3d8365b8 182 size_t size, /* I - File size */
183 int model_number, /* I - Model number */
184 int type) /* I - Driver type */
ef416fc2 185{
186 ppd_info_t *ppd; /* PPD */
b86bc4cf 187 char *recommended; /* Foomatic driver string */
ef416fc2 188
189
190 /*
191 * Add a new PPD file...
192 */
193
66ab9486 194 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
ef416fc2 195 {
66ab9486
MS
196 fprintf(stderr,
197 "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n",
198 cupsArrayCount(PPDsByName));
199 return (NULL);
ef416fc2 200 }
201
ef416fc2 202 /*
203 * Zero-out the PPD data and copy the values over...
204 */
205
3d8365b8 206 ppd->found = 1;
207 ppd->record.mtime = mtime;
208 ppd->record.size = size;
209 ppd->record.model_number = model_number;
210 ppd->record.type = type;
ef416fc2 211
66ab9486 212 strlcpy(ppd->record.filename, filename, sizeof(ppd->record.filename));
ef416fc2 213 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
b94498cf 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]));
ef416fc2 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));
bd7854cb 222 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
ef416fc2 223
b86bc4cf 224 /*
225 * Strip confusing (and often wrong) "recommended" suffix added by
226 * Foomatic drivers...
227 */
228
b94498cf 229 if ((recommended = strstr(ppd->record.make_and_model,
230 " (recommended)")) != NULL)
b86bc4cf 231 *recommended = '\0';
232
66ab9486
MS
233 /*
234 * Add the PPD to the PPD arrays...
235 */
236
237 cupsArrayAdd(PPDsByName, ppd);
238 cupsArrayAdd(PPDsByMakeModel, ppd);
239
ef416fc2 240 /*
241 * Return the new PPD pointer...
242 */
243
244 return (ppd);
245}
246
247
4509bb49
MS
248/*
249 * 'cat_drv()' - Generate a PPD from a driver info file.
250 */
251
252static int /* O - Exit code */
253cat_drv(const char *name, /* I - PPD name */
254 int request_id) /* I - Request ID for response? */
255{
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)
268
269
270 // Determine where CUPS has installed the data files...
271 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
272 datadir = CUPS_DATADIR;
273
274 // Pull out the
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)
281 {
282 fprintf(stderr, "ERROR: Bad PPD name \"%s\"!\n", name);
283
284 if (request_id)
285 {
286 snprintf(message, sizeof(message), "Bad PPD name \"%s\"!", name);
287
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",
292 "en-US");
293 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
294 cupsdSendIPPTrailer();
295 }
296
297 return (1);
298 }
299
300 *pc_file_name++ = '\0';
301
302#ifdef __APPLE__
303 if (!strncmp(resource, "/Library/Printers/PPDs.drv/", 27))
304 strlcpy(filename, resource, sizeof(filename));
305 else
306#endif // __APPLE__
307 {
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);
311 }
312
313 src = new ppdcSource(filename);
314
315 for (d = (ppdcDriver *)src->drivers->first();
316 d;
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)))
320 break;
321
322 if (d)
323 {
324 ppdcArray *locales; // Locale names
325 ppdcCatalog *catalog; // Message catalog in .drv file
326
327
328 fprintf(stderr, "DEBUG: %d locales defined in \"%s\"...\n",
329 src->po_files->count, filename);
330
331 locales = new ppdcArray();
332 for (catalog = (ppdcCatalog *)src->po_files->first();
333 catalog;
334 catalog = (ppdcCatalog *)src->po_files->next())
335 {
336 fprintf(stderr, "DEBUG: Adding locale \"%s\"...\n",
337 catalog->locale->value);
338 locales->add(catalog->locale);
339 }
340
341 if (request_id)
342 {
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",
347 "en-US");
348 cupsdSendIPPTrailer();
349 fflush(stdout);
350 }
351
352 out = cupsFileStdout();
353 d->write_ppd_file(out, NULL, locales, src, PPDC_LFONLY);
354 cupsFileClose(out);
355
356 delete locales;
357 }
358 else
359 {
360 fprintf(stderr, "ERROR: PPD \"%s\" not found!\n", name);
361
362 if (request_id)
363 {
364 snprintf(message, sizeof(message), "PPD \"%s\" not found!", name);
365
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",
370 "en-US");
371 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
372 cupsdSendIPPTrailer();
373 }
374 }
375
376 delete src;
377
378 return (!d);
379}
380
381
ef416fc2 382/*
383 * 'cat_ppd()' - Copy a PPD file to stdout.
384 */
385
bd7854cb 386static int /* O - Exit code */
b94498cf 387cat_ppd(const char *name, /* I - PPD name */
388 int request_id) /* I - Request ID for response? */
ef416fc2 389{
390 char scheme[256], /* Scheme from PPD name */
4509bb49
MS
391 *sptr, /* Pointer into scheme */
392 line[1024], /* Line/filename */
393 message[2048]; /* status-message */
ef416fc2 394
395
396 /*
397 * Figure out if this is a static or dynamic PPD file...
398 */
399
400 strlcpy(scheme, name, sizeof(scheme));
401 if ((sptr = strchr(scheme, ':')) != NULL)
402 {
403 *sptr = '\0';
404
405 if (!strcmp(scheme, "file"))
406 {
407 /*
408 * "file:name" == "name"...
409 */
410
411 name += 5;
412 scheme[0] = '\0';
413 }
414 }
415 else
416 scheme[0] = '\0';
417
09a101d6 418 if (request_id > 0)
419 puts("Content-Type: application/ipp\n");
b94498cf 420
4509bb49
MS
421 if (!scheme[0])
422 return (cat_static(name, request_id));
423 else if (!strcmp(scheme, "drv"))
424 return (cat_drv(name, request_id));
425 else
ef416fc2 426 {
427 /*
428 * Dynamic PPD, see if we have a driver program to support it...
429 */
430
431 const char *serverbin; /* CUPS_SERVERBIN env var */
c934a06c 432 char *argv[4]; /* Arguments for program */
ef416fc2 433
434
435 if ((serverbin = getenv("CUPS_SERVERBIN")) == NULL)
436 serverbin = CUPS_SERVERBIN;
437
438 snprintf(line, sizeof(line), "%s/driver/%s", serverbin, scheme);
439 if (access(line, X_OK))
440 {
441 /*
442 * File does not exist or is not executable...
443 */
444
445 fprintf(stderr, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n",
446 line, strerror(errno));
b94498cf 447
448 if (request_id > 0)
449 {
450 snprintf(message, sizeof(message), "Unable to access \"%s\" - %s",
451 line, strerror(errno));
452
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",
457 "en-US");
458 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
459 cupsdSendIPPTrailer();
460 }
461
ef416fc2 462 return (1);
463 }
464
465 /*
466 * Yes, let it cat the PPD file...
467 */
468
b94498cf 469 if (request_id)
470 {
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",
475 "en-US");
476 cupsdSendIPPTrailer();
477 }
478
c934a06c
MS
479 argv[0] = scheme;
480 argv[1] = (char *)"cat";
481 argv[2] = (char *)name;
482 argv[3] = NULL;
483
484 if (cupsdExec(line, argv))
ef416fc2 485 {
486 /*
487 * Unable to execute driver...
488 */
489
490 fprintf(stderr, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n",
491 line, strerror(errno));
492 return (1);
493 }
494 }
ef416fc2 495
4509bb49
MS
496 /*
497 * Return with no errors...
498 */
ef416fc2 499
4509bb49
MS
500 return (0);
501}
ef416fc2 502
b94498cf 503
4509bb49
MS
504/*
505 * 'copy_static()' - Copy a static PPD file to stdout.
506 */
b94498cf 507
4509bb49
MS
508static int /* O - Exit code */
509cat_static(const char *name, /* I - PPD name */
510 int request_id) /* I - Request ID for response? */
511{
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 */
b94498cf 516
ef416fc2 517
4509bb49
MS
518 if (name[0] == '/' || strstr(name, "../") || strstr(name, "/.."))
519 {
ef416fc2 520 /*
4509bb49 521 * Bad name...
ef416fc2 522 */
523
4509bb49
MS
524 fprintf(stderr, "ERROR: [cups-driverd] Bad PPD name \"%s\"!\n", name);
525
526 if (request_id)
b94498cf 527 {
4509bb49 528 snprintf(message, sizeof(message), "Bad PPD name \"%s\"!", name);
b94498cf 529
4509bb49
MS
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",
534 "en-US");
535 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
536 cupsdSendIPPTrailer();
b94498cf 537 }
b94498cf 538
4509bb49
MS
539 return (1);
540 }
b94498cf 541
4509bb49
MS
542 /*
543 * Try opening the file...
544 */
b94498cf 545
4509bb49
MS
546#ifdef __APPLE__
547 if (!strncmp(name, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
548 !strncmp(name, "Library/Printers/PPDs/Contents/Resources/", 41))
549 {
550 /*
551 * Map ppd-name to Mac OS X standard locations...
552 */
b94498cf 553
4509bb49
MS
554 snprintf(line, sizeof(line), "/%s", name);
555 }
556 else
b94498cf 557
4509bb49
MS
558#elif defined(__linux)
559 if (!strncmp(name, "lsb/usr/", 8))
560 {
561 /*
562 * Map ppd-name to LSB standard /usr/share/ppd location...
563 */
b94498cf 564
4509bb49
MS
565 snprintf(line, sizeof(line), "/usr/share/ppd/%s", name + 8);
566 }
567 else if (!strncmp(name, "lsb/opt/", 8))
568 {
569 /*
570 * Map ppd-name to LSB standard /opt/share/ppd location...
571 */
ef416fc2 572
4509bb49
MS
573 snprintf(line, sizeof(line), "/opt/share/ppd/%s", name + 8);
574 }
575 else if (!strncmp(name, "lsb/local/", 10))
576 {
577 /*
578 * Map ppd-name to LSB standard /usr/local/share/ppd location...
579 */
b94498cf 580
4509bb49
MS
581 snprintf(line, sizeof(line), "/usr/local/share/ppd/%s", name + 10);
582 }
583 else
b94498cf 584
4509bb49
MS
585#endif /* __APPLE__ */
586 {
587 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
588 datadir = CUPS_DATADIR;
b94498cf 589
4509bb49
MS
590 snprintf(line, sizeof(line), "%s/model/%s", datadir, name);
591 }
592
593 if ((fp = cupsFileOpen(line, "r")) == NULL)
594 {
595 fprintf(stderr, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n",
596 line, strerror(errno));
ef416fc2 597
b94498cf 598 if (request_id)
599 {
4509bb49
MS
600 snprintf(message, sizeof(message), "Unable to open \"%s\" - %s",
601 line, strerror(errno));
602
603 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
b94498cf 604 cupsdSendIPPGroup(IPP_TAG_OPERATION);
605 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
606 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
4509bb49
MS
607 "en-US");
608 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
b94498cf 609 cupsdSendIPPTrailer();
610 }
611
4509bb49
MS
612 return (1);
613 }
ef416fc2 614
4509bb49
MS
615 if (request_id)
616 {
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",
621 "en-US");
622 cupsdSendIPPTrailer();
ef416fc2 623 }
624
625 /*
4509bb49 626 * Now copy the file to stdout...
ef416fc2 627 */
628
4509bb49
MS
629 while (cupsFileGets(fp, line, sizeof(line)))
630 puts(line);
631
632 cupsFileClose(fp);
633
ef416fc2 634 return (0);
635}
636
637
638/*
639 * 'compare_names()' - Compare PPD filenames for sorting.
640 */
641
bd7854cb 642static int /* O - Result of comparison */
ef416fc2 643compare_names(const ppd_info_t *p0, /* I - First PPD file */
644 const ppd_info_t *p1) /* I - Second PPD file */
645{
66ab9486
MS
646 int diff; /* Difference between strings */
647
648
649 if ((diff = strcasecmp(p0->record.filename, p1->record.filename)) != 0)
650 return (diff);
651 else
652 return (strcasecmp(p0->record.name, p1->record.name));
ef416fc2 653}
654
655
656/*
657 * 'compare_ppds()' - Compare PPD file make and model names for sorting.
658 */
659
bd7854cb 660static int /* O - Result of comparison */
ef416fc2 661compare_ppds(const ppd_info_t *p0, /* I - First PPD file */
662 const ppd_info_t *p1) /* I - Second PPD file */
663{
664 int diff; /* Difference between strings */
665
bd7854cb 666
ef416fc2 667 /*
668 * First compare manufacturers...
669 */
670
671 if ((diff = strcasecmp(p0->record.make, p1->record.make)) != 0)
672 return (diff);
673 else if ((diff = cupsdCompareNames(p0->record.make_and_model,
674 p1->record.make_and_model)) != 0)
675 return (diff);
676 else
b94498cf 677 return (strcasecmp(p0->record.languages[0],
678 p1->record.languages[0]));
679}
680
681
682/*
683 * 'free_array()' - Free an array of strings.
684 */
685
686static void
687free_array(cups_array_t *a) /* I - Array to free */
688{
689 char *ptr; /* Pointer to string */
690
691
692 for (ptr = (char *)cupsArrayFirst(a);
693 ptr;
694 ptr = (char *)cupsArrayNext(a))
695 free(ptr);
696
697 cupsArrayDelete(a);
ef416fc2 698}
699
700
701/*
702 * 'list_ppds()' - List PPD files.
703 */
704
bd7854cb 705static int /* O - Exit code */
ef416fc2 706list_ppds(int request_id, /* I - Request ID */
707 int limit, /* I - Limit */
708 const char *opt) /* I - Option argument */
709{
66ab9486 710 int i; /* Looping vars */
ef416fc2 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 */
b94498cf 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 */
3d8365b8 726 *model_number_str, /* ppd-model-number option */
b94498cf 727 *product, /* ppd-product option */
3d8365b8 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 */
b94498cf 733 device_id_len, /* Length of ppd-device-id */
3d8365b8 734 send_device_id, /* Send ppd-device-id? */
b94498cf 735 send_make, /* Send ppd-make? */
736 send_make_and_model, /* Send ppd-make-and-model? */
3d8365b8 737 send_model_number, /* Send ppd-model-number? */
b94498cf 738 send_name, /* Send ppd-name? */
3d8365b8 739 send_natural_language, /* Send ppd-natural-language? */
b94498cf 740 send_product, /* Send ppd-product? */
741 send_psversion, /* Send ppd-psversion? */
3d8365b8 742 send_type, /* Send ppd-type? */
b94498cf 743 sent_header; /* Sent the IPP header? */
744
745
746 fprintf(stderr,
747 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
748 "opt=\"%s\"\n", request_id, limit, opt);
ef416fc2 749
750 /*
751 * See if we a PPD database file...
752 */
753
66ab9486
MS
754 PPDsByName = cupsArrayNew((cups_array_func_t)compare_names, NULL);
755 PPDsByMakeModel = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
756 ChangedPPD = 0;
ef416fc2 757
758 if ((cups_cachedir = getenv("CUPS_CACHEDIR")) == NULL)
759 cups_cachedir = CUPS_CACHEDIR;
760
761 snprintf(filename, sizeof(filename), "%s/ppds.dat", cups_cachedir);
b94498cf 762 if ((fp = cupsFileOpen(filename, "r")) != NULL)
ef416fc2 763 {
764 /*
b94498cf 765 * See if we have the right sync word...
ef416fc2 766 */
767
b94498cf 768 unsigned ppdsync; /* Sync word */
66ab9486
MS
769 int num_ppds; /* Number of PPDs */
770
ef416fc2 771
b94498cf 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 &&
66ab9486
MS
777 (num_ppds = (fileinfo.st_size - sizeof(ppdsync)) /
778 sizeof(ppd_rec_t)) > 0)
ef416fc2 779 {
b94498cf 780 /*
781 * We have a ppds.dat file, so read it!
782 */
783
66ab9486 784 for (; num_ppds > 0; num_ppds --)
ef416fc2 785 {
66ab9486 786 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
b94498cf 787 {
66ab9486
MS
788 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
789 stderr);
790 exit(1);
b94498cf 791 }
ef416fc2 792
66ab9486
MS
793 if (cupsFileRead(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)) > 0)
794 {
795 cupsArrayAdd(PPDsByName, ppd);
796 cupsArrayAdd(PPDsByMakeModel, ppd);
797 }
798 else
799 {
800 free(ppd);
801 break;
802 }
b94498cf 803 }
66ab9486
MS
804
805 fprintf(stderr, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
806 filename, cupsArrayCount(PPDsByName));
ef416fc2 807 }
ef416fc2 808
b94498cf 809 cupsFileClose(fp);
810 }
811
ef416fc2 812 /*
813 * Load all PPDs in the specified directory and below...
814 */
815
ef416fc2 816 if ((cups_datadir = getenv("CUPS_DATADIR")) == NULL)
817 cups_datadir = CUPS_DATADIR;
818
819 snprintf(model, sizeof(model), "%s/model", cups_datadir);
b94498cf 820 load_ppds(model, "", 1);
821
4509bb49
MS
822 snprintf(model, sizeof(model), "%s/drv", cups_datadir);
823 load_ppds(model, "", 1);
824
b94498cf 825#ifdef __APPLE__
826 /*
827 * Load PPDs from standard Mac OS X locations...
828 */
829
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);
838
839#elif defined(__linux)
840 /*
841 * Load PPDs from LSB-defined locations...
842 */
843
568fa3fa
MS
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);
b94498cf 850#endif /* __APPLE__ */
ef416fc2 851
852 /*
853 * Cull PPD files that are no longer present...
854 */
855
66ab9486
MS
856 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
857 ppd;
858 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
ef416fc2 859 if (!ppd->found)
860 {
861 /*
862 * Remove this PPD file from the list...
863 */
864
66ab9486
MS
865 cupsArrayRemove(PPDsByName, ppd);
866 cupsArrayRemove(PPDsByMakeModel, ppd);
867 free(ppd);
ef416fc2 868 }
869
ef416fc2 870 /*
871 * Write the new ppds.dat file...
872 */
873
874 if (ChangedPPD)
875 {
876 if ((fp = cupsFileOpen(filename, "w")) != NULL)
877 {
b94498cf 878 unsigned ppdsync = PPD_SYNC; /* Sync word */
879
880
881 cupsFileWrite(fp, (char *)&ppdsync, sizeof(ppdsync));
882
66ab9486
MS
883 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
884 ppd;
885 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
ef416fc2 886 cupsFileWrite(fp, (char *)&(ppd->record), sizeof(ppd_rec_t));
887
888 cupsFileClose(fp);
889
890 fprintf(stderr, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
66ab9486 891 filename, cupsArrayCount(PPDsByName));
ef416fc2 892 }
893 else
894 fprintf(stderr, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
895 filename, strerror(errno));
896 }
897 else
898 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr);
899
900 /*
901 * Scan for dynamic PPD files...
902 */
903
904 load_drivers();
905
906 /*
907 * Add the raw driver...
908 */
909
66ab9486 910 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
3d8365b8 911 PPD_TYPE_UNKNOWN);
ef416fc2 912
ef416fc2 913 /*
914 * Send IPP attributes...
915 */
916
3d8365b8 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);
b94498cf 927
928 if (make_and_model)
929 mam_len = strlen(make_and_model);
930 else
931 mam_len = 0;
ef416fc2 932
b94498cf 933 if (device_id)
934 device_id_len = strlen(device_id);
935 else
936 device_id_len = 0;
937
3d8365b8 938 if (model_number_str)
939 model_number = atoi(model_number_str);
940 else
941 model_number = 0;
942
943 if (type_str)
944 {
945 for (type = 0;
946 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
947 type ++)
948 if (!strcmp(type_str, ppd_types[type]))
949 break;
950
951 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
952 {
953 fprintf(stderr, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
954 type_str);
955 type_str = NULL;
956 }
957 }
bc44d920 958 else
959 type = 0;
3d8365b8 960
b94498cf 961 if (requested)
962 fprintf(stderr, "DEBUG: [cups-driverd] requested-attributes=\"%s\"\n",
963 requested);
964 if (device_id)
965 fprintf(stderr, "DEBUG: [cups-driverd] ppd-device-id=\"%s\"\n",
966 device_id);
967 if (language)
968 fprintf(stderr, "DEBUG: [cups-driverd] ppd-natural-language=\"%s\"\n",
969 language);
970 if (make)
971 fprintf(stderr, "DEBUG: [cups-driverd] ppd-make=\"%s\"\n",
972 make);
973 if (make_and_model)
974 fprintf(stderr, "DEBUG: [cups-driverd] ppd-make-and-model=\"%s\"\n",
975 make_and_model);
3d8365b8 976 if (model_number_str)
977 fprintf(stderr, "DEBUG: [cups-driverd] ppd-model-number=\"%s\"\n",
978 model_number_str);
b94498cf 979 if (product)
980 fprintf(stderr, "DEBUG: [cups-driverd] ppd-product=\"%s\"\n",
981 product);
982 if (psversion)
983 fprintf(stderr, "DEBUG: [cups-driverd] ppd-psversion=\"%s\"\n",
984 psversion);
3d8365b8 985 if (type_str)
986 fprintf(stderr, "DEBUG: [cups-driverd] ppd-type=\"%s\"\n", type_str);
ef416fc2 987
988 if (!requested || strstr(requested, "all"))
989 {
990 send_name = 1;
991 send_make = 1;
992 send_make_and_model = 1;
3d8365b8 993 send_model_number = 1;
ef416fc2 994 send_natural_language = 1;
bd7854cb 995 send_device_id = 1;
f899b121 996 send_product = 1;
b94498cf 997 send_psversion = 1;
3d8365b8 998 send_type = 1;
ef416fc2 999 }
1000 else
1001 {
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;
3d8365b8 1007 send_model_number = strstr(requested, "ppd-model-number") != NULL;
ef416fc2 1008 send_natural_language = strstr(requested, "ppd-natural-language") != NULL;
bd7854cb 1009 send_device_id = strstr(requested, "ppd-device-id") != NULL;
f899b121 1010 send_product = strstr(requested, "ppd-product") != NULL;
b94498cf 1011 send_psversion = strstr(requested, "ppd-psversion") != NULL;
3d8365b8 1012 send_type = strstr(requested, "ppd-type") != NULL;
ef416fc2 1013 }
1014
1015 puts("Content-Type: application/ipp\n");
1016
b94498cf 1017 sent_header = 0;
ef416fc2 1018
66ab9486
MS
1019 if (limit <= 0 || limit > cupsArrayCount(PPDsByMakeModel))
1020 count = cupsArrayCount(PPDsByMakeModel);
ef416fc2 1021 else
1022 count = limit;
1023
66ab9486
MS
1024 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1025 count > 0 && ppd;
1026 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
b94498cf 1027 {
1028 /*
1029 * Filter PPDs based on make, model, or device ID...
1030 */
1031
66ab9486
MS
1032 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1033 ppd->record.type >= PPD_TYPE_DRV)
1034 continue;
1035
b94498cf 1036 if (device_id && strncasecmp(ppd->record.device_id, device_id,
1037 device_id_len))
1038 continue; /* TODO: implement smart compare */
1039
1040 if (language)
ef416fc2 1041 {
66ab9486
MS
1042 for (i = 0; i < PPD_MAX_LANG; i ++)
1043 if (!ppd->record.languages[i][0] ||
1044 !strcasecmp(ppd->record.languages[i], language))
b94498cf 1045 break;
1046
66ab9486 1047 if (i >= PPD_MAX_LANG || !ppd->record.languages[i][0])
b94498cf 1048 continue;
1049 }
1050
1051 if (make && strcasecmp(ppd->record.make, make))
1052 continue;
1053
1054 if (make_and_model && strncasecmp(ppd->record.make_and_model,
1055 make_and_model, mam_len))
1056 continue;
1057
3d8365b8 1058 if (model_number_str && ppd->record.model_number != model_number)
1059 continue;
1060
b94498cf 1061 if (product)
1062 {
66ab9486
MS
1063 for (i = 0; i < PPD_MAX_PROD; i ++)
1064 if (!ppd->record.products[i][0] ||
1065 !strcasecmp(ppd->record.products[i], product))
b94498cf 1066 break;
ef416fc2 1067
66ab9486 1068 if (i >= PPD_MAX_PROD || !ppd->record.products[i][0])
b94498cf 1069 continue;
1070 }
ef416fc2 1071
b94498cf 1072 if (psversion)
1073 {
66ab9486
MS
1074 for (i = 0; i < PPD_MAX_VERS; i ++)
1075 if (!ppd->record.psversions[i][0] ||
1076 !strcasecmp(ppd->record.psversions[i], psversion))
b94498cf 1077 break;
ef416fc2 1078
66ab9486 1079 if (i >= PPD_MAX_VERS || !ppd->record.psversions[i][0])
b94498cf 1080 continue;
1081 }
ef416fc2 1082
3d8365b8 1083 if (type_str && ppd->record.type != type)
1084 continue;
1085
b94498cf 1086 /*
1087 * Send this PPD...
1088 */
ef416fc2 1089
b94498cf 1090 if (!sent_header)
1091 {
1092 sent_header = 1;
ef416fc2 1093
b94498cf 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");
1098 }
ef416fc2 1099
b94498cf 1100 fprintf(stderr, "DEBUG: [cups-driverd] Sending %s (%s)...\n",
1101 ppd->record.name, ppd->record.make_and_model);
ef416fc2 1102
b94498cf 1103 count --;
bd7854cb 1104
b94498cf 1105 cupsdSendIPPGroup(IPP_TAG_PRINTER);
f899b121 1106
b94498cf 1107 if (send_name)
1108 cupsdSendIPPString(IPP_TAG_NAME, "ppd-name", ppd->record.name);
ef416fc2 1109
b94498cf 1110 if (send_natural_language)
1111 {
1112 cupsdSendIPPString(IPP_TAG_LANGUAGE, "ppd-natural-language",
1113 ppd->record.languages[0]);
ef416fc2 1114
66ab9486
MS
1115 for (i = 1; i < PPD_MAX_LANG && ppd->record.languages[i][0]; i ++)
1116 cupsdSendIPPString(IPP_TAG_LANGUAGE, "", ppd->record.languages[i]);
b94498cf 1117 }
ef416fc2 1118
b94498cf 1119 if (send_make)
1120 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make", ppd->record.make);
ef416fc2 1121
b94498cf 1122 if (send_make_and_model)
1123 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make-and-model",
1124 ppd->record.make_and_model);
1125
1126 if (send_device_id)
1127 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-device-id",
1128 ppd->record.device_id);
1129
1130 if (send_product)
1131 {
1132 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-product",
1133 ppd->record.products[0]);
1134
66ab9486
MS
1135 for (i = 1; i < PPD_MAX_PROD && ppd->record.products[i][0]; i ++)
1136 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.products[i]);
b94498cf 1137 }
1138
1139 if (send_psversion)
1140 {
1141 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-psversion",
1142 ppd->record.psversions[0]);
1143
66ab9486
MS
1144 for (i = 1; i < PPD_MAX_VERS && ppd->record.psversions[i][0]; i ++)
1145 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.psversions[i]);
b94498cf 1146 }
1147
3d8365b8 1148 if (send_type)
1149 cupsdSendIPPString(IPP_TAG_KEYWORD, "ppd-type",
1150 ppd_types[ppd->record.type]);
1151
1152 if (send_model_number)
1153 cupsdSendIPPInteger(IPP_TAG_INTEGER, "ppd-model-number",
1154 ppd->record.model_number);
1155
b94498cf 1156 /*
1157 * If we have only requested the ppd-make attribute, then skip
1158 * the remaining PPDs with this make...
1159 */
1160
1161 if (requested && !strcmp(requested, "ppd-make"))
1162 {
1163 const char *this_make; /* This ppd-make */
1164
1165
66ab9486
MS
1166 for (this_make = ppd->record.make,
1167 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel);
1168 ppd;
1169 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
b94498cf 1170 if (strcasecmp(this_make, ppd->record.make))
1171 break;
1172
66ab9486 1173 cupsArrayPrev(PPDsByMakeModel);
ef416fc2 1174 }
b94498cf 1175 }
1176
1177 if (!sent_header)
1178 {
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");
1183 }
ef416fc2 1184
1185 cupsdSendIPPTrailer();
1186
1187 return (0);
1188}
1189
1190
1191/*
1192 * 'load_ppds()' - Load PPD files recursively.
1193 */
1194
bd7854cb 1195static int /* O - 1 on success, 0 on failure */
ef416fc2 1196load_ppds(const char *d, /* I - Actual directory */
b94498cf 1197 const char *p, /* I - Virtual path in name */
1198 int descend) /* I - Descend into directories? */
ef416fc2 1199{
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 */
80ca4592 1208 lang_version[64], /* PPD LanguageVersion */
1209 lang_encoding[64], /* PPD LanguageEncoding */
ef416fc2 1210 country[64], /* Country code */
1211 manufacturer[256], /* Manufacturer */
1212 make_model[256], /* Make and Model */
1213 model_name[256], /* ModelName */
bd7854cb 1214 nick_name[256], /* NickName */
f899b121 1215 device_id[256], /* 1284DeviceID */
b94498cf 1216 product[256], /* Product */
5eb9da71
MS
1217 psversion[256], /* PSVersion */
1218 temp[512]; /* Temporary make and model */
3d8365b8 1219 int model_number, /* cupsModelNumber */
1220 type; /* ppd-type */
b94498cf 1221 cups_array_t *products, /* Product array */
1222 *psversions, /* PSVersion array */
1223 *cups_languages; /* cupsLanguages array */
ef416fc2 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 */
1228 {
1229 const char *version, /* LanguageVersion string */
1230 *language; /* Language code */
1231 } languages[] =
1232 {
dd1abb6b
MS
1233 { "chinese", "zh" },
1234 { "czech", "cs" },
1235 { "danish", "da" },
1236 { "dutch", "nl" },
1237 { "english", "en" },
1238 { "finnish", "fi" },
1239 { "french", "fr" },
1240 { "german", "de" },
1241 { "greek", "el" },
1242 { "hungarian", "hu" },
1243 { "italian", "it" },
1244 { "japanese", "ja" },
1245 { "korean", "ko" },
1246 { "norwegian", "no" },
1247 { "polish", "pl" },
1248 { "portuguese", "pt" },
1249 { "russian", "ru" },
1250 { "simplified chinese", "zh_CN" },
1251 { "slovak", "sk" },
1252 { "spanish", "es" },
1253 { "swedish", "sv" },
1254 { "traditional chinese", "zh_TW" },
1255 { "turkish", "tr" }
ef416fc2 1256 };
1257
1258
4509bb49
MS
1259 fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d);
1260
ef416fc2 1261 if ((dir = cupsDirOpen(d)) == NULL)
1262 {
839a51c8
MS
1263 if (errno != ENOENT)
1264 fprintf(stderr,
1265 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1266 d, strerror(errno));
1267
ef416fc2 1268 return (0);
1269 }
1270
1271 while ((dent = cupsDirRead(dir)) != NULL)
1272 {
bd7854cb 1273 /*
1274 * Skip files/directories starting with "."...
1275 */
1276
1277 if (dent->filename[0] == '.')
1278 continue;
1279
ef416fc2 1280 /*
1281 * See if this is a file...
1282 */
1283
1284 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
1285
1286 if (p[0])
1287 snprintf(name, sizeof(name), "%s/%s", p, dent->filename);
1288 else
1289 strlcpy(name, dent->filename, sizeof(name));
1290
1291 if (S_ISDIR(dent->fileinfo.st_mode))
1292 {
1293 /*
1294 * Do subdirectory...
1295 */
1296
b94498cf 1297 if (descend)
1298 if (!load_ppds(filename, name, 1))
1299 {
1300 cupsDirClose(dir);
1301 return (1);
1302 }
ef416fc2 1303
1304 continue;
1305 }
1306
1307 /*
1308 * See if this file has been scanned before...
1309 */
1310
66ab9486
MS
1311 strcpy(key.record.filename, name);
1312 strcpy(key.record.name, name);
ef416fc2 1313
66ab9486 1314 ppd = (ppd_info_t *)cupsArrayFind(PPDsByName, &key);
ef416fc2 1315
66ab9486
MS
1316 if (ppd &&
1317 ppd->record.size == dent->fileinfo.st_size &&
1318 ppd->record.mtime == dent->fileinfo.st_mtime)
1319 {
1320 do
ef416fc2 1321 {
1322 ppd->found = 1;
ef416fc2 1323 }
66ab9486
MS
1324 while ((ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) != NULL &&
1325 !strcasecmp(ppd->record.filename, name));
1326
1327 continue;
ef416fc2 1328 }
ef416fc2 1329
1330 /*
1331 * No, file is new/changed, so re-scan it...
1332 */
1333
1334 if ((fp = cupsFileOpen(filename, "r")) == NULL)
1335 continue;
1336
1337 /*
1338 * Now see if this is a PPD file...
1339 */
1340
1341 line[0] = '\0';
1342 cupsFileGets(fp, line, sizeof(line));
1343
1344 if (strncmp(line, "*PPD-Adobe:", 11))
1345 {
1346 /*
4509bb49 1347 * Nope, treat it as a driver information file...
ef416fc2 1348 */
1349
4509bb49
MS
1350 load_drv(filename, name, fp, dent->fileinfo.st_mtime,
1351 dent->fileinfo.st_size);
ef416fc2 1352 continue;
1353 }
1354
1355 /*
1356 * Now read until we get the NickName field...
1357 */
1358
b94498cf 1359 cups_languages = cupsArrayNew(NULL, NULL);
1360 products = cupsArrayNew(NULL, NULL);
1361 psversions = cupsArrayNew(NULL, NULL);
f899b121 1362
80ca4592 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");
3d8365b8 1369 model_number = 0;
1370 type = PPD_TYPE_POSTSCRIPT;
ef416fc2 1371
1372 while (cupsFileGets(fp, line, sizeof(line)) != NULL)
1373 {
1374 if (!strncmp(line, "*Manufacturer:", 14))
1375 sscanf(line, "%*[^\"]\"%255[^\"]", manufacturer);
1376 else if (!strncmp(line, "*ModelName:", 11))
1377 sscanf(line, "%*[^\"]\"%127[^\"]", model_name);
80ca4592 1378 else if (!strncmp(line, "*LanguageEncoding:", 18))
1379 sscanf(line, "%*[^:]:%63s", lang_encoding);
ef416fc2 1380 else if (!strncmp(line, "*LanguageVersion:", 17))
80ca4592 1381 sscanf(line, "%*[^:]:%63s", lang_version);
ef416fc2 1382 else if (!strncmp(line, "*NickName:", 10))
1383 sscanf(line, "%*[^\"]\"%255[^\"]", nick_name);
89d46774 1384 else if (!strncasecmp(line, "*1284DeviceID:", 14))
bd7854cb 1385 sscanf(line, "%*[^\"]\"%255[^\"]", device_id);
3d8365b8 1386 else if (!strncmp(line, "*Product:", 9))
f899b121 1387 {
5eb9da71
MS
1388 if (sscanf(line, "%*[^\"]\"(%255[^)]", product) == 1)
1389 cupsArrayAdd(products, strdup(product));
f899b121 1390 }
3d8365b8 1391 else if (!strncmp(line, "*PSVersion:", 11))
b94498cf 1392 {
1393 sscanf(line, "%*[^\"]\"%255[^\"]", psversion);
1394 cupsArrayAdd(psversions, strdup(psversion));
1395 }
3d8365b8 1396 else if (!strncmp(line, "*cupsLanguages:", 15))
b94498cf 1397 {
1398 char *start; /* Start of language */
1399
1400
1401 for (start = line + 15; *start && isspace(*start & 255); start ++);
1402
1403 if (*start++ == '\"')
1404 {
1405 while (*start)
1406 {
1407 for (ptr = start + 1;
1408 *ptr && *ptr != '\"' && !isspace(*ptr & 255);
1409 ptr ++);
1410
1411 if (*ptr)
1412 {
1413 *ptr++ = '\0';
1414
1415 while (isspace(*ptr & 255))
1416 *ptr++ = '\0';
1417 }
1418
1419 cupsArrayAdd(cups_languages, strdup(start));
1420 start = ptr;
1421 }
1422 }
1423 }
3d8365b8 1424 else if (!strncmp(line, "*cupsFax:", 9))
1425 {
1426 for (ptr = line + 9; isspace(*ptr & 255); ptr ++);
1427
1428 if (!strncasecmp(ptr, "true", 4))
1429 type = PPD_TYPE_FAX;
1430 }
a0f6818e 1431 else if (!strncmp(line, "*cupsFilter:", 12) && type == PPD_TYPE_POSTSCRIPT)
3d8365b8 1432 {
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;
3d8365b8 1437 }
1438 else if (!strncmp(line, "*cupsModelNumber:", 17))
1439 sscanf(line, "*cupsModelNumber:%d", &model_number);
ef416fc2 1440 else if (!strncmp(line, "*OpenUI", 7))
1441 {
1442 /*
1443 * Stop early if we have a NickName or ModelName attributes
1444 * before the first OpenUI...
1445 */
1446
b94498cf 1447 if ((model_name[0] || nick_name[0]) && cupsArrayCount(products) > 0 &&
1448 cupsArrayCount(psversions) > 0)
ef416fc2 1449 break;
1450 }
ef416fc2 1451 }
1452
1453 /*
1454 * Close the file...
1455 */
1456
1457 cupsFileClose(fp);
1458
1459 /*
1460 * See if we got all of the required info...
1461 */
1462
1463 if (nick_name[0])
80ca4592 1464 cupsCharsetToUTF8((cups_utf8_t *)make_model, nick_name,
1465 sizeof(make_model), _ppdGetEncoding(lang_encoding));
ef416fc2 1466 else
1467 strcpy(make_model, model_name);
1468
1469 while (isspace(make_model[0] & 255))
1470 _cups_strcpy(make_model, make_model + 1);
1471
b94498cf 1472 if (!make_model[0] || cupsArrayCount(products) == 0 ||
1473 cupsArrayCount(psversions) == 0)
f899b121 1474 {
1475 /*
1476 * We don't have all the info needed, so skip this file...
1477 */
1478
1479 if (!make_model[0])
1480 fprintf(stderr, "WARNING: Missing NickName and ModelName in %s!\n",
1481 filename);
1482
1483 if (cupsArrayCount(products) == 0)
1484 fprintf(stderr, "WARNING: Missing Product in %s!\n", filename);
1485
b94498cf 1486 if (cupsArrayCount(psversions) == 0)
1487 fprintf(stderr, "WARNING: Missing PSVersion in %s!\n", filename);
f899b121 1488
b94498cf 1489 free_array(products);
1490 free_array(psversions);
1491 free_array(cups_languages);
f899b121 1492
1493 continue;
1494 }
ef416fc2 1495
3d8365b8 1496 if (model_name[0])
1497 cupsArrayAdd(products, strdup(model_name));
1498
ef416fc2 1499 /*
5eb9da71 1500 * Normalize the make and model string...
ef416fc2 1501 */
1502
1503 while (isspace(manufacturer[0] & 255))
1504 _cups_strcpy(manufacturer, manufacturer + 1);
1505
5eb9da71
MS
1506 if (!strncasecmp(make_model, manufacturer, strlen(manufacturer)))
1507 strlcpy(temp, make_model, sizeof(temp));
1508 else
1509 snprintf(temp, sizeof(temp), "%s %s", manufacturer, make_model);
1510
1511 _ppdNormalizeMakeAndModel(temp, make_model, sizeof(make_model));
1512
1513 /*
1514 * See if we got a manufacturer...
1515 */
1516
ef416fc2 1517 if (!manufacturer[0] || !strcmp(manufacturer, "ESP"))
1518 {
1519 /*
1520 * Nope, copy the first part of the make and model then...
1521 */
1522
1523 strlcpy(manufacturer, make_model, sizeof(manufacturer));
1524
1525 /*
1526 * Truncate at the first space, dash, or slash, or make the
1527 * manufacturer "Other"...
1528 */
1529
1530 for (ptr = manufacturer; *ptr; ptr ++)
1531 if (*ptr == ' ' || *ptr == '-' || *ptr == '/')
1532 break;
1533
1534 if (*ptr && ptr > manufacturer)
1535 *ptr = '\0';
ef416fc2 1536 else
1537 strcpy(manufacturer, "Other");
ef416fc2 1538 }
1539 else if (!strncasecmp(manufacturer, "LHAG", 4) ||
1540 !strncasecmp(manufacturer, "linotype", 8))
1541 strcpy(manufacturer, "LHAG");
5eb9da71
MS
1542 else if (!strncasecmp(manufacturer, "Hewlett", 7))
1543 strcpy(manufacturer, "HP");
ef416fc2 1544
1545 /*
80ca4592 1546 * Fix the lang_version as needed...
ef416fc2 1547 */
1548
80ca4592 1549 if ((ptr = strchr(lang_version, '-')) != NULL)
ef416fc2 1550 *ptr++ = '\0';
80ca4592 1551 else if ((ptr = strchr(lang_version, '_')) != NULL)
ef416fc2 1552 *ptr++ = '\0';
1553
1554 if (ptr)
1555 {
1556 /*
1557 * Setup the country suffix...
1558 */
1559
1560 country[0] = '_';
1561 _cups_strcpy(country + 1, ptr);
1562 }
1563 else
1564 {
1565 /*
1566 * No country suffix...
1567 */
1568
1569 country[0] = '\0';
1570 }
1571
1572 for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++)
80ca4592 1573 if (!strcasecmp(languages[i].version, lang_version))
ef416fc2 1574 break;
1575
1576 if (i < (int)(sizeof(languages) / sizeof(languages[0])))
1577 {
1578 /*
1579 * Found a known language...
1580 */
1581
80ca4592 1582 snprintf(lang_version, sizeof(lang_version), "%s%s",
1583 languages[i].language, country);
ef416fc2 1584 }
1585 else
1586 {
1587 /*
1588 * Unknown language; use "xx"...
1589 */
1590
80ca4592 1591 strcpy(lang_version, "xx");
ef416fc2 1592 }
1593
1594 /*
5eb9da71 1595 * Record the PPD file...
ef416fc2 1596 */
1597
1598 new_ppd = !ppd;
1599
1600 if (new_ppd)
1601 {
1602 /*
1603 * Add new PPD file...
1604 */
1605
1606 fprintf(stderr, "DEBUG: [cups-driverd] Adding ppd \"%s\"...\n", name);
1607
66ab9486
MS
1608 ppd = add_ppd(name, name, lang_version, manufacturer, make_model,
1609 device_id, (char *)cupsArrayFirst(products),
b94498cf 1610 (char *)cupsArrayFirst(psversions),
3d8365b8 1611 dent->fileinfo.st_mtime, dent->fileinfo.st_size,
1612 model_number, type);
b94498cf 1613
1614 if (!ppd)
ef416fc2 1615 {
1616 cupsDirClose(dir);
1617 return (0);
1618 }
1619 }
1620 else
1621 {
1622 /*
1623 * Update existing record...
1624 */
1625
1626 fprintf(stderr, "DEBUG: [cups-driverd] Updating ppd \"%s\"...\n", name);
1627
1628 memset(ppd, 0, sizeof(ppd_info_t));
1629
3d8365b8 1630 ppd->found = 1;
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;
ef416fc2 1635
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));
b94498cf 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]));
bd7854cb 1646 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
ef416fc2 1647 }
1648
f899b121 1649 /*
b94498cf 1650 * Add remaining products, versions, and languages...
f899b121 1651 */
1652
b94498cf 1653 for (i = 1;
1654 i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL;
1655 i ++)
1656 strlcpy(ppd->record.products[i], ptr,
1657 sizeof(ppd->record.products[0]));
1658
1659 for (i = 1;
1660 i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL;
1661 i ++)
1662 strlcpy(ppd->record.psversions[i], ptr,
1663 sizeof(ppd->record.psversions[0]));
f899b121 1664
b94498cf 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]));
1670
1671 /*
1672 * Free products, versions, and languages...
1673 */
1674
1675 free_array(cups_languages);
1676 free_array(products);
1677 free_array(psversions);
f899b121 1678
ef416fc2 1679 ChangedPPD = 1;
1680 }
1681
1682 cupsDirClose(dir);
1683
1684 return (1);
1685}
1686
1687
4509bb49
MS
1688/*
1689 * 'load_drv()' - Load the PPDs from a driver information file.
1690 */
1691
1692static int /* O - 1 on success, 0 on failure */
1693load_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 */
1698{
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
1711
1712
66ab9486
MS
1713 /*
1714 * Load the driver info file...
1715 */
1716
4509bb49
MS
1717 src = new ppdcSource(filename, fp);
1718
1719 if (src->drivers->count == 0)
1720 {
1721 fprintf(stderr,
1722 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1723 filename);
1724 delete src;
1725 return (0);
1726 }
1727
66ab9486
MS
1728 /*
1729 * Add a dummy entry for the file...
1730 */
1731
1732 add_ppd(filename, filename, "", "", "", "", "", "", mtime, size, 0,
1733 PPD_TYPE_DRV);
1734
1735 /*
1736 * Then the drivers in the file...
1737 */
1738
4509bb49
MS
1739 for (d = (ppdcDriver *)src->drivers->first();
1740 d;
1741 d = (ppdcDriver *)src->drivers->next())
1742 {
1743 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0,
1744 "/%s/%s", name,
1745 d->file_name ? d->file_name->value :
1746 d->pc_file_name->value);
1747
1748 device_id = d->find_attr("1284DeviceID", NULL);
1749 ps_version = d->find_attr("PSVersion", NULL);
1750 nick_name = d->find_attr("NickName", NULL);
1751
1752 if (nick_name)
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,
1758 d->version->value);
1759 else
1760 snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value,
1761 d->version->value);
1762
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;
1770 else
1771 {
1772 for (filter = (ppdcFilter *)d->filters->first(),
1773 type = PPD_TYPE_POSTSCRIPT;
1774 filter;
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;
1781 }
1782
1783 for (product = (ppdcAttr *)d->attrs->first(), product_found = false;
1784 product;
1785 product = (ppdcAttr *)d->attrs->next())
1786 if (!strcmp(product->name->value, "Product"))
1787 {
1788 product_found = true;
1789
66ab9486 1790 add_ppd(filename, uri, "en", d->manufacturer->value, make_model,
4509bb49
MS
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);
1795 }
1796
1797 if (!product_found)
66ab9486 1798 add_ppd(filename, uri, "en", d->manufacturer->value, make_model,
4509bb49
MS
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);
1803 }
1804
1805 delete src;
1806
1807 return (1);
1808}
1809
1810
ef416fc2 1811/*
1812 * 'load_drivers()' - Load driver-generated PPD files.
1813 */
1814
bd7854cb 1815static int /* O - 1 on success, 0 on failure */
ef416fc2 1816load_drivers(void)
1817{
b94498cf 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 */
ef416fc2 1822 char drivers[1024]; /* Location of driver programs */
c934a06c
MS
1823 int pid; /* Process ID for driver program */
1824 cups_file_t *fp; /* Pipe to driver program */
ef416fc2 1825 cups_dir_t *dir; /* Directory pointer */
1826 cups_dentry_t *dent; /* Directory entry */
c934a06c
MS
1827 char *argv[3], /* Arguments for command */
1828 filename[1024], /* Name of driver */
ef416fc2 1829 line[2048], /* Line from driver */
1830 name[512], /* ppd-name */
ef416fc2 1831 make[128], /* ppd-make */
b94498cf 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 */
3d8365b8 1836 psversion[128], /* ppd-psversion */
1837 type_str[128]; /* ppd-type */
1838 int type; /* PPD type */
b94498cf 1839 ppd_info_t *ppd; /* Newly added PPD */
ef416fc2 1840
1841
1842 /*
1843 * Try opening the driver directory...
1844 */
1845
1846 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
1847 server_bin = CUPS_SERVERBIN;
1848
1849 snprintf(drivers, sizeof(drivers), "%s/driver", server_bin);
1850
1851 if ((dir = cupsDirOpen(drivers)) == NULL)
1852 {
1853 fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory "
839a51c8
MS
1854 "\"%s\": %s\n",
1855 drivers, strerror(errno));
ef416fc2 1856 return (0);
1857 }
1858
1859 /*
1860 * Loop through all of the device drivers...
1861 */
1862
c934a06c
MS
1863 argv[1] = (char *)"list";
1864 argv[2] = NULL;
1865
ef416fc2 1866 while ((dent = cupsDirRead(dir)) != NULL)
1867 {
1868 /*
1869 * Only look at executable files...
1870 */
1871
1872 if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode))
1873 continue;
1874
1875 /*
1876 * Run the driver with no arguments and collect the output...
1877 */
1878
c934a06c
MS
1879 argv[0] = dent->filename;
1880 snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename);
1881
1882 if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL)
ef416fc2 1883 {
c934a06c 1884 while (cupsFileGets(fp, line, sizeof(line)))
ef416fc2 1885 {
1886 /*
1887 * Each line is of the form:
1888 *
f899b121 1889 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
b94498cf 1890 * "ppd-device-id" "ppd-product" "ppd-psversion"
ef416fc2 1891 */
1892
bd7854cb 1893 device_id[0] = '\0';
f899b121 1894 product[0] = '\0';
b94498cf 1895 psversion[0] = '\0';
3d8365b8 1896 strcpy(type_str, "postscript");
bd7854cb 1897
1898 if (sscanf(line, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
b94498cf 1899 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
3d8365b8 1900 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1901 "%*[ \t]\"%127[^\"]\"",
b94498cf 1902 name, languages, make, make_and_model,
3d8365b8 1903 device_id, product, psversion, type_str) < 4)
ef416fc2 1904 {
1905 /*
1906 * Bad format; strip trailing newline and write an error message.
1907 */
1908
1909 if (line[strlen(line) - 1] == '\n')
1910 line[strlen(line) - 1] = '\0';
1911
1912 fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1913 dent->filename, line);
1914 break;
1915 }
1916 else
1917 {
1918 /*
1919 * Add the device to the array of available devices...
1920 */
1921
b94498cf 1922 if ((start = strchr(languages, ',')) != NULL)
1923 *start++ = '\0';
1924
3d8365b8 1925 for (type = 0;
1926 type < (int)(sizeof(ppd_types) / sizeof(ppd_types[0]));
1927 type ++)
1928 if (!strcmp(type_str, ppd_types[type]))
1929 break;
1930
1931 if (type >= (int)(sizeof(ppd_types) / sizeof(ppd_types[0])))
1932 {
c934a06c
MS
1933 fprintf(stderr,
1934 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
3d8365b8 1935 type_str);
1936 type = PPD_TYPE_UNKNOWN;
1937 }
1938
66ab9486 1939 ppd = add_ppd("", name, languages, make, make_and_model, device_id,
3d8365b8 1940 product, psversion, 0, 0, 0, type);
b94498cf 1941
1942 if (!ppd)
ef416fc2 1943 {
1944 cupsDirClose(dir);
c934a06c 1945 cupsFileClose(fp);
ef416fc2 1946 return (0);
1947 }
1948
b94498cf 1949 if (start && *start)
1950 {
1951 for (i = 1; i < PPD_MAX_LANG && *start; i ++)
1952 {
1953 if ((ptr = strchr(start, ',')) != NULL)
1954 *ptr++ = '\0';
1955 else
1956 ptr = start + strlen(start);
1957
1958 strlcpy(ppd->record.languages[i], start,
1959 sizeof(ppd->record.languages[0]));
1960
1961 start = ptr;
1962 }
1963 }
1964
ef416fc2 1965 fprintf(stderr, "DEBUG: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1966 name);
1967 }
1968 }
1969
c934a06c 1970 cupsFileClose(fp);
ef416fc2 1971 }
1972 else
1973 fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1974 filename, strerror(errno));
1975 }
1976
1977 cupsDirClose(dir);
1978
1979 return (1);
1980}
1981
1982
1983/*
4509bb49 1984 * End of "$Id$".
ef416fc2 1985 */