4 * PPD cache implementation for CUPS.
6 * Copyright 2010-2015 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * Include necessary headers...
21 #include "cups-private.h"
26 * Macro to test for two almost-equal PWG measurements.
29 #define _PWG_EQUIVALENT(x, y) (abs((x)-(y)) < 2)
36 static int pwg_compare_finishings(_pwg_finishings_t
*a
,
37 _pwg_finishings_t
*b
);
38 static void pwg_free_finishings(_pwg_finishings_t
*f
);
39 static void pwg_ppdize_name(const char *ipp
, char *name
, size_t namesize
);
40 static void pwg_unppdize_name(const char *ppd
, char *name
, size_t namesize
,
41 const char *dashchars
);
45 * '_ppdCacheCreateWithFile()' - Create PPD cache and mapping data from a
48 * Use the @link _ppdCacheWriteFile@ function to write PWG mapping data to a
52 _ppd_cache_t
* /* O - PPD cache and mapping data */
53 _ppdCacheCreateWithFile(
54 const char *filename
, /* I - File to read */
55 ipp_t
**attrs
) /* IO - IPP attributes, if any */
57 cups_file_t
*fp
; /* File */
58 _ppd_cache_t
*pc
; /* PWG mapping data */
59 pwg_size_t
*size
; /* Current size */
60 pwg_map_t
*map
; /* Current map */
61 _pwg_finishings_t
*finishings
; /* Current finishings option */
62 int linenum
, /* Current line number */
63 num_bins
, /* Number of bins in file */
64 num_sizes
, /* Number of sizes in file */
65 num_sources
, /* Number of sources in file */
66 num_types
; /* Number of types in file */
67 char line
[2048], /* Current line */
68 *value
, /* Pointer to value in line */
69 *valueptr
, /* Pointer into value */
70 pwg_keyword
[128], /* PWG keyword */
71 ppd_keyword
[PPD_MAX_NAME
];
73 _pwg_print_color_mode_t print_color_mode
;
74 /* Print color mode for preset */
75 _pwg_print_quality_t print_quality
; /* Print quality for preset */
78 DEBUG_printf(("_ppdCacheCreateWithFile(filename=\"%s\")", filename
));
81 * Range check input...
89 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
97 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
99 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
104 * Read the first line and make sure it has "#CUPS-PPD-CACHE-version" in it...
107 if (!cupsFileGets(fp
, line
, sizeof(line
)))
109 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
110 DEBUG_puts("_ppdCacheCreateWithFile: Unable to read first line.");
115 if (strncmp(line
, "#CUPS-PPD-CACHE-", 16))
117 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
118 DEBUG_printf(("_ppdCacheCreateWithFile: Wrong first line \"%s\".", line
));
123 if (atoi(line
+ 16) != _PPD_CACHE_VERSION
)
125 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Out of date PPD cache file."), 1);
126 DEBUG_printf(("_ppdCacheCreateWithFile: Cache file has version %s, "
127 "expected %d.", line
+ 16, _PPD_CACHE_VERSION
));
133 * Allocate the mapping data structure...
136 if ((pc
= calloc(1, sizeof(_ppd_cache_t
))) == NULL
)
138 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
139 DEBUG_puts("_ppdCacheCreateWithFile: Unable to allocate _ppd_cache_t.");
143 pc
->max_copies
= 9999;
155 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
157 DEBUG_printf(("_ppdCacheCreateWithFile: line=\"%s\", value=\"%s\", "
158 "linenum=%d", line
, value
, linenum
));
162 DEBUG_printf(("_ppdCacheCreateWithFile: Missing value on line %d.",
164 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
167 else if (!_cups_strcasecmp(line
, "Filter"))
170 pc
->filters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
171 (cups_acopy_func_t
)_cupsStrAlloc
,
172 (cups_afree_func_t
)_cupsStrFree
);
174 cupsArrayAdd(pc
->filters
, value
);
176 else if (!_cups_strcasecmp(line
, "PreFilter"))
179 pc
->prefilters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
180 (cups_acopy_func_t
)_cupsStrAlloc
,
181 (cups_afree_func_t
)_cupsStrFree
);
183 cupsArrayAdd(pc
->prefilters
, value
);
185 else if (!_cups_strcasecmp(line
, "Product"))
187 pc
->product
= _cupsStrAlloc(value
);
189 else if (!_cups_strcasecmp(line
, "SingleFile"))
191 pc
->single_file
= !_cups_strcasecmp(value
, "true");
193 else if (!_cups_strcasecmp(line
, "IPP"))
195 off_t pos
= cupsFileTell(fp
), /* Position in file */
196 length
= strtol(value
, NULL
, 10);
197 /* Length of IPP attributes */
201 DEBUG_puts("_ppdCacheCreateWithFile: IPP listed multiple times.");
202 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
205 else if (length
<= 0)
207 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP length.");
208 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
215 * Read IPP attributes into the provided variable...
220 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
221 *attrs
) != IPP_STATE_DATA
)
223 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
224 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
231 * Skip the IPP data entirely...
234 cupsFileSeek(fp
, pos
+ length
);
237 if (cupsFileTell(fp
) != (pos
+ length
))
239 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
240 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
244 else if (!_cups_strcasecmp(line
, "NumBins"))
248 DEBUG_puts("_ppdCacheCreateWithFile: NumBins listed multiple times.");
249 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
253 if ((num_bins
= atoi(value
)) <= 0 || num_bins
> 65536)
255 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumBins value %d on line "
256 "%d.", num_sizes
, linenum
));
257 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
261 if ((pc
->bins
= calloc((size_t)num_bins
, sizeof(pwg_map_t
))) == NULL
)
263 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d bins.",
265 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
269 else if (!_cups_strcasecmp(line
, "Bin"))
271 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
273 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Bin on line %d.", linenum
));
274 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
278 if (pc
->num_bins
>= num_bins
)
280 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Bin's on line %d.",
282 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
286 map
= pc
->bins
+ pc
->num_bins
;
287 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
288 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
292 else if (!_cups_strcasecmp(line
, "NumSizes"))
296 DEBUG_puts("_ppdCacheCreateWithFile: NumSizes listed multiple times.");
297 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
301 if ((num_sizes
= atoi(value
)) < 0 || num_sizes
> 65536)
303 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSizes value %d on line "
304 "%d.", num_sizes
, linenum
));
305 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
311 if ((pc
->sizes
= calloc((size_t)num_sizes
, sizeof(pwg_size_t
))) == NULL
)
313 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sizes.",
315 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
320 else if (!_cups_strcasecmp(line
, "Size"))
322 if (pc
->num_sizes
>= num_sizes
)
324 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Size's on line %d.",
326 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
330 size
= pc
->sizes
+ pc
->num_sizes
;
332 if (sscanf(value
, "%127s%40s%d%d%d%d%d%d", pwg_keyword
, ppd_keyword
,
333 &(size
->width
), &(size
->length
), &(size
->left
),
334 &(size
->bottom
), &(size
->right
), &(size
->top
)) != 8)
336 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Size on line %d.",
338 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
342 size
->map
.pwg
= _cupsStrAlloc(pwg_keyword
);
343 size
->map
.ppd
= _cupsStrAlloc(ppd_keyword
);
347 else if (!_cups_strcasecmp(line
, "CustomSize"))
349 if (pc
->custom_max_width
> 0)
351 DEBUG_printf(("_ppdCacheCreateWithFile: Too many CustomSize's on line "
353 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
357 if (sscanf(value
, "%d%d%d%d%d%d%d%d", &(pc
->custom_max_width
),
358 &(pc
->custom_max_length
), &(pc
->custom_min_width
),
359 &(pc
->custom_min_length
), &(pc
->custom_size
.left
),
360 &(pc
->custom_size
.bottom
), &(pc
->custom_size
.right
),
361 &(pc
->custom_size
.top
)) != 8)
363 DEBUG_printf(("_ppdCacheCreateWithFile: Bad CustomSize on line %d.",
365 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
369 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "max",
370 pc
->custom_max_width
, pc
->custom_max_length
, NULL
);
371 pc
->custom_max_keyword
= _cupsStrAlloc(pwg_keyword
);
373 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "min",
374 pc
->custom_min_width
, pc
->custom_min_length
, NULL
);
375 pc
->custom_min_keyword
= _cupsStrAlloc(pwg_keyword
);
377 else if (!_cups_strcasecmp(line
, "SourceOption"))
379 pc
->source_option
= _cupsStrAlloc(value
);
381 else if (!_cups_strcasecmp(line
, "NumSources"))
385 DEBUG_puts("_ppdCacheCreateWithFile: NumSources listed multiple "
387 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
391 if ((num_sources
= atoi(value
)) <= 0 || num_sources
> 65536)
393 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSources value %d on "
394 "line %d.", num_sources
, linenum
));
395 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
399 if ((pc
->sources
= calloc((size_t)num_sources
, sizeof(pwg_map_t
))) == NULL
)
401 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sources.",
403 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
407 else if (!_cups_strcasecmp(line
, "Source"))
409 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
411 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Source on line %d.",
413 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
417 if (pc
->num_sources
>= num_sources
)
419 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Source's on line %d.",
421 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
425 map
= pc
->sources
+ pc
->num_sources
;
426 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
427 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
431 else if (!_cups_strcasecmp(line
, "NumTypes"))
435 DEBUG_puts("_ppdCacheCreateWithFile: NumTypes listed multiple times.");
436 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
440 if ((num_types
= atoi(value
)) <= 0 || num_types
> 65536)
442 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumTypes value %d on "
443 "line %d.", num_types
, linenum
));
444 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
448 if ((pc
->types
= calloc((size_t)num_types
, sizeof(pwg_map_t
))) == NULL
)
450 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d types.",
452 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
456 else if (!_cups_strcasecmp(line
, "Type"))
458 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
460 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Type on line %d.",
462 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
466 if (pc
->num_types
>= num_types
)
468 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Type's on line %d.",
470 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
474 map
= pc
->types
+ pc
->num_types
;
475 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
476 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
480 else if (!_cups_strcasecmp(line
, "Preset"))
483 * Preset output-mode print-quality name=value ...
486 print_color_mode
= (_pwg_print_color_mode_t
)strtol(value
, &valueptr
, 10);
487 print_quality
= (_pwg_print_quality_t
)strtol(valueptr
, &valueptr
, 10);
489 if (print_color_mode
< _PWG_PRINT_COLOR_MODE_MONOCHROME
||
490 print_color_mode
>= _PWG_PRINT_COLOR_MODE_MAX
||
491 print_quality
< _PWG_PRINT_QUALITY_DRAFT
||
492 print_quality
>= _PWG_PRINT_QUALITY_MAX
||
493 valueptr
== value
|| !*valueptr
)
495 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Preset on line %d.",
497 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
501 pc
->num_presets
[print_color_mode
][print_quality
] =
502 cupsParseOptions(valueptr
, 0,
503 pc
->presets
[print_color_mode
] + print_quality
);
505 else if (!_cups_strcasecmp(line
, "SidesOption"))
506 pc
->sides_option
= _cupsStrAlloc(value
);
507 else if (!_cups_strcasecmp(line
, "Sides1Sided"))
508 pc
->sides_1sided
= _cupsStrAlloc(value
);
509 else if (!_cups_strcasecmp(line
, "Sides2SidedLong"))
510 pc
->sides_2sided_long
= _cupsStrAlloc(value
);
511 else if (!_cups_strcasecmp(line
, "Sides2SidedShort"))
512 pc
->sides_2sided_short
= _cupsStrAlloc(value
);
513 else if (!_cups_strcasecmp(line
, "Finishings"))
517 cupsArrayNew3((cups_array_func_t
)pwg_compare_finishings
,
519 (cups_afree_func_t
)pwg_free_finishings
);
521 if ((finishings
= calloc(1, sizeof(_pwg_finishings_t
))) == NULL
)
524 finishings
->value
= (ipp_finishings_t
)strtol(value
, &valueptr
, 10);
525 finishings
->num_options
= cupsParseOptions(valueptr
, 0,
526 &(finishings
->options
));
528 cupsArrayAdd(pc
->finishings
, finishings
);
530 else if (!_cups_strcasecmp(line
, "MaxCopies"))
531 pc
->max_copies
= atoi(value
);
532 else if (!_cups_strcasecmp(line
, "ChargeInfoURI"))
533 pc
->charge_info_uri
= _cupsStrAlloc(value
);
534 else if (!_cups_strcasecmp(line
, "JobAccountId"))
535 pc
->account_id
= !_cups_strcasecmp(value
, "true");
536 else if (!_cups_strcasecmp(line
, "JobAccountingUserId"))
537 pc
->accounting_user_id
= !_cups_strcasecmp(value
, "true");
538 else if (!_cups_strcasecmp(line
, "JobPassword"))
539 pc
->password
= _cupsStrAlloc(value
);
540 else if (!_cups_strcasecmp(line
, "Mandatory"))
543 _cupsArrayAddStrings(pc
->mandatory
, value
, ' ');
545 pc
->mandatory
= _cupsArrayNewStrings(value
, ' ');
547 else if (!_cups_strcasecmp(line
, "SupportFile"))
549 if (!pc
->support_files
)
550 pc
->support_files
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
551 (cups_acopy_func_t
)_cupsStrAlloc
,
552 (cups_afree_func_t
)_cupsStrFree
);
554 cupsArrayAdd(pc
->support_files
, value
);
558 DEBUG_printf(("_ppdCacheCreateWithFile: Unknown %s on line %d.", line
,
563 if (pc
->num_sizes
< num_sizes
)
565 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sizes (%d < %d).",
566 pc
->num_sizes
, num_sizes
));
567 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
571 if (pc
->num_sources
< num_sources
)
573 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sources (%d < %d).",
574 pc
->num_sources
, num_sources
));
575 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
579 if (pc
->num_types
< num_types
)
581 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough types (%d < %d).",
582 pc
->num_types
, num_types
));
583 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
592 * If we get here the file was bad - free any data and return...
598 _ppdCacheDestroy(pc
);
611 * '_ppdCacheCreateWithPPD()' - Create PWG mapping data from a PPD file.
614 _ppd_cache_t
* /* O - PPD cache and mapping data */
615 _ppdCacheCreateWithPPD(ppd_file_t
*ppd
) /* I - PPD file */
617 int i
, j
, k
; /* Looping vars */
618 _ppd_cache_t
*pc
; /* PWG mapping data */
619 ppd_option_t
*input_slot
, /* InputSlot option */
620 *media_type
, /* MediaType option */
621 *output_bin
, /* OutputBin option */
622 *color_model
, /* ColorModel option */
623 *duplex
; /* Duplex option */
624 ppd_choice_t
*choice
; /* Current InputSlot/MediaType */
625 pwg_map_t
*map
; /* Current source/type map */
626 ppd_attr_t
*ppd_attr
; /* Current PPD preset attribute */
627 int num_options
; /* Number of preset options and props */
628 cups_option_t
*options
; /* Preset options and properties */
629 ppd_size_t
*ppd_size
; /* Current PPD size */
630 pwg_size_t
*pwg_size
; /* Current PWG size */
631 char pwg_keyword
[3 + PPD_MAX_NAME
+ 1 + 12 + 1 + 12 + 3],
632 /* PWG keyword string */
633 ppd_name
[PPD_MAX_NAME
];
634 /* Normalized PPD name */
635 const char *pwg_name
; /* Standard PWG media name */
636 pwg_media_t
*pwg_media
; /* PWG media data */
637 _pwg_print_color_mode_t pwg_print_color_mode
;
638 /* print-color-mode index */
639 _pwg_print_quality_t pwg_print_quality
;
640 /* print-quality index */
641 int similar
; /* Are the old and new size similar? */
642 pwg_size_t
*old_size
; /* Current old size */
643 int old_imageable
, /* Old imageable length in 2540ths */
644 old_borderless
, /* Old borderless state */
645 old_known_pwg
; /* Old PWG name is well-known */
646 int new_width
, /* New width in 2540ths */
647 new_length
, /* New length in 2540ths */
648 new_left
, /* New left margin in 2540ths */
649 new_bottom
, /* New bottom margin in 2540ths */
650 new_right
, /* New right margin in 2540ths */
651 new_top
, /* New top margin in 2540ths */
652 new_imageable
, /* New imageable length in 2540ths */
653 new_borderless
, /* New borderless state */
654 new_known_pwg
; /* New PWG name is well-known */
655 pwg_size_t
*new_size
; /* New size to add, if any */
656 const char *filter
; /* Current filter */
657 _pwg_finishings_t
*finishings
; /* Current finishings value */
660 DEBUG_printf(("_ppdCacheCreateWithPPD(ppd=%p)", ppd
));
663 * Range check input...
673 if ((pc
= calloc(1, sizeof(_ppd_cache_t
))) == NULL
)
675 DEBUG_puts("_ppdCacheCreateWithPPD: Unable to allocate _ppd_cache_t.");
680 * Copy and convert size data...
683 if (ppd
->num_sizes
> 0)
685 if ((pc
->sizes
= calloc((size_t)ppd
->num_sizes
, sizeof(pwg_size_t
))) == NULL
)
687 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
688 "pwg_size_t's.", ppd
->num_sizes
));
692 for (i
= ppd
->num_sizes
, pwg_size
= pc
->sizes
, ppd_size
= ppd
->sizes
;
697 * Don't copy over custom size...
700 if (!_cups_strcasecmp(ppd_size
->name
, "Custom"))
704 * Convert the PPD size name to the corresponding PWG keyword name.
707 if ((pwg_media
= pwgMediaForPPD(ppd_size
->name
)) != NULL
)
710 * Standard name, do we have conflicts?
713 for (j
= 0; j
< pc
->num_sizes
; j
++)
714 if (!strcmp(pc
->sizes
[j
].map
.pwg
, pwg_media
->pwg
))
724 * Standard name and no conflicts, use it!
727 pwg_name
= pwg_media
->pwg
;
733 * Not a standard name; convert it to a PWG vendor name of the form:
735 * pp_lowerppd_WIDTHxHEIGHTuu
738 pwg_name
= pwg_keyword
;
741 pwg_unppdize_name(ppd_size
->name
, ppd_name
, sizeof(ppd_name
), "_.");
742 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), NULL
, ppd_name
,
743 PWG_FROM_POINTS(ppd_size
->width
),
744 PWG_FROM_POINTS(ppd_size
->length
), NULL
);
748 * If we have a similar paper with non-zero margins then we only want to
749 * keep it if it has a larger imageable area length. The NULL check is for
750 * dimensions that are <= 0...
753 if ((pwg_media
= _pwgMediaNearSize(PWG_FROM_POINTS(ppd_size
->width
),
754 PWG_FROM_POINTS(ppd_size
->length
),
758 new_width
= pwg_media
->width
;
759 new_length
= pwg_media
->length
;
760 new_left
= PWG_FROM_POINTS(ppd_size
->left
);
761 new_bottom
= PWG_FROM_POINTS(ppd_size
->bottom
);
762 new_right
= PWG_FROM_POINTS(ppd_size
->width
- ppd_size
->right
);
763 new_top
= PWG_FROM_POINTS(ppd_size
->length
- ppd_size
->top
);
764 new_imageable
= new_length
- new_top
- new_bottom
;
765 new_borderless
= new_bottom
== 0 && new_top
== 0 &&
766 new_left
== 0 && new_right
== 0;
768 for (k
= pc
->num_sizes
, similar
= 0, old_size
= pc
->sizes
, new_size
= NULL
;
772 old_imageable
= old_size
->length
- old_size
->top
- old_size
->bottom
;
773 old_borderless
= old_size
->left
== 0 && old_size
->bottom
== 0 &&
774 old_size
->right
== 0 && old_size
->top
== 0;
775 old_known_pwg
= strncmp(old_size
->map
.pwg
, "oe_", 3) &&
776 strncmp(old_size
->map
.pwg
, "om_", 3);
778 similar
= old_borderless
== new_borderless
&&
779 _PWG_EQUIVALENT(old_size
->width
, new_width
) &&
780 _PWG_EQUIVALENT(old_size
->length
, new_length
);
783 (new_known_pwg
|| (!old_known_pwg
&& new_imageable
> old_imageable
)))
786 * The new paper has a larger imageable area so it could replace
787 * the older paper. Regardless of the imageable area, we always
788 * prefer the size with a well-known PWG name.
792 _cupsStrFree(old_size
->map
.ppd
);
793 _cupsStrFree(old_size
->map
.pwg
);
800 * The paper was unique enough to deserve its own entry so add it to the
804 new_size
= pwg_size
++;
814 new_size
->map
.ppd
= _cupsStrAlloc(ppd_size
->name
);
815 new_size
->map
.pwg
= _cupsStrAlloc(pwg_name
);
816 new_size
->width
= new_width
;
817 new_size
->length
= new_length
;
818 new_size
->left
= new_left
;
819 new_size
->bottom
= new_bottom
;
820 new_size
->right
= new_right
;
821 new_size
->top
= new_top
;
826 if (ppd
->variable_sizes
)
829 * Generate custom size data...
832 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "max",
833 PWG_FROM_POINTS(ppd
->custom_max
[0]),
834 PWG_FROM_POINTS(ppd
->custom_max
[1]), NULL
);
835 pc
->custom_max_keyword
= _cupsStrAlloc(pwg_keyword
);
836 pc
->custom_max_width
= PWG_FROM_POINTS(ppd
->custom_max
[0]);
837 pc
->custom_max_length
= PWG_FROM_POINTS(ppd
->custom_max
[1]);
839 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "min",
840 PWG_FROM_POINTS(ppd
->custom_min
[0]),
841 PWG_FROM_POINTS(ppd
->custom_min
[1]), NULL
);
842 pc
->custom_min_keyword
= _cupsStrAlloc(pwg_keyword
);
843 pc
->custom_min_width
= PWG_FROM_POINTS(ppd
->custom_min
[0]);
844 pc
->custom_min_length
= PWG_FROM_POINTS(ppd
->custom_min
[1]);
846 pc
->custom_size
.left
= PWG_FROM_POINTS(ppd
->custom_margins
[0]);
847 pc
->custom_size
.bottom
= PWG_FROM_POINTS(ppd
->custom_margins
[1]);
848 pc
->custom_size
.right
= PWG_FROM_POINTS(ppd
->custom_margins
[2]);
849 pc
->custom_size
.top
= PWG_FROM_POINTS(ppd
->custom_margins
[3]);
853 * Copy and convert InputSlot data...
856 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) == NULL
)
857 input_slot
= ppdFindOption(ppd
, "HPPaperSource");
861 pc
->source_option
= _cupsStrAlloc(input_slot
->keyword
);
863 if ((pc
->sources
= calloc((size_t)input_slot
->num_choices
, sizeof(pwg_map_t
))) == NULL
)
865 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
866 "pwg_map_t's for InputSlot.", input_slot
->num_choices
));
870 pc
->num_sources
= input_slot
->num_choices
;
872 for (i
= input_slot
->num_choices
, choice
= input_slot
->choices
,
875 i
--, choice
++, map
++)
877 if (!_cups_strncasecmp(choice
->choice
, "Auto", 4) ||
878 !_cups_strcasecmp(choice
->choice
, "Default"))
880 else if (!_cups_strcasecmp(choice
->choice
, "Cassette"))
882 else if (!_cups_strcasecmp(choice
->choice
, "PhotoTray"))
884 else if (!_cups_strcasecmp(choice
->choice
, "CDTray"))
886 else if (!_cups_strncasecmp(choice
->choice
, "Multipurpose", 12) ||
887 !_cups_strcasecmp(choice
->choice
, "MP") ||
888 !_cups_strcasecmp(choice
->choice
, "MPTray"))
889 pwg_name
= "by-pass-tray";
890 else if (!_cups_strcasecmp(choice
->choice
, "LargeCapacity"))
891 pwg_name
= "large-capacity";
892 else if (!_cups_strncasecmp(choice
->choice
, "Lower", 5))
894 else if (!_cups_strncasecmp(choice
->choice
, "Middle", 6))
896 else if (!_cups_strncasecmp(choice
->choice
, "Upper", 5))
898 else if (!_cups_strncasecmp(choice
->choice
, "Side", 4))
900 else if (!_cups_strcasecmp(choice
->choice
, "Roll"))
901 pwg_name
= "main-roll";
905 * Convert PPD name to lowercase...
908 pwg_name
= pwg_keyword
;
909 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
),
913 map
->pwg
= _cupsStrAlloc(pwg_name
);
914 map
->ppd
= _cupsStrAlloc(choice
->choice
);
919 * Copy and convert MediaType data...
922 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
924 if ((pc
->types
= calloc((size_t)media_type
->num_choices
, sizeof(pwg_map_t
))) == NULL
)
926 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
927 "pwg_map_t's for MediaType.", media_type
->num_choices
));
931 pc
->num_types
= media_type
->num_choices
;
933 for (i
= media_type
->num_choices
, choice
= media_type
->choices
,
936 i
--, choice
++, map
++)
938 if (!_cups_strncasecmp(choice
->choice
, "Auto", 4) ||
939 !_cups_strcasecmp(choice
->choice
, "Any") ||
940 !_cups_strcasecmp(choice
->choice
, "Default"))
942 else if (!_cups_strncasecmp(choice
->choice
, "Card", 4))
943 pwg_name
= "cardstock";
944 else if (!_cups_strncasecmp(choice
->choice
, "Env", 3))
945 pwg_name
= "envelope";
946 else if (!_cups_strncasecmp(choice
->choice
, "Gloss", 5))
947 pwg_name
= "photographic-glossy";
948 else if (!_cups_strcasecmp(choice
->choice
, "HighGloss"))
949 pwg_name
= "photographic-high-gloss";
950 else if (!_cups_strcasecmp(choice
->choice
, "Matte"))
951 pwg_name
= "photographic-matte";
952 else if (!_cups_strncasecmp(choice
->choice
, "Plain", 5))
953 pwg_name
= "stationery";
954 else if (!_cups_strncasecmp(choice
->choice
, "Coated", 6))
955 pwg_name
= "stationery-coated";
956 else if (!_cups_strcasecmp(choice
->choice
, "Inkjet"))
957 pwg_name
= "stationery-inkjet";
958 else if (!_cups_strcasecmp(choice
->choice
, "Letterhead"))
959 pwg_name
= "stationery-letterhead";
960 else if (!_cups_strncasecmp(choice
->choice
, "Preprint", 8))
961 pwg_name
= "stationery-preprinted";
962 else if (!_cups_strcasecmp(choice
->choice
, "Recycled"))
963 pwg_name
= "stationery-recycled";
964 else if (!_cups_strncasecmp(choice
->choice
, "Transparen", 10))
965 pwg_name
= "transparency";
969 * Convert PPD name to lowercase...
972 pwg_name
= pwg_keyword
;
973 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
),
977 map
->pwg
= _cupsStrAlloc(pwg_name
);
978 map
->ppd
= _cupsStrAlloc(choice
->choice
);
983 * Copy and convert OutputBin data...
986 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
988 if ((pc
->bins
= calloc((size_t)output_bin
->num_choices
, sizeof(pwg_map_t
))) == NULL
)
990 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
991 "pwg_map_t's for OutputBin.", output_bin
->num_choices
));
995 pc
->num_bins
= output_bin
->num_choices
;
997 for (i
= output_bin
->num_choices
, choice
= output_bin
->choices
,
1000 i
--, choice
++, map
++)
1002 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
), "_");
1004 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
1005 map
->ppd
= _cupsStrAlloc(choice
->choice
);
1009 if ((ppd_attr
= ppdFindAttr(ppd
, "APPrinterPreset", NULL
)) != NULL
)
1012 * Copy and convert APPrinterPreset (output-mode + print-quality) data...
1015 const char *quality
, /* com.apple.print.preset.quality value */
1016 *output_mode
, /* com.apple.print.preset.output-mode value */
1017 *color_model_val
, /* ColorModel choice */
1018 *graphicsType
, /* com.apple.print.preset.graphicsType value */
1019 *media_front_coating
; /* com.apple.print.preset.media-front-coating value */
1023 num_options
= _ppdParseOptions(ppd_attr
->value
, 0, &options
,
1026 if ((quality
= cupsGetOption("com.apple.print.preset.quality",
1027 num_options
, options
)) != NULL
)
1030 * Get the print-quality for this preset...
1033 if (!strcmp(quality
, "low"))
1034 pwg_print_quality
= _PWG_PRINT_QUALITY_DRAFT
;
1035 else if (!strcmp(quality
, "high"))
1036 pwg_print_quality
= _PWG_PRINT_QUALITY_HIGH
;
1038 pwg_print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
1041 * Ignore graphicsType "Photo" presets that are not high quality.
1044 graphicsType
= cupsGetOption("com.apple.print.preset.graphicsType",
1045 num_options
, options
);
1047 if (pwg_print_quality
!= _PWG_PRINT_QUALITY_HIGH
&& graphicsType
&&
1048 !strcmp(graphicsType
, "Photo"))
1052 * Ignore presets for normal and draft quality where the coating
1053 * isn't "none" or "autodetect".
1056 media_front_coating
= cupsGetOption(
1057 "com.apple.print.preset.media-front-coating",
1058 num_options
, options
);
1060 if (pwg_print_quality
!= _PWG_PRINT_QUALITY_HIGH
&&
1061 media_front_coating
&&
1062 strcmp(media_front_coating
, "none") &&
1063 strcmp(media_front_coating
, "autodetect"))
1067 * Get the output mode for this preset...
1070 output_mode
= cupsGetOption("com.apple.print.preset.output-mode",
1071 num_options
, options
);
1072 color_model_val
= cupsGetOption("ColorModel", num_options
, options
);
1076 if (!strcmp(output_mode
, "monochrome"))
1077 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
1079 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1081 else if (color_model_val
)
1083 if (!_cups_strcasecmp(color_model_val
, "Gray"))
1084 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
1086 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1089 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1092 * Save the options for this combination as needed...
1095 if (!pc
->num_presets
[pwg_print_color_mode
][pwg_print_quality
])
1096 pc
->num_presets
[pwg_print_color_mode
][pwg_print_quality
] =
1097 _ppdParseOptions(ppd_attr
->value
, 0,
1098 pc
->presets
[pwg_print_color_mode
] +
1099 pwg_print_quality
, _PPD_PARSE_OPTIONS
);
1102 cupsFreeOptions(num_options
, options
);
1104 while ((ppd_attr
= ppdFindNextAttr(ppd
, "APPrinterPreset", NULL
)) != NULL
);
1107 if (!pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_DRAFT
] &&
1108 !pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_NORMAL
] &&
1109 !pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_HIGH
])
1112 * Try adding some common color options to create grayscale presets. These
1113 * are listed in order of popularity...
1116 const char *color_option
= NULL
, /* Color control option */
1117 *gray_choice
= NULL
; /* Choice to select grayscale */
1119 if ((color_model
= ppdFindOption(ppd
, "ColorModel")) != NULL
&&
1120 ppdFindChoice(color_model
, "Gray"))
1122 color_option
= "ColorModel";
1123 gray_choice
= "Gray";
1125 else if ((color_model
= ppdFindOption(ppd
, "HPColorMode")) != NULL
&&
1126 ppdFindChoice(color_model
, "grayscale"))
1128 color_option
= "HPColorMode";
1129 gray_choice
= "grayscale";
1131 else if ((color_model
= ppdFindOption(ppd
, "BRMonoColor")) != NULL
&&
1132 ppdFindChoice(color_model
, "Mono"))
1134 color_option
= "BRMonoColor";
1135 gray_choice
= "Mono";
1137 else if ((color_model
= ppdFindOption(ppd
, "CNIJSGrayScale")) != NULL
&&
1138 ppdFindChoice(color_model
, "1"))
1140 color_option
= "CNIJSGrayScale";
1143 else if ((color_model
= ppdFindOption(ppd
, "HPColorAsGray")) != NULL
&&
1144 ppdFindChoice(color_model
, "True"))
1146 color_option
= "HPColorAsGray";
1147 gray_choice
= "True";
1150 if (color_option
&& gray_choice
)
1153 * Copy and convert ColorModel (output-mode) data...
1156 cups_option_t
*coption
, /* Color option */
1157 *moption
; /* Monochrome option */
1159 for (pwg_print_quality
= _PWG_PRINT_QUALITY_DRAFT
;
1160 pwg_print_quality
< _PWG_PRINT_QUALITY_MAX
;
1161 pwg_print_quality
++)
1163 if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][pwg_print_quality
])
1166 * Copy the color options...
1169 num_options
= pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
]
1170 [pwg_print_quality
];
1171 options
= calloc(sizeof(cups_option_t
), (size_t)num_options
);
1175 for (i
= num_options
, moption
= options
,
1176 coption
= pc
->presets
[_PWG_PRINT_COLOR_MODE_COLOR
]
1177 [pwg_print_quality
];
1179 i
--, moption
++, coption
++)
1181 moption
->name
= _cupsStrRetain(coption
->name
);
1182 moption
->value
= _cupsStrRetain(coption
->value
);
1185 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1187 pc
->presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1191 else if (pwg_print_quality
!= _PWG_PRINT_QUALITY_NORMAL
)
1195 * Add the grayscale option to the preset...
1198 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1199 cupsAddOption(color_option
, gray_choice
,
1200 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
]
1201 [pwg_print_quality
],
1202 pc
->presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
] +
1209 * Copy and convert Duplex (sides) data...
1212 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
1213 if ((duplex
= ppdFindOption(ppd
, "JCLDuplex")) == NULL
)
1214 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
1215 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
1216 duplex
= ppdFindOption(ppd
, "KD03Duplex");
1220 pc
->sides_option
= _cupsStrAlloc(duplex
->keyword
);
1222 for (i
= duplex
->num_choices
, choice
= duplex
->choices
;
1226 if ((!_cups_strcasecmp(choice
->choice
, "None") ||
1227 !_cups_strcasecmp(choice
->choice
, "False")) && !pc
->sides_1sided
)
1228 pc
->sides_1sided
= _cupsStrAlloc(choice
->choice
);
1229 else if ((!_cups_strcasecmp(choice
->choice
, "DuplexNoTumble") ||
1230 !_cups_strcasecmp(choice
->choice
, "LongEdge") ||
1231 !_cups_strcasecmp(choice
->choice
, "Top")) && !pc
->sides_2sided_long
)
1232 pc
->sides_2sided_long
= _cupsStrAlloc(choice
->choice
);
1233 else if ((!_cups_strcasecmp(choice
->choice
, "DuplexTumble") ||
1234 !_cups_strcasecmp(choice
->choice
, "ShortEdge") ||
1235 !_cups_strcasecmp(choice
->choice
, "Bottom")) &&
1236 !pc
->sides_2sided_short
)
1237 pc
->sides_2sided_short
= _cupsStrAlloc(choice
->choice
);
1242 * Copy filters and pre-filters...
1245 pc
->filters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
1246 (cups_acopy_func_t
)_cupsStrAlloc
,
1247 (cups_afree_func_t
)_cupsStrFree
);
1249 cupsArrayAdd(pc
->filters
,
1250 "application/vnd.cups-raw application/octet-stream 0 -");
1252 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFilter2", NULL
)) != NULL
)
1256 cupsArrayAdd(pc
->filters
, ppd_attr
->value
);
1258 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsFilter2", NULL
)) != NULL
);
1260 else if (ppd
->num_filters
> 0)
1262 for (i
= 0; i
< ppd
->num_filters
; i
++)
1263 cupsArrayAdd(pc
->filters
, ppd
->filters
[i
]);
1266 cupsArrayAdd(pc
->filters
, "application/vnd.cups-postscript 0 -");
1269 * See if we have a command filter...
1272 for (filter
= (const char *)cupsArrayFirst(pc
->filters
);
1274 filter
= (const char *)cupsArrayNext(pc
->filters
))
1275 if (!_cups_strncasecmp(filter
, "application/vnd.cups-command", 28) &&
1276 _cups_isspace(filter
[28]))
1280 ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) == NULL
||
1281 _cups_strcasecmp(ppd_attr
->value
, "none")))
1284 * No command filter and no cupsCommands keyword telling us not to use one.
1285 * See if this is a PostScript printer, and if so add a PostScript command
1289 for (filter
= (const char *)cupsArrayFirst(pc
->filters
);
1291 filter
= (const char *)cupsArrayNext(pc
->filters
))
1292 if (!_cups_strncasecmp(filter
, "application/vnd.cups-postscript", 31) &&
1293 _cups_isspace(filter
[31]))
1297 cupsArrayAdd(pc
->filters
,
1298 "application/vnd.cups-command application/postscript 100 "
1302 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
1304 pc
->prefilters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
1305 (cups_acopy_func_t
)_cupsStrAlloc
,
1306 (cups_afree_func_t
)_cupsStrFree
);
1310 cupsArrayAdd(pc
->prefilters
, ppd_attr
->value
);
1312 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
);
1315 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsSingleFile", NULL
)) != NULL
)
1316 pc
->single_file
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1319 * Copy the product string, if any...
1323 pc
->product
= _cupsStrAlloc(ppd
->product
);
1326 * Copy finishings mapping data...
1329 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsIPPFinishings", NULL
)) != NULL
)
1331 pc
->finishings
= cupsArrayNew3((cups_array_func_t
)pwg_compare_finishings
,
1332 NULL
, NULL
, 0, NULL
,
1333 (cups_afree_func_t
)pwg_free_finishings
);
1337 if ((finishings
= calloc(1, sizeof(_pwg_finishings_t
))) == NULL
)
1340 finishings
->value
= (ipp_finishings_t
)atoi(ppd_attr
->spec
);
1341 finishings
->num_options
= _ppdParseOptions(ppd_attr
->value
, 0,
1342 &(finishings
->options
),
1343 _PPD_PARSE_OPTIONS
);
1345 cupsArrayAdd(pc
->finishings
, finishings
);
1347 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsIPPFinishings",
1355 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsMaxCopies", NULL
)) != NULL
)
1356 pc
->max_copies
= atoi(ppd_attr
->value
);
1357 else if (ppd
->manual_copies
)
1360 pc
->max_copies
= 9999;
1363 * cupsChargeInfoURI, cupsJobAccountId, cupsJobAccountingUserId,
1364 * cupsJobPassword, and cupsMandatory.
1367 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsChargeInfoURI", NULL
)) != NULL
)
1368 pc
->charge_info_uri
= _cupsStrAlloc(ppd_attr
->value
);
1370 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobAccountId", NULL
)) != NULL
)
1371 pc
->account_id
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1373 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobAccountingUserId", NULL
)) != NULL
)
1374 pc
->accounting_user_id
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1376 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobPassword", NULL
)) != NULL
)
1377 pc
->password
= _cupsStrAlloc(ppd_attr
->value
);
1379 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsMandatory", NULL
)) != NULL
)
1380 pc
->mandatory
= _cupsArrayNewStrings(ppd_attr
->value
, ' ');
1386 pc
->support_files
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
1387 (cups_acopy_func_t
)_cupsStrAlloc
,
1388 (cups_afree_func_t
)_cupsStrFree
);
1390 for (ppd_attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
1392 ppd_attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
1393 cupsArrayAdd(pc
->support_files
, ppd_attr
->value
);
1395 if ((ppd_attr
= ppdFindAttr(ppd
, "APPrinterIconPath", NULL
)) != NULL
)
1396 cupsArrayAdd(pc
->support_files
, ppd_attr
->value
);
1399 * Return the cache data...
1405 * If we get here we need to destroy the PWG mapping data and return NULL...
1410 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Out of memory."), 1);
1411 _ppdCacheDestroy(pc
);
1418 * '_ppdCacheDestroy()' - Free all memory used for PWG mapping data.
1422 _ppdCacheDestroy(_ppd_cache_t
*pc
) /* I - PPD cache and mapping data */
1424 int i
; /* Looping var */
1425 pwg_map_t
*map
; /* Current map */
1426 pwg_size_t
*size
; /* Current size */
1430 * Range check input...
1437 * Free memory as needed...
1442 for (i
= pc
->num_bins
, map
= pc
->bins
; i
> 0; i
--, map
++)
1444 _cupsStrFree(map
->pwg
);
1445 _cupsStrFree(map
->ppd
);
1453 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
1455 _cupsStrFree(size
->map
.pwg
);
1456 _cupsStrFree(size
->map
.ppd
);
1462 if (pc
->source_option
)
1463 _cupsStrFree(pc
->source_option
);
1467 for (i
= pc
->num_sources
, map
= pc
->sources
; i
> 0; i
--, map
++)
1469 _cupsStrFree(map
->pwg
);
1470 _cupsStrFree(map
->ppd
);
1478 for (i
= pc
->num_types
, map
= pc
->types
; i
> 0; i
--, map
++)
1480 _cupsStrFree(map
->pwg
);
1481 _cupsStrFree(map
->ppd
);
1487 if (pc
->custom_max_keyword
)
1488 _cupsStrFree(pc
->custom_max_keyword
);
1490 if (pc
->custom_min_keyword
)
1491 _cupsStrFree(pc
->custom_min_keyword
);
1493 _cupsStrFree(pc
->product
);
1494 cupsArrayDelete(pc
->filters
);
1495 cupsArrayDelete(pc
->prefilters
);
1496 cupsArrayDelete(pc
->finishings
);
1498 _cupsStrFree(pc
->charge_info_uri
);
1499 _cupsStrFree(pc
->password
);
1501 cupsArrayDelete(pc
->mandatory
);
1503 cupsArrayDelete(pc
->support_files
);
1510 * '_ppdCacheGetBin()' - Get the PWG output-bin keyword associated with a PPD
1514 const char * /* O - output-bin or NULL */
1516 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1517 const char *output_bin
) /* I - PPD OutputBin string */
1519 int i
; /* Looping var */
1523 * Range check input...
1526 if (!pc
|| !output_bin
)
1530 * Look up the OutputBin string...
1534 for (i
= 0; i
< pc
->num_bins
; i
++)
1535 if (!_cups_strcasecmp(output_bin
, pc
->bins
[i
].ppd
))
1536 return (pc
->bins
[i
].pwg
);
1543 * '_ppdCacheGetFinishingOptions()' - Get PPD finishing options for the given
1544 * IPP finishings value(s).
1547 int /* O - New number of options */
1548 _ppdCacheGetFinishingOptions(
1549 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1550 ipp_t
*job
, /* I - Job attributes or NULL */
1551 ipp_finishings_t value
, /* I - IPP finishings value of IPP_FINISHINGS_NONE */
1552 int num_options
, /* I - Number of options */
1553 cups_option_t
**options
) /* IO - Options */
1555 int i
; /* Looping var */
1556 _pwg_finishings_t
*f
, /* PWG finishings options */
1557 key
; /* Search key */
1558 ipp_attribute_t
*attr
; /* Finishings attribute */
1559 cups_option_t
*option
; /* Current finishings option */
1563 * Range check input...
1566 if (!pc
|| cupsArrayCount(pc
->finishings
) == 0 || !options
||
1567 (!job
&& value
== IPP_FINISHINGS_NONE
))
1568 return (num_options
);
1571 * Apply finishing options...
1574 if (job
&& (attr
= ippFindAttribute(job
, "finishings", IPP_TAG_ENUM
)) != NULL
)
1576 int num_values
= ippGetCount(attr
); /* Number of values */
1578 for (i
= 0; i
< num_values
; i
++)
1580 key
.value
= (ipp_finishings_t
)ippGetInteger(attr
, i
);
1582 if ((f
= cupsArrayFind(pc
->finishings
, &key
)) != NULL
)
1584 int j
; /* Another looping var */
1586 for (j
= f
->num_options
, option
= f
->options
; j
> 0; j
--, option
++)
1587 num_options
= cupsAddOption(option
->name
, option
->value
,
1588 num_options
, options
);
1592 else if (value
!= IPP_FINISHINGS_NONE
)
1596 if ((f
= cupsArrayFind(pc
->finishings
, &key
)) != NULL
)
1598 int j
; /* Another looping var */
1600 for (j
= f
->num_options
, option
= f
->options
; j
> 0; j
--, option
++)
1601 num_options
= cupsAddOption(option
->name
, option
->value
,
1602 num_options
, options
);
1606 return (num_options
);
1611 * '_ppdCacheGetFinishingValues()' - Get IPP finishings value(s) from the given
1615 int /* O - Number of finishings values */
1616 _ppdCacheGetFinishingValues(
1617 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1618 int num_options
, /* I - Number of options */
1619 cups_option_t
*options
, /* I - Options */
1620 int max_values
, /* I - Maximum number of finishings values */
1621 int *values
) /* O - Finishings values */
1623 int i
, /* Looping var */
1624 num_values
= 0; /* Number of values */
1625 _pwg_finishings_t
*f
; /* Current finishings option */
1626 cups_option_t
*option
; /* Current option */
1627 const char *val
; /* Value for option */
1631 * Range check input...
1634 DEBUG_printf(("_ppdCacheGetFinishingValues(pc=%p, num_options=%d, options=%p, max_values=%d, values=%p)", pc
, num_options
, options
, max_values
, values
));
1636 if (!pc
|| !pc
->finishings
|| num_options
< 1 || max_values
< 1 || !values
)
1638 DEBUG_puts("_ppdCacheGetFinishingValues: Bad arguments, returning 0.");
1643 * Go through the finishings options and see what is set...
1646 for (f
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
);
1648 f
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
1650 DEBUG_printf(("_ppdCacheGetFinishingValues: Checking %d (%s)", f
->value
, ippEnumString("finishings", f
->value
)));
1652 for (i
= f
->num_options
, option
= f
->options
; i
> 0; i
--, option
++)
1654 DEBUG_printf(("_ppdCacheGetFinishingValues: %s=%s?", option
->name
, option
->value
));
1656 if ((val
= cupsGetOption(option
->name
, num_options
, options
)) == NULL
||
1657 _cups_strcasecmp(option
->value
, val
))
1659 DEBUG_puts("_ppdCacheGetFinishingValues: NO");
1666 DEBUG_printf(("_ppdCacheGetFinishingValues: Adding %d.", f
->value
));
1668 values
[num_values
++] = f
->value
;
1670 if (num_values
>= max_values
)
1675 DEBUG_printf(("_ppdCacheGetFinishingValues: Returning %d.", num_values
));
1677 return (num_values
);
1682 * '_ppdCacheGetInputSlot()' - Get the PPD InputSlot associated with the job
1683 * attributes or a keyword string.
1686 const char * /* O - PPD InputSlot or NULL */
1687 _ppdCacheGetInputSlot(
1688 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1689 ipp_t
*job
, /* I - Job attributes or NULL */
1690 const char *keyword
) /* I - Keyword string or NULL */
1693 * Range check input...
1696 if (!pc
|| pc
->num_sources
== 0 || (!job
&& !keyword
))
1699 if (job
&& !keyword
)
1702 * Lookup the media-col attribute and any media-source found there...
1705 ipp_attribute_t
*media_col
, /* media-col attribute */
1706 *media_source
; /* media-source attribute */
1707 pwg_size_t size
; /* Dimensional size */
1708 int margins_set
; /* Were the margins set? */
1710 media_col
= ippFindAttribute(job
, "media-col", IPP_TAG_BEGIN_COLLECTION
);
1712 (media_source
= ippFindAttribute(ippGetCollection(media_col
, 0),
1714 IPP_TAG_KEYWORD
)) != NULL
)
1717 * Use the media-source value from media-col...
1720 keyword
= ippGetString(media_source
, 0, NULL
);
1722 else if (pwgInitSize(&size
, job
, &margins_set
))
1725 * For media <= 5x7, look for a photo tray...
1728 if (size
.width
<= (5 * 2540) && size
.length
<= (7 * 2540))
1735 int i
; /* Looping var */
1737 for (i
= 0; i
< pc
->num_sources
; i
++)
1738 if (!_cups_strcasecmp(keyword
, pc
->sources
[i
].pwg
))
1739 return (pc
->sources
[i
].ppd
);
1747 * '_ppdCacheGetMediaType()' - Get the PPD MediaType associated with the job
1748 * attributes or a keyword string.
1751 const char * /* O - PPD MediaType or NULL */
1752 _ppdCacheGetMediaType(
1753 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1754 ipp_t
*job
, /* I - Job attributes or NULL */
1755 const char *keyword
) /* I - Keyword string or NULL */
1758 * Range check input...
1761 if (!pc
|| pc
->num_types
== 0 || (!job
&& !keyword
))
1764 if (job
&& !keyword
)
1767 * Lookup the media-col attribute and any media-source found there...
1770 ipp_attribute_t
*media_col
, /* media-col attribute */
1771 *media_type
; /* media-type attribute */
1773 media_col
= ippFindAttribute(job
, "media-col", IPP_TAG_BEGIN_COLLECTION
);
1776 if ((media_type
= ippFindAttribute(media_col
->values
[0].collection
,
1778 IPP_TAG_KEYWORD
)) == NULL
)
1779 media_type
= ippFindAttribute(media_col
->values
[0].collection
,
1780 "media-type", IPP_TAG_NAME
);
1783 keyword
= media_type
->values
[0].string
.text
;
1789 int i
; /* Looping var */
1791 for (i
= 0; i
< pc
->num_types
; i
++)
1792 if (!_cups_strcasecmp(keyword
, pc
->types
[i
].pwg
))
1793 return (pc
->types
[i
].ppd
);
1801 * '_ppdCacheGetOutputBin()' - Get the PPD OutputBin associated with the keyword
1805 const char * /* O - PPD OutputBin or NULL */
1806 _ppdCacheGetOutputBin(
1807 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1808 const char *output_bin
) /* I - Keyword string */
1810 int i
; /* Looping var */
1814 * Range check input...
1817 if (!pc
|| !output_bin
)
1821 * Look up the OutputBin string...
1825 for (i
= 0; i
< pc
->num_bins
; i
++)
1826 if (!_cups_strcasecmp(output_bin
, pc
->bins
[i
].pwg
))
1827 return (pc
->bins
[i
].ppd
);
1834 * '_ppdCacheGetPageSize()' - Get the PPD PageSize associated with the job
1835 * attributes or a keyword string.
1838 const char * /* O - PPD PageSize or NULL */
1839 _ppdCacheGetPageSize(
1840 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1841 ipp_t
*job
, /* I - Job attributes or NULL */
1842 const char *keyword
, /* I - Keyword string or NULL */
1843 int *exact
) /* O - 1 if exact match, 0 otherwise */
1845 int i
; /* Looping var */
1846 pwg_size_t
*size
, /* Current size */
1847 *closest
, /* Closest size */
1848 jobsize
; /* Size data from job */
1849 int margins_set
, /* Were the margins set? */
1850 dwidth
, /* Difference in width */
1851 dlength
, /* Difference in length */
1852 dleft
, /* Difference in left margins */
1853 dright
, /* Difference in right margins */
1854 dbottom
, /* Difference in bottom margins */
1855 dtop
, /* Difference in top margins */
1856 dmin
, /* Minimum difference */
1857 dclosest
; /* Closest difference */
1858 const char *ppd_name
; /* PPD media name */
1861 DEBUG_printf(("_ppdCacheGetPageSize(pc=%p, job=%p, keyword=\"%s\", exact=%p)",
1862 pc
, job
, keyword
, exact
));
1865 * Range check input...
1868 if (!pc
|| (!job
&& !keyword
))
1879 * Try getting the PPD media name from the job attributes...
1882 ipp_attribute_t
*attr
; /* Job attribute */
1884 if ((attr
= ippFindAttribute(job
, "PageSize", IPP_TAG_ZERO
)) == NULL
)
1885 if ((attr
= ippFindAttribute(job
, "PageRegion", IPP_TAG_ZERO
)) == NULL
)
1886 attr
= ippFindAttribute(job
, "media", IPP_TAG_ZERO
);
1890 DEBUG_printf(("1_ppdCacheGetPageSize: Found attribute %s (%s)",
1891 attr
->name
, ippTagString(attr
->value_tag
)));
1893 DEBUG_puts("1_ppdCacheGetPageSize: Did not find media attribute.");
1896 if (attr
&& (attr
->value_tag
== IPP_TAG_NAME
||
1897 attr
->value_tag
== IPP_TAG_KEYWORD
))
1898 ppd_name
= attr
->values
[0].string
.text
;
1901 DEBUG_printf(("1_ppdCacheGetPageSize: ppd_name=\"%s\"", ppd_name
));
1906 * Try looking up the named PPD size first...
1909 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
1911 DEBUG_printf(("2_ppdCacheGetPageSize: size[%d]=[\"%s\" \"%s\"]",
1912 (int)(size
- pc
->sizes
), size
->map
.pwg
, size
->map
.ppd
));
1914 if (!_cups_strcasecmp(ppd_name
, size
->map
.ppd
) ||
1915 !_cups_strcasecmp(ppd_name
, size
->map
.pwg
))
1920 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", ppd_name
));
1922 return (size
->map
.ppd
);
1927 if (job
&& !keyword
)
1930 * Get the size using media-col or media, with the preference being
1934 if (!pwgInitSize(&jobsize
, job
, &margins_set
))
1940 * Get the size using a media keyword...
1943 pwg_media_t
*media
; /* Media definition */
1946 if ((media
= pwgMediaForPWG(keyword
)) == NULL
)
1947 if ((media
= pwgMediaForLegacy(keyword
)) == NULL
)
1948 if ((media
= pwgMediaForPPD(keyword
)) == NULL
)
1951 jobsize
.width
= media
->width
;
1952 jobsize
.length
= media
->length
;
1957 * Now that we have the dimensions and possibly the margins, look at the
1958 * available sizes and find the match...
1962 dclosest
= 999999999;
1964 if (!ppd_name
|| _cups_strncasecmp(ppd_name
, "Custom.", 7) ||
1965 _cups_strncasecmp(ppd_name
, "custom_", 7))
1967 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
1970 * Adobe uses a size matching algorithm with an epsilon of 5 points, which
1971 * is just about 176/2540ths...
1974 dwidth
= size
->width
- jobsize
.width
;
1975 dlength
= size
->length
- jobsize
.length
;
1977 if (dwidth
<= -176 || dwidth
>= 176 || dlength
<= -176 || dlength
>= 176)
1983 * Use a tighter epsilon of 1 point (35/2540ths) for margins...
1986 dleft
= size
->left
- jobsize
.left
;
1987 dright
= size
->right
- jobsize
.right
;
1988 dtop
= size
->top
- jobsize
.top
;
1989 dbottom
= size
->bottom
- jobsize
.bottom
;
1991 if (dleft
<= -35 || dleft
>= 35 || dright
<= -35 || dright
>= 35 ||
1992 dtop
<= -35 || dtop
>= 35 || dbottom
<= -35 || dbottom
>= 35)
1994 dleft
= dleft
< 0 ? -dleft
: dleft
;
1995 dright
= dright
< 0 ? -dright
: dright
;
1996 dbottom
= dbottom
< 0 ? -dbottom
: dbottom
;
1997 dtop
= dtop
< 0 ? -dtop
: dtop
;
1998 dmin
= dleft
+ dright
+ dbottom
+ dtop
;
2000 if (dmin
< dclosest
)
2013 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", size
->map
.ppd
));
2015 return (size
->map
.ppd
);
2021 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (closest)",
2024 return (closest
->map
.ppd
);
2028 * If we get here we need to check for custom page size support...
2031 if (jobsize
.width
>= pc
->custom_min_width
&&
2032 jobsize
.width
<= pc
->custom_max_width
&&
2033 jobsize
.length
>= pc
->custom_min_length
&&
2034 jobsize
.length
<= pc
->custom_max_length
)
2037 * In range, format as Custom.WWWWxLLLL (points).
2040 snprintf(pc
->custom_ppd_size
, sizeof(pc
->custom_ppd_size
), "Custom.%dx%d",
2041 (int)PWG_TO_POINTS(jobsize
.width
), (int)PWG_TO_POINTS(jobsize
.length
));
2043 if (margins_set
&& exact
)
2045 dleft
= pc
->custom_size
.left
- jobsize
.left
;
2046 dright
= pc
->custom_size
.right
- jobsize
.right
;
2047 dtop
= pc
->custom_size
.top
- jobsize
.top
;
2048 dbottom
= pc
->custom_size
.bottom
- jobsize
.bottom
;
2050 if (dleft
> -35 && dleft
< 35 && dright
> -35 && dright
< 35 &&
2051 dtop
> -35 && dtop
< 35 && dbottom
> -35 && dbottom
< 35)
2057 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (custom)",
2058 pc
->custom_ppd_size
));
2060 return (pc
->custom_ppd_size
);
2064 * No custom page size support or the size is out of range - return NULL.
2067 DEBUG_puts("1_ppdCacheGetPageSize: Returning NULL");
2074 * '_ppdCacheGetSize()' - Get the PWG size associated with a PPD PageSize.
2077 pwg_size_t
* /* O - PWG size or NULL */
2079 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2080 const char *page_size
) /* I - PPD PageSize */
2082 int i
; /* Looping var */
2083 pwg_media_t
*media
; /* Media */
2084 pwg_size_t
*size
; /* Current size */
2088 * Range check input...
2091 if (!pc
|| !page_size
)
2094 if (!_cups_strncasecmp(page_size
, "Custom.", 7))
2097 * Custom size; size name can be one of the following:
2099 * Custom.WIDTHxLENGTHin - Size in inches
2100 * Custom.WIDTHxLENGTHft - Size in feet
2101 * Custom.WIDTHxLENGTHcm - Size in centimeters
2102 * Custom.WIDTHxLENGTHmm - Size in millimeters
2103 * Custom.WIDTHxLENGTHm - Size in meters
2104 * Custom.WIDTHxLENGTH[pt] - Size in points
2107 double w
, l
; /* Width and length of page */
2108 char *ptr
; /* Pointer into PageSize */
2109 struct lconv
*loc
; /* Locale data */
2112 w
= (float)_cupsStrScand(page_size
+ 7, &ptr
, loc
);
2113 if (!ptr
|| *ptr
!= 'x')
2116 l
= (float)_cupsStrScand(ptr
+ 1, &ptr
, loc
);
2120 if (!_cups_strcasecmp(ptr
, "in"))
2125 else if (!_cups_strcasecmp(ptr
, "ft"))
2130 else if (!_cups_strcasecmp(ptr
, "mm"))
2135 else if (!_cups_strcasecmp(ptr
, "cm"))
2140 else if (!_cups_strcasecmp(ptr
, "m"))
2151 pc
->custom_size
.width
= (int)w
;
2152 pc
->custom_size
.length
= (int)l
;
2154 return (&(pc
->custom_size
));
2158 * Not a custom size - look it up...
2161 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2162 if (!_cups_strcasecmp(page_size
, size
->map
.ppd
) ||
2163 !_cups_strcasecmp(page_size
, size
->map
.pwg
))
2167 * Look up standard sizes...
2170 if ((media
= pwgMediaForPPD(page_size
)) == NULL
)
2171 if ((media
= pwgMediaForLegacy(page_size
)) == NULL
)
2172 media
= pwgMediaForPWG(page_size
);
2176 pc
->custom_size
.width
= media
->width
;
2177 pc
->custom_size
.length
= media
->length
;
2179 return (&(pc
->custom_size
));
2187 * '_ppdCacheGetSource()' - Get the PWG media-source associated with a PPD
2191 const char * /* O - PWG media-source keyword */
2193 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2194 const char *input_slot
) /* I - PPD InputSlot */
2196 int i
; /* Looping var */
2197 pwg_map_t
*source
; /* Current source */
2201 * Range check input...
2204 if (!pc
|| !input_slot
)
2207 for (i
= pc
->num_sources
, source
= pc
->sources
; i
> 0; i
--, source
++)
2208 if (!_cups_strcasecmp(input_slot
, source
->ppd
))
2209 return (source
->pwg
);
2216 * '_ppdCacheGetType()' - Get the PWG media-type associated with a PPD
2220 const char * /* O - PWG media-type keyword */
2222 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2223 const char *media_type
) /* I - PPD MediaType */
2225 int i
; /* Looping var */
2226 pwg_map_t
*type
; /* Current type */
2230 * Range check input...
2233 if (!pc
|| !media_type
)
2236 for (i
= pc
->num_types
, type
= pc
->types
; i
> 0; i
--, type
++)
2237 if (!_cups_strcasecmp(media_type
, type
->ppd
))
2245 * '_ppdCacheWriteFile()' - Write PWG mapping data to a file.
2248 int /* O - 1 on success, 0 on failure */
2250 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2251 const char *filename
, /* I - File to write */
2252 ipp_t
*attrs
) /* I - Attributes to write, if any */
2254 int i
, j
, k
; /* Looping vars */
2255 cups_file_t
*fp
; /* Output file */
2256 pwg_size_t
*size
; /* Current size */
2257 pwg_map_t
*map
; /* Current map */
2258 _pwg_finishings_t
*f
; /* Current finishing option */
2259 cups_option_t
*option
; /* Current option */
2260 const char *value
; /* Filter/pre-filter value */
2261 char newfile
[1024]; /* New filename */
2265 * Range check input...
2268 if (!pc
|| !filename
)
2270 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2275 * Open the file and write with compression...
2278 snprintf(newfile
, sizeof(newfile
), "%s.N", filename
);
2279 if ((fp
= cupsFileOpen(newfile
, "w9")) == NULL
)
2281 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
2286 * Standard header...
2289 cupsFilePrintf(fp
, "#CUPS-PPD-CACHE-%d\n", _PPD_CACHE_VERSION
);
2295 if (pc
->num_bins
> 0)
2297 cupsFilePrintf(fp
, "NumBins %d\n", pc
->num_bins
);
2298 for (i
= pc
->num_bins
, map
= pc
->bins
; i
> 0; i
--, map
++)
2299 cupsFilePrintf(fp
, "Bin %s %s\n", map
->pwg
, map
->ppd
);
2306 cupsFilePrintf(fp
, "NumSizes %d\n", pc
->num_sizes
);
2307 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2308 cupsFilePrintf(fp
, "Size %s %s %d %d %d %d %d %d\n", size
->map
.pwg
,
2309 size
->map
.ppd
, size
->width
, size
->length
, size
->left
,
2310 size
->bottom
, size
->right
, size
->top
);
2311 if (pc
->custom_max_width
> 0)
2312 cupsFilePrintf(fp
, "CustomSize %d %d %d %d %d %d %d %d\n",
2313 pc
->custom_max_width
, pc
->custom_max_length
,
2314 pc
->custom_min_width
, pc
->custom_min_length
,
2315 pc
->custom_size
.left
, pc
->custom_size
.bottom
,
2316 pc
->custom_size
.right
, pc
->custom_size
.top
);
2322 if (pc
->source_option
)
2323 cupsFilePrintf(fp
, "SourceOption %s\n", pc
->source_option
);
2325 if (pc
->num_sources
> 0)
2327 cupsFilePrintf(fp
, "NumSources %d\n", pc
->num_sources
);
2328 for (i
= pc
->num_sources
, map
= pc
->sources
; i
> 0; i
--, map
++)
2329 cupsFilePrintf(fp
, "Source %s %s\n", map
->pwg
, map
->ppd
);
2336 if (pc
->num_types
> 0)
2338 cupsFilePrintf(fp
, "NumTypes %d\n", pc
->num_types
);
2339 for (i
= pc
->num_types
, map
= pc
->types
; i
> 0; i
--, map
++)
2340 cupsFilePrintf(fp
, "Type %s %s\n", map
->pwg
, map
->ppd
);
2347 for (i
= _PWG_PRINT_COLOR_MODE_MONOCHROME
; i
< _PWG_PRINT_COLOR_MODE_MAX
; i
++)
2348 for (j
= _PWG_PRINT_QUALITY_DRAFT
; j
< _PWG_PRINT_QUALITY_MAX
; j
++)
2349 if (pc
->num_presets
[i
][j
])
2351 cupsFilePrintf(fp
, "Preset %d %d", i
, j
);
2352 for (k
= pc
->num_presets
[i
][j
], option
= pc
->presets
[i
][j
];
2355 cupsFilePrintf(fp
, " %s=%s", option
->name
, option
->value
);
2356 cupsFilePutChar(fp
, '\n');
2363 if (pc
->sides_option
)
2364 cupsFilePrintf(fp
, "SidesOption %s\n", pc
->sides_option
);
2366 if (pc
->sides_1sided
)
2367 cupsFilePrintf(fp
, "Sides1Sided %s\n", pc
->sides_1sided
);
2369 if (pc
->sides_2sided_long
)
2370 cupsFilePrintf(fp
, "Sides2SidedLong %s\n", pc
->sides_2sided_long
);
2372 if (pc
->sides_2sided_short
)
2373 cupsFilePrintf(fp
, "Sides2SidedShort %s\n", pc
->sides_2sided_short
);
2376 * Product, cupsFilter, cupsFilter2, and cupsPreFilter...
2380 cupsFilePutConf(fp
, "Product", pc
->product
);
2382 for (value
= (const char *)cupsArrayFirst(pc
->filters
);
2384 value
= (const char *)cupsArrayNext(pc
->filters
))
2385 cupsFilePutConf(fp
, "Filter", value
);
2387 for (value
= (const char *)cupsArrayFirst(pc
->prefilters
);
2389 value
= (const char *)cupsArrayNext(pc
->prefilters
))
2390 cupsFilePutConf(fp
, "PreFilter", value
);
2392 cupsFilePrintf(fp
, "SingleFile %s\n", pc
->single_file
? "true" : "false");
2395 * Finishing options...
2398 for (f
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
);
2400 f
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
2402 cupsFilePrintf(fp
, "Finishings %d", f
->value
);
2403 for (i
= f
->num_options
, option
= f
->options
; i
> 0; i
--, option
++)
2404 cupsFilePrintf(fp
, " %s=%s", option
->name
, option
->value
);
2405 cupsFilePutChar(fp
, '\n');
2412 cupsFilePrintf(fp
, "MaxCopies %d\n", pc
->max_copies
);
2415 * Accounting/quota/PIN/managed printing values...
2418 if (pc
->charge_info_uri
)
2419 cupsFilePutConf(fp
, "ChargeInfoURI", pc
->charge_info_uri
);
2421 cupsFilePrintf(fp
, "AccountId %s\n", pc
->account_id
? "true" : "false");
2422 cupsFilePrintf(fp
, "AccountingUserId %s\n",
2423 pc
->accounting_user_id
? "true" : "false");
2426 cupsFilePutConf(fp
, "Password", pc
->password
);
2428 for (value
= (char *)cupsArrayFirst(pc
->mandatory
);
2430 value
= (char *)cupsArrayNext(pc
->mandatory
))
2431 cupsFilePutConf(fp
, "Mandatory", value
);
2437 for (value
= (char *)cupsArrayFirst(pc
->support_files
);
2439 value
= (char *)cupsArrayNext(pc
->support_files
))
2440 cupsFilePutConf(fp
, "SupportFile", value
);
2443 * IPP attributes, if any...
2448 cupsFilePrintf(fp
, "IPP " CUPS_LLFMT
"\n", CUPS_LLCAST
ippLength(attrs
));
2450 attrs
->state
= IPP_STATE_IDLE
;
2451 ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
, attrs
);
2455 * Close and return...
2458 if (cupsFileClose(fp
))
2465 return (!rename(newfile
, filename
));
2470 * '_pwgInputSlotForSource()' - Get the InputSlot name for the given PWG
2474 const char * /* O - InputSlot name */
2475 _pwgInputSlotForSource(
2476 const char *media_source
, /* I - PWG media-source */
2477 char *name
, /* I - Name buffer */
2478 size_t namesize
) /* I - Size of name buffer */
2481 * Range check input...
2484 if (!media_source
|| !name
|| namesize
< PPD_MAX_NAME
)
2487 if (_cups_strcasecmp(media_source
, "main"))
2488 strlcpy(name
, "Cassette", namesize
);
2489 else if (_cups_strcasecmp(media_source
, "alternate"))
2490 strlcpy(name
, "Multipurpose", namesize
);
2491 else if (_cups_strcasecmp(media_source
, "large-capacity"))
2492 strlcpy(name
, "LargeCapacity", namesize
);
2493 else if (_cups_strcasecmp(media_source
, "bottom"))
2494 strlcpy(name
, "Lower", namesize
);
2495 else if (_cups_strcasecmp(media_source
, "middle"))
2496 strlcpy(name
, "Middle", namesize
);
2497 else if (_cups_strcasecmp(media_source
, "top"))
2498 strlcpy(name
, "Upper", namesize
);
2499 else if (_cups_strcasecmp(media_source
, "rear"))
2500 strlcpy(name
, "Rear", namesize
);
2501 else if (_cups_strcasecmp(media_source
, "side"))
2502 strlcpy(name
, "Side", namesize
);
2503 else if (_cups_strcasecmp(media_source
, "envelope"))
2504 strlcpy(name
, "Envelope", namesize
);
2505 else if (_cups_strcasecmp(media_source
, "main-roll"))
2506 strlcpy(name
, "Roll", namesize
);
2507 else if (_cups_strcasecmp(media_source
, "alternate-roll"))
2508 strlcpy(name
, "Roll2", namesize
);
2510 pwg_ppdize_name(media_source
, name
, namesize
);
2517 * '_pwgMediaTypeForType()' - Get the MediaType name for the given PWG
2521 const char * /* O - MediaType name */
2522 _pwgMediaTypeForType(
2523 const char *media_type
, /* I - PWG media-type */
2524 char *name
, /* I - Name buffer */
2525 size_t namesize
) /* I - Size of name buffer */
2528 * Range check input...
2531 if (!media_type
|| !name
|| namesize
< PPD_MAX_NAME
)
2534 if (_cups_strcasecmp(media_type
, "auto"))
2535 strlcpy(name
, "Auto", namesize
);
2536 else if (_cups_strcasecmp(media_type
, "cardstock"))
2537 strlcpy(name
, "Cardstock", namesize
);
2538 else if (_cups_strcasecmp(media_type
, "envelope"))
2539 strlcpy(name
, "Envelope", namesize
);
2540 else if (_cups_strcasecmp(media_type
, "photographic-glossy"))
2541 strlcpy(name
, "Glossy", namesize
);
2542 else if (_cups_strcasecmp(media_type
, "photographic-high-gloss"))
2543 strlcpy(name
, "HighGloss", namesize
);
2544 else if (_cups_strcasecmp(media_type
, "photographic-matte"))
2545 strlcpy(name
, "Matte", namesize
);
2546 else if (_cups_strcasecmp(media_type
, "stationery"))
2547 strlcpy(name
, "Plain", namesize
);
2548 else if (_cups_strcasecmp(media_type
, "stationery-coated"))
2549 strlcpy(name
, "Coated", namesize
);
2550 else if (_cups_strcasecmp(media_type
, "stationery-inkjet"))
2551 strlcpy(name
, "Inkjet", namesize
);
2552 else if (_cups_strcasecmp(media_type
, "stationery-letterhead"))
2553 strlcpy(name
, "Letterhead", namesize
);
2554 else if (_cups_strcasecmp(media_type
, "stationery-preprinted"))
2555 strlcpy(name
, "Preprinted", namesize
);
2556 else if (_cups_strcasecmp(media_type
, "transparency"))
2557 strlcpy(name
, "Transparency", namesize
);
2559 pwg_ppdize_name(media_type
, name
, namesize
);
2566 * '_pwgPageSizeForMedia()' - Get the PageSize name for the given media.
2569 const char * /* O - PageSize name */
2570 _pwgPageSizeForMedia(
2571 pwg_media_t
*media
, /* I - Media */
2572 char *name
, /* I - PageSize name buffer */
2573 size_t namesize
) /* I - Size of name buffer */
2575 const char *sizeptr
, /* Pointer to size in PWG name */
2576 *dimptr
; /* Pointer to dimensions in PWG name */
2580 * Range check input...
2583 if (!media
|| !name
|| namesize
< PPD_MAX_NAME
)
2587 * Copy or generate a PageSize name...
2593 * Use a standard Adobe name...
2596 strlcpy(name
, media
->ppd
, namesize
);
2598 else if (!media
->pwg
|| !strncmp(media
->pwg
, "custom_", 7) ||
2599 (sizeptr
= strchr(media
->pwg
, '_')) == NULL
||
2600 (dimptr
= strchr(sizeptr
+ 1, '_')) == NULL
||
2601 (size_t)(dimptr
- sizeptr
) > namesize
)
2604 * Use a name of the form "wNNNhNNN"...
2607 snprintf(name
, namesize
, "w%dh%d", (int)PWG_TO_POINTS(media
->width
),
2608 (int)PWG_TO_POINTS(media
->length
));
2613 * Copy the size name from class_sizename_dimensions...
2616 memcpy(name
, sizeptr
+ 1, (size_t)(dimptr
- sizeptr
- 1));
2617 name
[dimptr
- sizeptr
- 1] = '\0';
2625 * 'pwg_compare_finishings()' - Compare two finishings values.
2628 static int /* O- Result of comparison */
2629 pwg_compare_finishings(
2630 _pwg_finishings_t
*a
, /* I - First finishings value */
2631 _pwg_finishings_t
*b
) /* I - Second finishings value */
2633 return ((int)b
->value
- (int)a
->value
);
2638 * 'pwg_free_finishings()' - Free a finishings value.
2642 pwg_free_finishings(
2643 _pwg_finishings_t
*f
) /* I - Finishings value */
2645 cupsFreeOptions(f
->num_options
, f
->options
);
2651 * 'pwg_ppdize_name()' - Convert an IPP keyword to a PPD keyword.
2655 pwg_ppdize_name(const char *ipp
, /* I - IPP keyword */
2656 char *name
, /* I - Name buffer */
2657 size_t namesize
) /* I - Size of name buffer */
2659 char *ptr
, /* Pointer into name buffer */
2660 *end
; /* End of name buffer */
2663 *name
= (char)toupper(*ipp
++);
2665 for (ptr
= name
+ 1, end
= name
+ namesize
- 1; *ipp
&& ptr
< end
;)
2667 if (*ipp
== '-' && _cups_isalpha(ipp
[1]))
2670 *ptr
++ = (char)toupper(*ipp
++ & 255);
2681 * 'pwg_unppdize_name()' - Convert a PPD keyword to a lowercase IPP keyword.
2685 pwg_unppdize_name(const char *ppd
, /* I - PPD keyword */
2686 char *name
, /* I - Name buffer */
2687 size_t namesize
, /* I - Size of name buffer */
2688 const char *dashchars
)/* I - Characters to be replaced by dashes */
2690 char *ptr
, /* Pointer into name buffer */
2691 *end
; /* End of name buffer */
2694 if (_cups_islower(*ppd
))
2697 * Already lowercase name, use as-is?
2700 const char *ppdptr
; /* Pointer into PPD keyword */
2702 for (ppdptr
= ppd
+ 1; *ppdptr
; ppdptr
++)
2703 if (_cups_isupper(*ppdptr
) || strchr(dashchars
, *ppdptr
))
2708 strlcpy(name
, ppd
, namesize
);
2713 for (ptr
= name
, end
= name
+ namesize
- 1; *ppd
&& ptr
< end
; ppd
++)
2715 if (_cups_isalnum(*ppd
) || *ppd
== '-')
2716 *ptr
++ = (char)tolower(*ppd
& 255);
2717 else if (strchr(dashchars
, *ppd
))
2722 if (!_cups_isupper(*ppd
) && _cups_isalnum(*ppd
) &&
2723 _cups_isupper(ppd
[1]) && ptr
< end
)
2725 else if (!isdigit(*ppd
& 255) && isdigit(ppd
[1] & 255))