]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-driverd.c
2 * "$Id: cups-driverd.c 6508 2007-05-03 20:07:14Z mike $"
4 * PPD/driver support for the Common UNIX Printing System (CUPS).
6 * This program handles listing and installing both static PPD files
7 * in CUPS_DATADIR/model and dynamically generated PPD files using
8 * the driver helper programs in CUPS_SERVERBIN/driver.
10 * Copyright 1997-2007 by Easy Software Products.
12 * These coded instructions, statements, and computer programs are the
13 * property of Easy Software Products and are protected by Federal
14 * copyright law. Distribution and use rights are outlined in the file
15 * "LICENSE.txt" which should have been included with this file. If this
16 * file is missing or damaged please contact Easy Software Products
19 * Attn: CUPS Licensing Information
20 * Easy Software Products
21 * 44141 Airport View Drive, Suite 204
22 * Hollywood, Maryland 20636 USA
24 * Voice: (301) 373-9600
25 * EMail: cups-info@cups.org
26 * WWW: http://www.cups.org
30 * main() - Scan for drivers and return an IPP response.
31 * add_ppd() - Add a PPD file.
32 * cat_ppd() - Copy a PPD file to stdout.
33 * compare_names() - Compare PPD filenames for sorting.
34 * compare_ppds() - Compare PPD file make and model names for sorting.
35 * free_array() - Free an array of strings.
36 * list_ppds() - List PPD files.
37 * load_ppds() - Load PPD files recursively.
38 * load_drivers() - Load driver-generated PPD files.
42 * Include necessary headers...
47 #include <cups/transcode.h>
51 * Private PPD functions...
54 extern cups_encoding_t
_ppdGetEncoding(const char *name
);
61 #define PPD_SYNC 0x50504433 /* Sync word for ppds.dat (PPD3) */
62 #define PPD_MAX_LANG 32 /* Maximum languages */
63 #define PPD_MAX_PROD 8 /* Maximum products */
64 #define PPD_MAX_VERS 8 /* Maximum versions */
68 * PPD information structures...
71 typedef struct /**** PPD record ****/
73 time_t mtime
; /* Modification time */
74 size_t size
; /* Size in bytes */
75 char name
[512], /* PPD name */
76 languages
[PPD_MAX_LANG
][6],
77 /* LanguageVersion/cupsLanguages */
78 products
[PPD_MAX_PROD
][128],
80 psversions
[PPD_MAX_VERS
][32],
81 /* PSVersion strings */
82 make
[128], /* Manufacturer */
83 make_and_model
[128], /* NickName/ModelName */
84 device_id
[128]; /* IEEE 1284 Device ID */
87 typedef struct /**** In-memory record ****/
89 int found
; /* 1 if PPD is found */
90 ppd_rec_t record
; /* PPDs.dat record */
98 int NumPPDs
, /* Number of PPD files */
99 SortedPPDs
, /* Number of sorted PPD files */
100 AllocPPDs
; /* Number of allocated entries */
101 ppd_info_t
*PPDs
; /* PPD file info */
102 int ChangedPPD
; /* Did we change the PPD database? */
109 static ppd_info_t
*add_ppd(const char *name
, const char *language
,
110 const char *make
, const char *make_and_model
,
111 const char *device_id
, const char *product
,
112 const char *psversion
, time_t mtime
,
114 static int cat_ppd(const char *name
, int request_id
);
115 static int compare_names(const ppd_info_t
*p0
,
116 const ppd_info_t
*p1
);
117 static int compare_ppds(const ppd_info_t
*p0
,
118 const ppd_info_t
*p1
);
119 static void free_array(cups_array_t
*a
);
120 static int list_ppds(int request_id
, int limit
, const char *opt
);
121 static int load_drivers(void);
122 static int load_ppds(const char *d
, const char *p
, int descend
);
126 * 'main()' - Scan for drivers and return an IPP response.
130 * cups-driverd request_id limit options
133 int /* O - Exit code */
134 main(int argc
, /* I - Number of command-line args */
135 char *argv
[]) /* I - Command-line arguments */
138 * Install or list PPDs...
141 if (argc
== 3 && !strcmp(argv
[1], "cat"))
142 return (cat_ppd(argv
[2], 0));
143 else if (argc
== 4 && !strcmp(argv
[1], "get"))
144 return (cat_ppd(argv
[3], atoi(argv
[2])));
145 else if (argc
== 5 && !strcmp(argv
[1], "list"))
146 return (list_ppds(atoi(argv
[2]), atoi(argv
[3]), argv
[4]));
149 fputs("Usage: cups-driverd cat ppd-name\n", stderr
);
150 fputs("Usage: cups-driverd get request_id ppd-name\n", stderr
);
151 fputs("Usage: cups-driverd list request_id limit options\n", stderr
);
158 * 'add_ppd()' - Add a PPD file.
161 static ppd_info_t
* /* O - PPD */
162 add_ppd(const char *name
, /* I - PPD name */
163 const char *language
, /* I - LanguageVersion */
164 const char *make
, /* I - Manufacturer */
165 const char *make_and_model
, /* I - NickName/ModelName */
166 const char *device_id
, /* I - 1284DeviceID */
167 const char *product
, /* I - Product */
168 const char *psversion
, /* I - PSVersion */
169 time_t mtime
, /* I - Modification time */
170 size_t size
) /* I - File size */
172 ppd_info_t
*ppd
; /* PPD */
173 char *recommended
; /* Foomatic driver string */
177 * Add a new PPD file...
180 if (NumPPDs
>= AllocPPDs
)
183 * Allocate (more) memory for the PPD files...
189 ppd
= malloc(sizeof(ppd_info_t
) * AllocPPDs
);
191 ppd
= realloc(PPDs
, sizeof(ppd_info_t
) * AllocPPDs
);
196 "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n",
204 ppd
= PPDs
+ NumPPDs
;
208 * Zero-out the PPD data and copy the values over...
211 memset(ppd
, 0, sizeof(ppd_info_t
));
214 ppd
->record
.mtime
= mtime
;
215 ppd
->record
.size
= size
;
217 strlcpy(ppd
->record
.name
, name
, sizeof(ppd
->record
.name
));
218 strlcpy(ppd
->record
.languages
[0], language
,
219 sizeof(ppd
->record
.languages
[0]));
220 strlcpy(ppd
->record
.products
[0], product
, sizeof(ppd
->record
.products
[0]));
221 strlcpy(ppd
->record
.psversions
[0], psversion
,
222 sizeof(ppd
->record
.psversions
[0]));
223 strlcpy(ppd
->record
.make
, make
, sizeof(ppd
->record
.make
));
224 strlcpy(ppd
->record
.make_and_model
, make_and_model
,
225 sizeof(ppd
->record
.make_and_model
));
226 strlcpy(ppd
->record
.device_id
, device_id
, sizeof(ppd
->record
.device_id
));
229 * Strip confusing (and often wrong) "recommended" suffix added by
230 * Foomatic drivers...
233 if ((recommended
= strstr(ppd
->record
.make_and_model
,
234 " (recommended)")) != NULL
)
238 * Return the new PPD pointer...
246 * 'cat_ppd()' - Copy a PPD file to stdout.
249 static int /* O - Exit code */
250 cat_ppd(const char *name
, /* I - PPD name */
251 int request_id
) /* I - Request ID for response? */
253 char scheme
[256], /* Scheme from PPD name */
254 *sptr
; /* Pointer into scheme */
255 char line
[1024]; /* Line/filename */
256 char message
[2048]; /* status-message */
260 * Figure out if this is a static or dynamic PPD file...
263 strlcpy(scheme
, name
, sizeof(scheme
));
264 if ((sptr
= strchr(scheme
, ':')) != NULL
)
268 if (!strcmp(scheme
, "file"))
271 * "file:name" == "name"...
281 puts("Content-Type: application/ipp\n");
286 * Dynamic PPD, see if we have a driver program to support it...
289 const char *serverbin
; /* CUPS_SERVERBIN env var */
292 if ((serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
293 serverbin
= CUPS_SERVERBIN
;
295 snprintf(line
, sizeof(line
), "%s/driver/%s", serverbin
, scheme
);
296 if (access(line
, X_OK
))
299 * File does not exist or is not executable...
302 fprintf(stderr
, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n",
303 line
, strerror(errno
));
307 snprintf(message
, sizeof(message
), "Unable to access \"%s\" - %s",
308 line
, strerror(errno
));
310 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
311 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
312 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
313 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
315 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
316 cupsdSendIPPTrailer();
323 * Yes, let it cat the PPD file...
328 cupsdSendIPPHeader(IPP_OK
, request_id
);
329 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
330 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
331 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
333 cupsdSendIPPTrailer();
336 if (execl(line
, scheme
, "cat", name
, (char *)NULL
))
339 * Unable to execute driver...
342 fprintf(stderr
, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n",
343 line
, strerror(errno
));
350 * Static PPD, see if we have a valid path and it exists...
353 cups_file_t
*fp
; /* PPD file */
354 const char *datadir
; /* CUPS_DATADIR env var */
357 if (name
[0] == '/' || strstr(name
, "../") || strstr(name
, "/.."))
363 fprintf(stderr
, "ERROR: [cups-driverd] Bad PPD name \"%s\"!\n", name
);
367 snprintf(message
, sizeof(message
), "Bad PPD name \"%s\"!", name
);
369 cupsdSendIPPHeader(IPP_OK
, request_id
);
370 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
371 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
372 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
374 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
375 cupsdSendIPPTrailer();
382 * Try opening the file...
386 if (!strncmp(name
, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
387 !strncmp(name
, "Library/Printers/PPDs/Contents/Resources/", 41))
390 * Map ppd-name to Mac OS X standard locations...
393 snprintf(line
, sizeof(line
), "/%s", name
);
397 #elif defined(__linux)
398 if (!strncmp(name
, "lsb/usr/", 8))
401 * Map ppd-name to LSB standard /usr/share/ppd location...
404 snprintf(line
, sizeof(line
), "/usr/share/ppd/%s", name
+ 8);
406 else if (!strncmp(name
, "lsb/opt/", 8))
409 * Map ppd-name to LSB standard /opt/share/ppd location...
412 snprintf(line
, sizeof(line
), "/opt/share/ppd/%s", name
+ 8);
414 else if (!strncmp(name
, "lsb/local/", 10))
417 * Map ppd-name to LSB standard /usr/local/share/ppd location...
420 snprintf(line
, sizeof(line
), "/usr/local/share/ppd/%s", name
+ 10);
424 #endif /* __APPLE__ */
426 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
427 datadir
= CUPS_DATADIR
;
429 snprintf(line
, sizeof(line
), "%s/model/%s", datadir
, name
);
432 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
434 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n",
435 line
, strerror(errno
));
439 snprintf(message
, sizeof(message
), "Unable to open \"%s\" - %s",
440 line
, strerror(errno
));
442 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
443 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
444 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
445 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
447 cupsdSendIPPString(IPP_TAG_TEXT
, "status-message", message
);
448 cupsdSendIPPTrailer();
456 cupsdSendIPPHeader(IPP_OK
, request_id
);
457 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
458 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
459 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language",
461 cupsdSendIPPTrailer();
465 * Now copy the file to stdout...
468 while (cupsFileGets(fp
, line
, sizeof(line
)))
475 * Return with no errors...
483 * 'compare_names()' - Compare PPD filenames for sorting.
486 static int /* O - Result of comparison */
487 compare_names(const ppd_info_t
*p0
, /* I - First PPD file */
488 const ppd_info_t
*p1
) /* I - Second PPD file */
490 return (strcasecmp(p0
->record
.name
, p1
->record
.name
));
495 * 'compare_ppds()' - Compare PPD file make and model names for sorting.
498 static int /* O - Result of comparison */
499 compare_ppds(const ppd_info_t
*p0
, /* I - First PPD file */
500 const ppd_info_t
*p1
) /* I - Second PPD file */
502 int diff
; /* Difference between strings */
506 * First compare manufacturers...
509 if ((diff
= strcasecmp(p0
->record
.make
, p1
->record
.make
)) != 0)
511 else if ((diff
= cupsdCompareNames(p0
->record
.make_and_model
,
512 p1
->record
.make_and_model
)) != 0)
515 return (strcasecmp(p0
->record
.languages
[0],
516 p1
->record
.languages
[0]));
521 * 'free_array()' - Free an array of strings.
525 free_array(cups_array_t
*a
) /* I - Array to free */
527 char *ptr
; /* Pointer to string */
530 for (ptr
= (char *)cupsArrayFirst(a
);
532 ptr
= (char *)cupsArrayNext(a
))
540 * 'list_ppds()' - List PPD files.
543 static int /* O - Exit code */
544 list_ppds(int request_id
, /* I - Request ID */
545 int limit
, /* I - Limit */
546 const char *opt
) /* I - Option argument */
548 int i
, j
; /* Looping vars */
549 int count
; /* Number of PPDs to send */
550 ppd_info_t
*ppd
; /* Current PPD file */
551 cups_file_t
*fp
; /* ppds.dat file */
552 struct stat fileinfo
; /* ppds.dat information */
553 char filename
[1024], /* ppds.dat filename */
554 model
[1024]; /* Model directory */
555 const char *cups_cachedir
; /* CUPS_CACHEDIR environment variable */
556 const char *cups_datadir
; /* CUPS_DATADIR environment variable */
557 int num_options
; /* Number of options */
558 cups_option_t
*options
; /* Options */
559 const char *requested
, /* requested-attributes option */
560 *device_id
, /* ppd-device-id option */
561 *language
, /* ppd-natural-language option */
562 *make
, /* ppd-make option */
563 *make_and_model
, /* ppd-make-and-model option */
564 *product
, /* ppd-product option */
565 *psversion
; /* ppd-psversion option */
566 int mam_len
, /* Length of ppd-make-and-model */
567 device_id_len
, /* Length of ppd-device-id */
568 send_natural_language
, /* Send ppd-natural-language? */
569 send_make
, /* Send ppd-make? */
570 send_make_and_model
, /* Send ppd-make-and-model? */
571 send_name
, /* Send ppd-name? */
572 send_device_id
, /* Send ppd-device-id? */
573 send_product
, /* Send ppd-product? */
574 send_psversion
, /* Send ppd-psversion? */
575 sent_header
; /* Sent the IPP header? */
579 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
580 "opt=\"%s\"\n", request_id
, limit
, opt
);
583 * See if we a PPD database file...
588 PPDs
= (ppd_info_t
*)NULL
;
591 if ((cups_cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
592 cups_cachedir
= CUPS_CACHEDIR
;
594 snprintf(filename
, sizeof(filename
), "%s/ppds.dat", cups_cachedir
);
595 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
598 * See if we have the right sync word...
601 unsigned ppdsync
; /* Sync word */
603 if (cupsFileRead(fp
, (char *)&ppdsync
, sizeof(ppdsync
))
604 == sizeof(ppdsync
) &&
605 ppdsync
== PPD_SYNC
&&
606 !stat(filename
, &fileinfo
) &&
607 ((fileinfo
.st_size
- sizeof(ppdsync
)) % sizeof(ppd_rec_t
)) == 0 &&
608 (NumPPDs
= (fileinfo
.st_size
- sizeof(ppdsync
)) /
609 sizeof(ppd_rec_t
)) > 0)
612 * We have a ppds.dat file, so read it!
615 if ((PPDs
= malloc(sizeof(ppd_info_t
) * NumPPDs
)) == NULL
)
617 "ERROR: [cups-driverd] Unable to allocate memory for %d "
618 "PPD files!\n", NumPPDs
);
623 for (i
= NumPPDs
, ppd
= PPDs
; i
> 0; i
--, ppd
++)
625 cupsFileRead(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
));
629 fprintf(stderr
, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
638 * Load all PPDs in the specified directory and below...
641 SortedPPDs
= NumPPDs
;
643 if ((cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
644 cups_datadir
= CUPS_DATADIR
;
646 snprintf(model
, sizeof(model
), "%s/model", cups_datadir
);
647 load_ppds(model
, "", 1);
651 * Load PPDs from standard Mac OS X locations...
654 load_ppds("/Library/Printers/PPDs/Contents/Resources",
655 "Library/Printers/PPDs/Contents/Resources", 0);
656 load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
657 "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
658 load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
659 "System/Library/Printers/PPDs/Contents/Resources", 0);
660 load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
661 "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
663 #elif defined(__linux)
665 * Load PPDs from LSB-defined locations...
668 load_ppds("/usr/local/share/ppds", "lsb/local", 1);
669 load_ppds("/usr/share/ppds", "lsb/usr", 1);
670 load_ppds("/opt/share/ppds", "lsb/opt", 1);
671 #endif /* __APPLE__ */
674 * Cull PPD files that are no longer present...
677 for (i
= NumPPDs
, ppd
= PPDs
; i
> 0; i
--, ppd
++)
681 * Remove this PPD file from the list...
685 memmove(ppd
, ppd
+ 1, (i
- 1) * sizeof(ppd_info_t
));
692 * Sort the PPDs by name...
696 qsort(PPDs
, NumPPDs
, sizeof(ppd_info_t
),
697 (int (*)(const void *, const void *))compare_names
);
700 * Write the new ppds.dat file...
705 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
707 unsigned ppdsync
= PPD_SYNC
; /* Sync word */
710 cupsFileWrite(fp
, (char *)&ppdsync
, sizeof(ppdsync
));
712 for (i
= NumPPDs
, ppd
= PPDs
; i
> 0; i
--, ppd
++)
713 cupsFileWrite(fp
, (char *)&(ppd
->record
), sizeof(ppd_rec_t
));
717 fprintf(stderr
, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
721 fprintf(stderr
, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
722 filename
, strerror(errno
));
725 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr
);
728 * Scan for dynamic PPD files...
734 * Add the raw driver...
737 add_ppd("raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0);
740 * Sort the PPDs by make and model...
744 qsort(PPDs
, NumPPDs
, sizeof(ppd_info_t
),
745 (int (*)(const void *, const void *))compare_ppds
);
748 * Send IPP attributes...
751 num_options
= cupsParseOptions(opt
, 0, &options
);
752 requested
= cupsGetOption("requested-attributes", num_options
, options
);
753 device_id
= cupsGetOption("ppd-device-id", num_options
, options
);
754 language
= cupsGetOption("ppd-natural-language", num_options
, options
);
755 make
= cupsGetOption("ppd-make", num_options
, options
);
756 make_and_model
= cupsGetOption("ppd-make-and-model", num_options
, options
);
757 product
= cupsGetOption("ppd-product", num_options
, options
);
758 psversion
= cupsGetOption("ppd-psversion", num_options
, options
);
761 mam_len
= strlen(make_and_model
);
766 device_id_len
= strlen(device_id
);
771 fprintf(stderr
, "DEBUG: [cups-driverd] requested-attributes=\"%s\"\n",
774 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-device-id=\"%s\"\n",
777 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-natural-language=\"%s\"\n",
780 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-make=\"%s\"\n",
783 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-make-and-model=\"%s\"\n",
786 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-product=\"%s\"\n",
789 fprintf(stderr
, "DEBUG: [cups-driverd] ppd-psversion=\"%s\"\n",
792 if (!requested
|| strstr(requested
, "all"))
796 send_make_and_model
= 1;
797 send_natural_language
= 1;
804 send_name
= strstr(requested
, "ppd-name") != NULL
;
805 send_make
= strstr(requested
, "ppd-make,") != NULL
||
806 strstr(requested
, ",ppd-make") != NULL
||
807 !strcmp(requested
, "ppd-make");
808 send_make_and_model
= strstr(requested
, "ppd-make-and-model") != NULL
;
809 send_natural_language
= strstr(requested
, "ppd-natural-language") != NULL
;
810 send_device_id
= strstr(requested
, "ppd-device-id") != NULL
;
811 send_product
= strstr(requested
, "ppd-product") != NULL
;
812 send_psversion
= strstr(requested
, "ppd-psversion") != NULL
;
815 puts("Content-Type: application/ipp\n");
819 if (limit
<= 0 || limit
> NumPPDs
)
824 for (i
= NumPPDs
, ppd
= PPDs
; count
> 0 && i
> 0; i
--, ppd
++)
827 * Filter PPDs based on make, model, or device ID...
830 if (device_id
&& strncasecmp(ppd
->record
.device_id
, device_id
,
832 continue; /* TODO: implement smart compare */
836 for (j
= 0; j
< PPD_MAX_LANG
; j
++)
837 if (!ppd
->record
.languages
[j
][0] ||
838 !strcasecmp(ppd
->record
.languages
[j
], language
))
841 if (j
>= PPD_MAX_LANG
|| !ppd
->record
.languages
[j
][0])
845 if (make
&& strcasecmp(ppd
->record
.make
, make
))
848 if (make_and_model
&& strncasecmp(ppd
->record
.make_and_model
,
849 make_and_model
, mam_len
))
854 for (j
= 0; j
< PPD_MAX_PROD
; j
++)
855 if (!ppd
->record
.products
[j
][0] ||
856 !strcasecmp(ppd
->record
.products
[j
], product
))
859 if (j
>= PPD_MAX_PROD
|| !ppd
->record
.products
[j
][0])
865 for (j
= 0; j
< PPD_MAX_VERS
; j
++)
866 if (!ppd
->record
.psversions
[j
][0] ||
867 !strcasecmp(ppd
->record
.psversions
[j
], psversion
))
870 if (j
>= PPD_MAX_VERS
|| !ppd
->record
.psversions
[j
][0])
882 cupsdSendIPPHeader(IPP_OK
, request_id
);
883 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
884 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
885 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
888 fprintf(stderr
, "DEBUG: [cups-driverd] Sending %s (%s)...\n",
889 ppd
->record
.name
, ppd
->record
.make_and_model
);
893 cupsdSendIPPGroup(IPP_TAG_PRINTER
);
896 cupsdSendIPPString(IPP_TAG_NAME
, "ppd-name", ppd
->record
.name
);
898 if (send_natural_language
)
900 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "ppd-natural-language",
901 ppd
->record
.languages
[0]);
903 for (j
= 1; j
< PPD_MAX_LANG
&& ppd
->record
.languages
[j
][0]; j
++)
904 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "", ppd
->record
.languages
[j
]);
908 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make", ppd
->record
.make
);
910 if (send_make_and_model
)
911 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-make-and-model",
912 ppd
->record
.make_and_model
);
915 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-device-id",
916 ppd
->record
.device_id
);
920 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-product",
921 ppd
->record
.products
[0]);
923 for (j
= 1; j
< PPD_MAX_PROD
&& ppd
->record
.products
[j
][0]; j
++)
924 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.products
[j
]);
929 cupsdSendIPPString(IPP_TAG_TEXT
, "ppd-psversion",
930 ppd
->record
.psversions
[0]);
932 for (j
= 1; j
< PPD_MAX_VERS
&& ppd
->record
.psversions
[j
][0]; j
++)
933 cupsdSendIPPString(IPP_TAG_TEXT
, "", ppd
->record
.psversions
[j
]);
937 * If we have only requested the ppd-make attribute, then skip
938 * the remaining PPDs with this make...
941 if (requested
&& !strcmp(requested
, "ppd-make"))
943 const char *this_make
; /* This ppd-make */
946 for (this_make
= ppd
->record
.make
, i
--, ppd
++; i
> 0; i
--, ppd
++)
947 if (strcasecmp(this_make
, ppd
->record
.make
))
957 cupsdSendIPPHeader(IPP_NOT_FOUND
, request_id
);
958 cupsdSendIPPGroup(IPP_TAG_OPERATION
);
959 cupsdSendIPPString(IPP_TAG_CHARSET
, "attributes-charset", "utf-8");
960 cupsdSendIPPString(IPP_TAG_LANGUAGE
, "attributes-natural-language", "en-US");
963 cupsdSendIPPTrailer();
970 * 'load_ppds()' - Load PPD files recursively.
973 static int /* O - 1 on success, 0 on failure */
974 load_ppds(const char *d
, /* I - Actual directory */
975 const char *p
, /* I - Virtual path in name */
976 int descend
) /* I - Descend into directories? */
978 int i
; /* Looping var */
979 cups_file_t
*fp
; /* Pointer to file */
980 cups_dir_t
*dir
; /* Directory pointer */
981 cups_dentry_t
*dent
; /* Directory entry */
982 char filename
[1024], /* Name of PPD or directory */
983 line
[256], /* Line from backend */
984 *ptr
, /* Pointer into name */
985 name
[128], /* Name of PPD file */
986 lang_version
[64], /* PPD LanguageVersion */
987 lang_encoding
[64], /* PPD LanguageEncoding */
988 country
[64], /* Country code */
989 manufacturer
[256], /* Manufacturer */
990 make_model
[256], /* Make and Model */
991 model_name
[256], /* ModelName */
992 nick_name
[256], /* NickName */
993 device_id
[256], /* 1284DeviceID */
994 product
[256], /* Product */
995 psversion
[256]; /* PSVersion */
996 cups_array_t
*products
, /* Product array */
997 *psversions
, /* PSVersion array */
998 *cups_languages
; /* cupsLanguages array */
999 ppd_info_t
*ppd
, /* New PPD file */
1000 key
; /* Search key */
1001 int new_ppd
; /* Is this a new PPD? */
1002 struct /* LanguageVersion translation table */
1004 const char *version
, /* LanguageVersion string */
1005 *language
; /* Language code */
1008 { "chinese", "zh" },
1011 { "english", "en" },
1012 { "finnish", "fi" },
1016 { "italian", "it" },
1017 { "japanese", "ja" },
1018 { "norwegian", "no" },
1020 { "portuguese", "pt" },
1021 { "russian", "ru" },
1023 { "spanish", "es" },
1024 { "swedish", "sv" },
1029 if ((dir
= cupsDirOpen(d
)) == NULL
)
1032 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
1033 d
, strerror(errno
));
1037 while ((dent
= cupsDirRead(dir
)) != NULL
)
1040 * Skip files/directories starting with "."...
1043 if (dent
->filename
[0] == '.')
1047 * See if this is a file...
1050 snprintf(filename
, sizeof(filename
), "%s/%s", d
, dent
->filename
);
1053 snprintf(name
, sizeof(name
), "%s/%s", p
, dent
->filename
);
1055 strlcpy(name
, dent
->filename
, sizeof(name
));
1057 if (S_ISDIR(dent
->fileinfo
.st_mode
))
1060 * Do subdirectory...
1064 if (!load_ppds(filename
, name
, 1))
1074 * See if this file has been scanned before...
1079 strcpy(key
.record
.name
, name
);
1081 ppd
= bsearch(&key
, PPDs
, SortedPPDs
, sizeof(ppd_info_t
),
1082 (int (*)(const void *, const void *))compare_names
);
1085 ppd
->record
.size
== dent
->fileinfo
.st_size
&&
1086 ppd
->record
.mtime
== dent
->fileinfo
.st_mtime
)
1096 * No, file is new/changed, so re-scan it...
1099 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
1103 * Now see if this is a PPD file...
1107 cupsFileGets(fp
, line
, sizeof(line
));
1109 if (strncmp(line
, "*PPD-Adobe:", 11))
1112 * Nope, close the file and continue...
1121 * Now read until we get the NickName field...
1124 cups_languages
= cupsArrayNew(NULL
, NULL
);
1125 products
= cupsArrayNew(NULL
, NULL
);
1126 psversions
= cupsArrayNew(NULL
, NULL
);
1128 model_name
[0] = '\0';
1129 nick_name
[0] = '\0';
1130 manufacturer
[0] = '\0';
1131 device_id
[0] = '\0';
1132 lang_encoding
[0] = '\0';
1133 strcpy(lang_version
, "en");
1135 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
1137 if (!strncmp(line
, "*Manufacturer:", 14))
1138 sscanf(line
, "%*[^\"]\"%255[^\"]", manufacturer
);
1139 else if (!strncmp(line
, "*ModelName:", 11))
1140 sscanf(line
, "%*[^\"]\"%127[^\"]", model_name
);
1141 else if (!strncmp(line
, "*LanguageEncoding:", 18))
1142 sscanf(line
, "%*[^:]:%63s", lang_encoding
);
1143 else if (!strncmp(line
, "*LanguageVersion:", 17))
1144 sscanf(line
, "%*[^:]:%63s", lang_version
);
1145 else if (!strncmp(line
, "*NickName:", 10))
1146 sscanf(line
, "%*[^\"]\"%255[^\"]", nick_name
);
1147 else if (!strncasecmp(line
, "*1284DeviceID:", 14))
1148 sscanf(line
, "%*[^\"]\"%255[^\"]", device_id
);
1149 else if (!strncasecmp(line
, "*Product:", 9))
1151 sscanf(line
, "%*[^\"]\"(%255[^)]", product
);
1152 cupsArrayAdd(products
, strdup(product
));
1154 else if (!strncasecmp(line
, "*PSVersion:", 11))
1156 sscanf(line
, "%*[^\"]\"%255[^\"]", psversion
);
1157 cupsArrayAdd(psversions
, strdup(psversion
));
1159 else if (!strncasecmp(line
, "*cupsLanguages:", 15))
1161 char *start
; /* Start of language */
1164 for (start
= line
+ 15; *start
&& isspace(*start
& 255); start
++);
1166 if (*start
++ == '\"')
1170 for (ptr
= start
+ 1;
1171 *ptr
&& *ptr
!= '\"' && !isspace(*ptr
& 255);
1178 while (isspace(*ptr
& 255))
1182 cupsArrayAdd(cups_languages
, strdup(start
));
1187 else if (!strncmp(line
, "*OpenUI", 7))
1190 * Stop early if we have a NickName or ModelName attributes
1191 * before the first OpenUI...
1194 if ((model_name
[0] || nick_name
[0]) && cupsArrayCount(products
) > 0 &&
1195 cupsArrayCount(psversions
) > 0)
1207 * See if we got all of the required info...
1211 cupsCharsetToUTF8((cups_utf8_t
*)make_model
, nick_name
,
1212 sizeof(make_model
), _ppdGetEncoding(lang_encoding
));
1214 strcpy(make_model
, model_name
);
1216 while (isspace(make_model
[0] & 255))
1217 _cups_strcpy(make_model
, make_model
+ 1);
1219 if (!make_model
[0] || cupsArrayCount(products
) == 0 ||
1220 cupsArrayCount(psversions
) == 0)
1223 * We don't have all the info needed, so skip this file...
1227 fprintf(stderr
, "WARNING: Missing NickName and ModelName in %s!\n",
1230 if (cupsArrayCount(products
) == 0)
1231 fprintf(stderr
, "WARNING: Missing Product in %s!\n", filename
);
1233 if (cupsArrayCount(psversions
) == 0)
1234 fprintf(stderr
, "WARNING: Missing PSVersion in %s!\n", filename
);
1236 free_array(products
);
1237 free_array(psversions
);
1238 free_array(cups_languages
);
1244 * See if we got a manufacturer...
1247 while (isspace(manufacturer
[0] & 255))
1248 _cups_strcpy(manufacturer
, manufacturer
+ 1);
1250 if (!manufacturer
[0] || !strcmp(manufacturer
, "ESP"))
1253 * Nope, copy the first part of the make and model then...
1256 strlcpy(manufacturer
, make_model
, sizeof(manufacturer
));
1259 * Truncate at the first space, dash, or slash, or make the
1260 * manufacturer "Other"...
1263 for (ptr
= manufacturer
; *ptr
; ptr
++)
1264 if (*ptr
== ' ' || *ptr
== '-' || *ptr
== '/')
1267 if (*ptr
&& ptr
> manufacturer
)
1269 else if (!strncasecmp(manufacturer
, "agfa", 4))
1270 strcpy(manufacturer
, "AGFA");
1271 else if (!strncasecmp(manufacturer
, "herk", 4) ||
1272 !strncasecmp(manufacturer
, "linotype", 8))
1273 strcpy(manufacturer
, "LHAG");
1275 strcpy(manufacturer
, "Other");
1278 * Hack for various vendors...
1281 if (!strcasecmp(manufacturer
, "XPrint"))
1282 strcpy(manufacturer
, "Xerox");
1283 else if (!strcasecmp(manufacturer
, "Eastman"))
1284 strcpy(manufacturer
, "Kodak");
1285 else if (!strcasecmp(manufacturer
, "laserwriter"))
1286 strcpy(manufacturer
, "Apple");
1287 else if (!strcasecmp(manufacturer
, "colorpoint"))
1288 strcpy(manufacturer
, "Seiko");
1289 else if (!strcasecmp(manufacturer
, "fiery"))
1290 strcpy(manufacturer
, "EFI");
1291 else if (!strcasecmp(manufacturer
, "ps") ||
1292 !strcasecmp(manufacturer
, "colorpass"))
1293 strcpy(manufacturer
, "Canon");
1294 else if (!strncasecmp(manufacturer
, "primera", 7))
1295 strcpy(manufacturer
, "Fargo");
1296 else if (!strcasecmp(manufacturer
, "designjet"))
1297 strcpy(manufacturer
, "HP");
1299 else if (!strncasecmp(manufacturer
, "LHAG", 4) ||
1300 !strncasecmp(manufacturer
, "linotype", 8))
1301 strcpy(manufacturer
, "LHAG");
1304 * Fix the lang_version as needed...
1307 if ((ptr
= strchr(lang_version
, '-')) != NULL
)
1309 else if ((ptr
= strchr(lang_version
, '_')) != NULL
)
1315 * Setup the country suffix...
1319 _cups_strcpy(country
+ 1, ptr
);
1324 * No country suffix...
1330 for (i
= 0; i
< (int)(sizeof(languages
) / sizeof(languages
[0])); i
++)
1331 if (!strcasecmp(languages
[i
].version
, lang_version
))
1334 if (i
< (int)(sizeof(languages
) / sizeof(languages
[0])))
1337 * Found a known language...
1340 snprintf(lang_version
, sizeof(lang_version
), "%s%s",
1341 languages
[i
].language
, country
);
1346 * Unknown language; use "xx"...
1349 strcpy(lang_version
, "xx");
1353 * Add the PPD file...
1361 * Add new PPD file...
1364 fprintf(stderr
, "DEBUG: [cups-driverd] Adding ppd \"%s\"...\n", name
);
1366 ppd
= add_ppd(name
, lang_version
, manufacturer
, make_model
, device_id
,
1367 (char *)cupsArrayFirst(products
),
1368 (char *)cupsArrayFirst(psversions
),
1369 dent
->fileinfo
.st_mtime
, dent
->fileinfo
.st_size
);
1380 * Update existing record...
1383 fprintf(stderr
, "DEBUG: [cups-driverd] Updating ppd \"%s\"...\n", name
);
1385 memset(ppd
, 0, sizeof(ppd_info_t
));
1388 ppd
->record
.mtime
= dent
->fileinfo
.st_mtime
;
1389 ppd
->record
.size
= dent
->fileinfo
.st_size
;
1391 strlcpy(ppd
->record
.name
, name
, sizeof(ppd
->record
.name
));
1392 strlcpy(ppd
->record
.make
, manufacturer
, sizeof(ppd
->record
.make
));
1393 strlcpy(ppd
->record
.make_and_model
, make_model
,
1394 sizeof(ppd
->record
.make_and_model
));
1395 strlcpy(ppd
->record
.languages
[0], lang_version
,
1396 sizeof(ppd
->record
.languages
[0]));
1397 strlcpy(ppd
->record
.products
[0], (char *)cupsArrayFirst(products
),
1398 sizeof(ppd
->record
.products
[0]));
1399 strlcpy(ppd
->record
.psversions
[0], (char *)cupsArrayFirst(psversions
),
1400 sizeof(ppd
->record
.psversions
[0]));
1401 strlcpy(ppd
->record
.device_id
, device_id
, sizeof(ppd
->record
.device_id
));
1405 * Add remaining products, versions, and languages...
1409 i
< PPD_MAX_PROD
&& (ptr
= (char *)cupsArrayNext(products
)) != NULL
;
1411 strlcpy(ppd
->record
.products
[i
], ptr
,
1412 sizeof(ppd
->record
.products
[0]));
1415 i
< PPD_MAX_VERS
&& (ptr
= (char *)cupsArrayNext(psversions
)) != NULL
;
1417 strlcpy(ppd
->record
.psversions
[i
], ptr
,
1418 sizeof(ppd
->record
.psversions
[0]));
1420 for (i
= 1, ptr
= (char *)cupsArrayFirst(cups_languages
);
1421 i
< PPD_MAX_LANG
&& ptr
;
1422 i
++, ptr
= (char *)cupsArrayNext(cups_languages
))
1423 strlcpy(ppd
->record
.languages
[i
], ptr
,
1424 sizeof(ppd
->record
.languages
[0]));
1427 * Free products, versions, and languages...
1430 free_array(cups_languages
);
1431 free_array(products
);
1432 free_array(psversions
);
1444 * 'load_drivers()' - Load driver-generated PPD files.
1447 static int /* O - 1 on success, 0 on failure */
1450 int i
; /* Looping var */
1451 char *start
, /* Start of value */
1452 *ptr
; /* Pointer into string */
1453 const char *server_bin
; /* CUPS_SERVERBIN env variable */
1454 char drivers
[1024]; /* Location of driver programs */
1455 FILE *fp
; /* Pipe to driver program */
1456 cups_dir_t
*dir
; /* Directory pointer */
1457 cups_dentry_t
*dent
; /* Directory entry */
1458 char filename
[1024], /* Name of driver */
1459 line
[2048], /* Line from driver */
1460 name
[512], /* ppd-name */
1461 make
[128], /* ppd-make */
1462 make_and_model
[128], /* ppd-make-and-model */
1463 device_id
[128], /* ppd-device-id */
1464 languages
[128], /* ppd-natural-language */
1465 product
[128], /* ppd-product */
1466 psversion
[128]; /* ppd-psversion */
1467 ppd_info_t
*ppd
; /* Newly added PPD */
1471 * Try opening the driver directory...
1474 if ((server_bin
= getenv("CUPS_SERVERBIN")) == NULL
)
1475 server_bin
= CUPS_SERVERBIN
;
1477 snprintf(drivers
, sizeof(drivers
), "%s/driver", server_bin
);
1479 if ((dir
= cupsDirOpen(drivers
)) == NULL
)
1481 fprintf(stderr
, "ERROR: [cups-driverd] Unable to open driver directory "
1483 drivers
, strerror(errno
));
1488 * Loop through all of the device drivers...
1491 while ((dent
= cupsDirRead(dir
)) != NULL
)
1494 * Only look at executable files...
1497 if (!(dent
->fileinfo
.st_mode
& 0111) || !S_ISREG(dent
->fileinfo
.st_mode
))
1501 * Run the driver with no arguments and collect the output...
1504 snprintf(filename
, sizeof(filename
), "%s/%s list", drivers
, dent
->filename
);
1505 if ((fp
= popen(filename
, "r")) != NULL
)
1507 while (fgets(line
, sizeof(line
), fp
) != NULL
)
1510 * Each line is of the form:
1512 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
1513 * "ppd-device-id" "ppd-product" "ppd-psversion"
1516 device_id
[0] = '\0';
1518 psversion
[0] = '\0';
1520 if (sscanf(line
, "\"%511[^\"]\"%127s%*[ \t]\"%127[^\"]\""
1521 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1522 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\"",
1523 name
, languages
, make
, make_and_model
,
1524 device_id
, product
, psversion
) < 4)
1527 * Bad format; strip trailing newline and write an error message.
1530 if (line
[strlen(line
) - 1] == '\n')
1531 line
[strlen(line
) - 1] = '\0';
1533 fprintf(stderr
, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1534 dent
->filename
, line
);
1540 * Add the device to the array of available devices...
1543 if ((start
= strchr(languages
, ',')) != NULL
)
1546 ppd
= add_ppd(name
, languages
, make
, make_and_model
, device_id
,
1547 product
, psversion
, 0, 0);
1555 if (start
&& *start
)
1557 for (i
= 1; i
< PPD_MAX_LANG
&& *start
; i
++)
1559 if ((ptr
= strchr(start
, ',')) != NULL
)
1562 ptr
= start
+ strlen(start
);
1564 strlcpy(ppd
->record
.languages
[i
], start
,
1565 sizeof(ppd
->record
.languages
[0]));
1571 fprintf(stderr
, "DEBUG: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1579 fprintf(stderr
, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1580 filename
, strerror(errno
));
1590 * End of "$Id: cups-driverd.c 6508 2007-05-03 20:07:14Z mike $".