]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/ppd-cache.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / cups / ppd-cache.c
1 /*
2 * "$Id$"
3 *
4 * PPD cache implementation for CUPS.
5 *
6 * Copyright 2010-2014 by Apple Inc.
7 *
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/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 */
16
17 /*
18 * Include necessary headers...
19 */
20
21 #include "cups-private.h"
22 #include <math.h>
23
24
25 /*
26 * Macro to test for two almost-equal PWG measurements.
27 */
28
29 #define _PWG_EQUIVALENT(x, y) (abs((x)-(y)) < 2)
30
31
32 /*
33 * Local functions...
34 */
35
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);
42
43
44 /*
45 * '_ppdCacheCreateWithFile()' - Create PPD cache and mapping data from a
46 * written file.
47 *
48 * Use the @link _ppdCacheWriteFile@ function to write PWG mapping data to a
49 * file.
50 */
51
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 */
56 {
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];
72 /* PPD keyword */
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 */
76
77
78 DEBUG_printf(("_ppdCacheCreateWithFile(filename=\"%s\")", filename));
79
80 /*
81 * Range check input...
82 */
83
84 if (attrs)
85 *attrs = NULL;
86
87 if (!filename)
88 {
89 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
90 return (NULL);
91 }
92
93 /*
94 * Open the file...
95 */
96
97 if ((fp = cupsFileOpen(filename, "r")) == NULL)
98 {
99 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
100 return (NULL);
101 }
102
103 /*
104 * Read the first line and make sure it has "#CUPS-PPD-CACHE-version" in it...
105 */
106
107 if (!cupsFileGets(fp, line, sizeof(line)))
108 {
109 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
110 DEBUG_puts("_ppdCacheCreateWithFile: Unable to read first line.");
111 cupsFileClose(fp);
112 return (NULL);
113 }
114
115 if (strncmp(line, "#CUPS-PPD-CACHE-", 16))
116 {
117 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
118 DEBUG_printf(("_ppdCacheCreateWithFile: Wrong first line \"%s\".", line));
119 cupsFileClose(fp);
120 return (NULL);
121 }
122
123 if (atoi(line + 16) != _PPD_CACHE_VERSION)
124 {
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));
128 cupsFileClose(fp);
129 return (NULL);
130 }
131
132 /*
133 * Allocate the mapping data structure...
134 */
135
136 if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL)
137 {
138 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
139 DEBUG_puts("_ppdCacheCreateWithFile: Unable to allocate _ppd_cache_t.");
140 goto create_error;
141 }
142
143 pc->max_copies = 9999;
144
145 /*
146 * Read the file...
147 */
148
149 linenum = 0;
150 num_bins = 0;
151 num_sizes = 0;
152 num_sources = 0;
153 num_types = 0;
154
155 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
156 {
157 DEBUG_printf(("_ppdCacheCreateWithFile: line=\"%s\", value=\"%s\", "
158 "linenum=%d", line, value, linenum));
159
160 if (!value)
161 {
162 DEBUG_printf(("_ppdCacheCreateWithFile: Missing value on line %d.",
163 linenum));
164 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
165 goto create_error;
166 }
167 else if (!_cups_strcasecmp(line, "Filter"))
168 {
169 if (!pc->filters)
170 pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0,
171 (cups_acopy_func_t)_cupsStrAlloc,
172 (cups_afree_func_t)_cupsStrFree);
173
174 cupsArrayAdd(pc->filters, value);
175 }
176 else if (!_cups_strcasecmp(line, "PreFilter"))
177 {
178 if (!pc->prefilters)
179 pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0,
180 (cups_acopy_func_t)_cupsStrAlloc,
181 (cups_afree_func_t)_cupsStrFree);
182
183 cupsArrayAdd(pc->prefilters, value);
184 }
185 else if (!_cups_strcasecmp(line, "Product"))
186 {
187 pc->product = _cupsStrAlloc(value);
188 }
189 else if (!_cups_strcasecmp(line, "SingleFile"))
190 {
191 pc->single_file = !_cups_strcasecmp(value, "true");
192 }
193 else if (!_cups_strcasecmp(line, "IPP"))
194 {
195 off_t pos = cupsFileTell(fp), /* Position in file */
196 length = strtol(value, NULL, 10);
197 /* Length of IPP attributes */
198
199 if (attrs && *attrs)
200 {
201 DEBUG_puts("_ppdCacheCreateWithFile: IPP listed multiple times.");
202 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
203 goto create_error;
204 }
205 else if (length <= 0)
206 {
207 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP length.");
208 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
209 goto create_error;
210 }
211
212 if (attrs)
213 {
214 /*
215 * Read IPP attributes into the provided variable...
216 */
217
218 *attrs = ippNew();
219
220 if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL,
221 *attrs) != IPP_STATE_DATA)
222 {
223 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
224 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
225 goto create_error;
226 }
227 }
228 else
229 {
230 /*
231 * Skip the IPP data entirely...
232 */
233
234 cupsFileSeek(fp, pos + length);
235 }
236
237 if (cupsFileTell(fp) != (pos + length))
238 {
239 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
240 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
241 goto create_error;
242 }
243 }
244 else if (!_cups_strcasecmp(line, "NumBins"))
245 {
246 if (num_bins > 0)
247 {
248 DEBUG_puts("_ppdCacheCreateWithFile: NumBins listed multiple times.");
249 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
250 goto create_error;
251 }
252
253 if ((num_bins = atoi(value)) <= 0 || num_bins > 65536)
254 {
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);
258 goto create_error;
259 }
260
261 if ((pc->bins = calloc((size_t)num_bins, sizeof(pwg_map_t))) == NULL)
262 {
263 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d bins.",
264 num_sizes));
265 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
266 goto create_error;
267 }
268 }
269 else if (!_cups_strcasecmp(line, "Bin"))
270 {
271 if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
272 {
273 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Bin on line %d.", linenum));
274 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
275 goto create_error;
276 }
277
278 if (pc->num_bins >= num_bins)
279 {
280 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Bin's on line %d.",
281 linenum));
282 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
283 goto create_error;
284 }
285
286 map = pc->bins + pc->num_bins;
287 map->pwg = _cupsStrAlloc(pwg_keyword);
288 map->ppd = _cupsStrAlloc(ppd_keyword);
289
290 pc->num_bins ++;
291 }
292 else if (!_cups_strcasecmp(line, "NumSizes"))
293 {
294 if (num_sizes > 0)
295 {
296 DEBUG_puts("_ppdCacheCreateWithFile: NumSizes listed multiple times.");
297 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
298 goto create_error;
299 }
300
301 if ((num_sizes = atoi(value)) < 0 || num_sizes > 65536)
302 {
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);
306 goto create_error;
307 }
308
309 if (num_sizes > 0)
310 {
311 if ((pc->sizes = calloc((size_t)num_sizes, sizeof(pwg_size_t))) == NULL)
312 {
313 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sizes.",
314 num_sizes));
315 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
316 goto create_error;
317 }
318 }
319 }
320 else if (!_cups_strcasecmp(line, "Size"))
321 {
322 if (pc->num_sizes >= num_sizes)
323 {
324 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Size's on line %d.",
325 linenum));
326 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
327 goto create_error;
328 }
329
330 size = pc->sizes + pc->num_sizes;
331
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)
335 {
336 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Size on line %d.",
337 linenum));
338 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
339 goto create_error;
340 }
341
342 size->map.pwg = _cupsStrAlloc(pwg_keyword);
343 size->map.ppd = _cupsStrAlloc(ppd_keyword);
344
345 pc->num_sizes ++;
346 }
347 else if (!_cups_strcasecmp(line, "CustomSize"))
348 {
349 if (pc->custom_max_width > 0)
350 {
351 DEBUG_printf(("_ppdCacheCreateWithFile: Too many CustomSize's on line "
352 "%d.", linenum));
353 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
354 goto create_error;
355 }
356
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)
362 {
363 DEBUG_printf(("_ppdCacheCreateWithFile: Bad CustomSize on line %d.",
364 linenum));
365 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
366 goto create_error;
367 }
368
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);
372
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);
376 }
377 else if (!_cups_strcasecmp(line, "SourceOption"))
378 {
379 pc->source_option = _cupsStrAlloc(value);
380 }
381 else if (!_cups_strcasecmp(line, "NumSources"))
382 {
383 if (num_sources > 0)
384 {
385 DEBUG_puts("_ppdCacheCreateWithFile: NumSources listed multiple "
386 "times.");
387 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
388 goto create_error;
389 }
390
391 if ((num_sources = atoi(value)) <= 0 || num_sources > 65536)
392 {
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);
396 goto create_error;
397 }
398
399 if ((pc->sources = calloc((size_t)num_sources, sizeof(pwg_map_t))) == NULL)
400 {
401 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sources.",
402 num_sources));
403 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
404 goto create_error;
405 }
406 }
407 else if (!_cups_strcasecmp(line, "Source"))
408 {
409 if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
410 {
411 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Source on line %d.",
412 linenum));
413 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
414 goto create_error;
415 }
416
417 if (pc->num_sources >= num_sources)
418 {
419 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Source's on line %d.",
420 linenum));
421 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
422 goto create_error;
423 }
424
425 map = pc->sources + pc->num_sources;
426 map->pwg = _cupsStrAlloc(pwg_keyword);
427 map->ppd = _cupsStrAlloc(ppd_keyword);
428
429 pc->num_sources ++;
430 }
431 else if (!_cups_strcasecmp(line, "NumTypes"))
432 {
433 if (num_types > 0)
434 {
435 DEBUG_puts("_ppdCacheCreateWithFile: NumTypes listed multiple times.");
436 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
437 goto create_error;
438 }
439
440 if ((num_types = atoi(value)) <= 0 || num_types > 65536)
441 {
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);
445 goto create_error;
446 }
447
448 if ((pc->types = calloc((size_t)num_types, sizeof(pwg_map_t))) == NULL)
449 {
450 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d types.",
451 num_types));
452 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
453 goto create_error;
454 }
455 }
456 else if (!_cups_strcasecmp(line, "Type"))
457 {
458 if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
459 {
460 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Type on line %d.",
461 linenum));
462 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
463 goto create_error;
464 }
465
466 if (pc->num_types >= num_types)
467 {
468 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Type's on line %d.",
469 linenum));
470 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
471 goto create_error;
472 }
473
474 map = pc->types + pc->num_types;
475 map->pwg = _cupsStrAlloc(pwg_keyword);
476 map->ppd = _cupsStrAlloc(ppd_keyword);
477
478 pc->num_types ++;
479 }
480 else if (!_cups_strcasecmp(line, "Preset"))
481 {
482 /*
483 * Preset output-mode print-quality name=value ...
484 */
485
486 print_color_mode = (_pwg_print_color_mode_t)strtol(value, &valueptr, 10);
487 print_quality = (_pwg_print_quality_t)strtol(valueptr, &valueptr, 10);
488
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)
494 {
495 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Preset on line %d.",
496 linenum));
497 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
498 goto create_error;
499 }
500
501 pc->num_presets[print_color_mode][print_quality] =
502 cupsParseOptions(valueptr, 0,
503 pc->presets[print_color_mode] + print_quality);
504 }
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"))
514 {
515 if (!pc->finishings)
516 pc->finishings =
517 cupsArrayNew3((cups_array_func_t)pwg_compare_finishings,
518 NULL, NULL, 0, NULL,
519 (cups_afree_func_t)pwg_free_finishings);
520
521 if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL)
522 goto create_error;
523
524 finishings->value = (ipp_finishings_t)strtol(value, &valueptr, 10);
525 finishings->num_options = cupsParseOptions(valueptr, 0,
526 &(finishings->options));
527
528 cupsArrayAdd(pc->finishings, finishings);
529 }
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"))
541 {
542 if (pc->mandatory)
543 _cupsArrayAddStrings(pc->mandatory, value, ' ');
544 else
545 pc->mandatory = _cupsArrayNewStrings(value, ' ');
546 }
547 else if (!_cups_strcasecmp(line, "SupportFile"))
548 {
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);
553
554 cupsArrayAdd(pc->support_files, value);
555 }
556 else
557 {
558 DEBUG_printf(("_ppdCacheCreateWithFile: Unknown %s on line %d.", line,
559 linenum));
560 }
561 }
562
563 if (pc->num_sizes < num_sizes)
564 {
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);
568 goto create_error;
569 }
570
571 if (pc->num_sources < num_sources)
572 {
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);
576 goto create_error;
577 }
578
579 if (pc->num_types < num_types)
580 {
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);
584 goto create_error;
585 }
586
587 cupsFileClose(fp);
588
589 return (pc);
590
591 /*
592 * If we get here the file was bad - free any data and return...
593 */
594
595 create_error:
596
597 cupsFileClose(fp);
598 _ppdCacheDestroy(pc);
599
600 if (attrs)
601 {
602 ippDelete(*attrs);
603 *attrs = NULL;
604 }
605
606 return (NULL);
607 }
608
609
610 /*
611 * '_ppdCacheCreateWithPPD()' - Create PWG mapping data from a PPD file.
612 */
613
614 _ppd_cache_t * /* O - PPD cache and mapping data */
615 _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */
616 {
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 */
658
659
660 DEBUG_printf(("_ppdCacheCreateWithPPD(ppd=%p)", ppd));
661
662 /*
663 * Range check input...
664 */
665
666 if (!ppd)
667 return (NULL);
668
669 /*
670 * Allocate memory...
671 */
672
673 if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL)
674 {
675 DEBUG_puts("_ppdCacheCreateWithPPD: Unable to allocate _ppd_cache_t.");
676 goto create_error;
677 }
678
679 /*
680 * Copy and convert size data...
681 */
682
683 if (ppd->num_sizes > 0)
684 {
685 if ((pc->sizes = calloc((size_t)ppd->num_sizes, sizeof(pwg_size_t))) == NULL)
686 {
687 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
688 "pwg_size_t's.", ppd->num_sizes));
689 goto create_error;
690 }
691
692 for (i = ppd->num_sizes, pwg_size = pc->sizes, ppd_size = ppd->sizes;
693 i > 0;
694 i --, ppd_size ++)
695 {
696 /*
697 * Don't copy over custom size...
698 */
699
700 if (!_cups_strcasecmp(ppd_size->name, "Custom"))
701 continue;
702
703 /*
704 * Convert the PPD size name to the corresponding PWG keyword name.
705 */
706
707 if ((pwg_media = pwgMediaForPPD(ppd_size->name)) != NULL)
708 {
709 /*
710 * Standard name, do we have conflicts?
711 */
712
713 for (j = 0; j < pc->num_sizes; j ++)
714 if (!strcmp(pc->sizes[j].map.pwg, pwg_media->pwg))
715 {
716 pwg_media = NULL;
717 break;
718 }
719 }
720
721 if (pwg_media)
722 {
723 /*
724 * Standard name and no conflicts, use it!
725 */
726
727 pwg_name = pwg_media->pwg;
728 new_known_pwg = 1;
729 }
730 else
731 {
732 /*
733 * Not a standard name; convert it to a PWG vendor name of the form:
734 *
735 * pp_lowerppd_WIDTHxHEIGHTuu
736 */
737
738 pwg_name = pwg_keyword;
739 new_known_pwg = 0;
740
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);
745 }
746
747 /*
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...
751 */
752
753 if ((pwg_media = pwgMediaForSize(PWG_FROM_POINTS(ppd_size->width),
754 PWG_FROM_POINTS(ppd_size->length))) == NULL)
755 continue;
756
757 new_width = pwg_media->width;
758 new_length = pwg_media->length;
759 new_left = PWG_FROM_POINTS(ppd_size->left);
760 new_bottom = PWG_FROM_POINTS(ppd_size->bottom);
761 new_right = PWG_FROM_POINTS(ppd_size->width - ppd_size->right);
762 new_top = PWG_FROM_POINTS(ppd_size->length - ppd_size->top);
763 new_imageable = new_length - new_top - new_bottom;
764 new_borderless = new_bottom == 0 && new_top == 0 &&
765 new_left == 0 && new_right == 0;
766
767 for (k = pc->num_sizes, similar = 0, old_size = pc->sizes, new_size = NULL;
768 k > 0 && !similar;
769 k --, old_size ++)
770 {
771 old_imageable = old_size->length - old_size->top - old_size->bottom;
772 old_borderless = old_size->left == 0 && old_size->bottom == 0 &&
773 old_size->right == 0 && old_size->top == 0;
774 old_known_pwg = strncmp(old_size->map.pwg, "oe_", 3) &&
775 strncmp(old_size->map.pwg, "om_", 3);
776
777 similar = old_borderless == new_borderless &&
778 _PWG_EQUIVALENT(old_size->width, new_width) &&
779 _PWG_EQUIVALENT(old_size->length, new_length);
780
781 if (similar &&
782 (new_known_pwg || (!old_known_pwg && new_imageable > old_imageable)))
783 {
784 /*
785 * The new paper has a larger imageable area so it could replace
786 * the older paper. Regardless of the imageable area, we always
787 * prefer the size with a well-known PWG name.
788 */
789
790 new_size = old_size;
791 _cupsStrFree(old_size->map.ppd);
792 _cupsStrFree(old_size->map.pwg);
793 }
794 }
795
796 if (!similar)
797 {
798 /*
799 * The paper was unique enough to deserve its own entry so add it to the
800 * end.
801 */
802
803 new_size = pwg_size ++;
804 pc->num_sizes ++;
805 }
806
807 if (new_size)
808 {
809 /*
810 * Save this size...
811 */
812
813 new_size->map.ppd = _cupsStrAlloc(ppd_size->name);
814 new_size->map.pwg = _cupsStrAlloc(pwg_name);
815 new_size->width = new_width;
816 new_size->length = new_length;
817 new_size->left = new_left;
818 new_size->bottom = new_bottom;
819 new_size->right = new_right;
820 new_size->top = new_top;
821 }
822 }
823 }
824
825 if (ppd->variable_sizes)
826 {
827 /*
828 * Generate custom size data...
829 */
830
831 pwgFormatSizeName(pwg_keyword, sizeof(pwg_keyword), "custom", "max",
832 PWG_FROM_POINTS(ppd->custom_max[0]),
833 PWG_FROM_POINTS(ppd->custom_max[1]), NULL);
834 pc->custom_max_keyword = _cupsStrAlloc(pwg_keyword);
835 pc->custom_max_width = PWG_FROM_POINTS(ppd->custom_max[0]);
836 pc->custom_max_length = PWG_FROM_POINTS(ppd->custom_max[1]);
837
838 pwgFormatSizeName(pwg_keyword, sizeof(pwg_keyword), "custom", "min",
839 PWG_FROM_POINTS(ppd->custom_min[0]),
840 PWG_FROM_POINTS(ppd->custom_min[1]), NULL);
841 pc->custom_min_keyword = _cupsStrAlloc(pwg_keyword);
842 pc->custom_min_width = PWG_FROM_POINTS(ppd->custom_min[0]);
843 pc->custom_min_length = PWG_FROM_POINTS(ppd->custom_min[1]);
844
845 pc->custom_size.left = PWG_FROM_POINTS(ppd->custom_margins[0]);
846 pc->custom_size.bottom = PWG_FROM_POINTS(ppd->custom_margins[1]);
847 pc->custom_size.right = PWG_FROM_POINTS(ppd->custom_margins[2]);
848 pc->custom_size.top = PWG_FROM_POINTS(ppd->custom_margins[3]);
849 }
850
851 /*
852 * Copy and convert InputSlot data...
853 */
854
855 if ((input_slot = ppdFindOption(ppd, "InputSlot")) == NULL)
856 input_slot = ppdFindOption(ppd, "HPPaperSource");
857
858 if (input_slot)
859 {
860 pc->source_option = _cupsStrAlloc(input_slot->keyword);
861
862 if ((pc->sources = calloc((size_t)input_slot->num_choices, sizeof(pwg_map_t))) == NULL)
863 {
864 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
865 "pwg_map_t's for InputSlot.", input_slot->num_choices));
866 goto create_error;
867 }
868
869 pc->num_sources = input_slot->num_choices;
870
871 for (i = input_slot->num_choices, choice = input_slot->choices,
872 map = pc->sources;
873 i > 0;
874 i --, choice ++, map ++)
875 {
876 if (!_cups_strncasecmp(choice->choice, "Auto", 4) ||
877 !_cups_strcasecmp(choice->choice, "Default"))
878 pwg_name = "auto";
879 else if (!_cups_strcasecmp(choice->choice, "Cassette"))
880 pwg_name = "main";
881 else if (!_cups_strcasecmp(choice->choice, "PhotoTray"))
882 pwg_name = "photo";
883 else if (!_cups_strcasecmp(choice->choice, "CDTray"))
884 pwg_name = "disc";
885 else if (!_cups_strncasecmp(choice->choice, "Multipurpose", 12) ||
886 !_cups_strcasecmp(choice->choice, "MP") ||
887 !_cups_strcasecmp(choice->choice, "MPTray"))
888 pwg_name = "by-pass-tray";
889 else if (!_cups_strcasecmp(choice->choice, "LargeCapacity"))
890 pwg_name = "large-capacity";
891 else if (!_cups_strncasecmp(choice->choice, "Lower", 5))
892 pwg_name = "bottom";
893 else if (!_cups_strncasecmp(choice->choice, "Middle", 6))
894 pwg_name = "middle";
895 else if (!_cups_strncasecmp(choice->choice, "Upper", 5))
896 pwg_name = "top";
897 else if (!_cups_strncasecmp(choice->choice, "Side", 4))
898 pwg_name = "side";
899 else if (!_cups_strcasecmp(choice->choice, "Roll"))
900 pwg_name = "main-roll";
901 else
902 {
903 /*
904 * Convert PPD name to lowercase...
905 */
906
907 pwg_name = pwg_keyword;
908 pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword),
909 "_");
910 }
911
912 map->pwg = _cupsStrAlloc(pwg_name);
913 map->ppd = _cupsStrAlloc(choice->choice);
914 }
915 }
916
917 /*
918 * Copy and convert MediaType data...
919 */
920
921 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
922 {
923 if ((pc->types = calloc((size_t)media_type->num_choices, sizeof(pwg_map_t))) == NULL)
924 {
925 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
926 "pwg_map_t's for MediaType.", media_type->num_choices));
927 goto create_error;
928 }
929
930 pc->num_types = media_type->num_choices;
931
932 for (i = media_type->num_choices, choice = media_type->choices,
933 map = pc->types;
934 i > 0;
935 i --, choice ++, map ++)
936 {
937 if (!_cups_strncasecmp(choice->choice, "Auto", 4) ||
938 !_cups_strcasecmp(choice->choice, "Any") ||
939 !_cups_strcasecmp(choice->choice, "Default"))
940 pwg_name = "auto";
941 else if (!_cups_strncasecmp(choice->choice, "Card", 4))
942 pwg_name = "cardstock";
943 else if (!_cups_strncasecmp(choice->choice, "Env", 3))
944 pwg_name = "envelope";
945 else if (!_cups_strncasecmp(choice->choice, "Gloss", 5))
946 pwg_name = "photographic-glossy";
947 else if (!_cups_strcasecmp(choice->choice, "HighGloss"))
948 pwg_name = "photographic-high-gloss";
949 else if (!_cups_strcasecmp(choice->choice, "Matte"))
950 pwg_name = "photographic-matte";
951 else if (!_cups_strncasecmp(choice->choice, "Plain", 5))
952 pwg_name = "stationery";
953 else if (!_cups_strncasecmp(choice->choice, "Coated", 6))
954 pwg_name = "stationery-coated";
955 else if (!_cups_strcasecmp(choice->choice, "Inkjet"))
956 pwg_name = "stationery-inkjet";
957 else if (!_cups_strcasecmp(choice->choice, "Letterhead"))
958 pwg_name = "stationery-letterhead";
959 else if (!_cups_strncasecmp(choice->choice, "Preprint", 8))
960 pwg_name = "stationery-preprinted";
961 else if (!_cups_strcasecmp(choice->choice, "Recycled"))
962 pwg_name = "stationery-recycled";
963 else if (!_cups_strncasecmp(choice->choice, "Transparen", 10))
964 pwg_name = "transparency";
965 else
966 {
967 /*
968 * Convert PPD name to lowercase...
969 */
970
971 pwg_name = pwg_keyword;
972 pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword),
973 "_");
974 }
975
976 map->pwg = _cupsStrAlloc(pwg_name);
977 map->ppd = _cupsStrAlloc(choice->choice);
978 }
979 }
980
981 /*
982 * Copy and convert OutputBin data...
983 */
984
985 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
986 {
987 if ((pc->bins = calloc((size_t)output_bin->num_choices, sizeof(pwg_map_t))) == NULL)
988 {
989 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
990 "pwg_map_t's for OutputBin.", output_bin->num_choices));
991 goto create_error;
992 }
993
994 pc->num_bins = output_bin->num_choices;
995
996 for (i = output_bin->num_choices, choice = output_bin->choices,
997 map = pc->bins;
998 i > 0;
999 i --, choice ++, map ++)
1000 {
1001 pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword), "_");
1002
1003 map->pwg = _cupsStrAlloc(pwg_keyword);
1004 map->ppd = _cupsStrAlloc(choice->choice);
1005 }
1006 }
1007
1008 if ((ppd_attr = ppdFindAttr(ppd, "APPrinterPreset", NULL)) != NULL)
1009 {
1010 /*
1011 * Copy and convert APPrinterPreset (output-mode + print-quality) data...
1012 */
1013
1014 const char *quality, /* com.apple.print.preset.quality value */
1015 *output_mode, /* com.apple.print.preset.output-mode value */
1016 *color_model_val, /* ColorModel choice */
1017 *graphicsType, /* com.apple.print.preset.graphicsType value */
1018 *media_front_coating; /* com.apple.print.preset.media-front-coating value */
1019
1020 do
1021 {
1022 num_options = _ppdParseOptions(ppd_attr->value, 0, &options,
1023 _PPD_PARSE_ALL);
1024
1025 if ((quality = cupsGetOption("com.apple.print.preset.quality",
1026 num_options, options)) != NULL)
1027 {
1028 /*
1029 * Get the print-quality for this preset...
1030 */
1031
1032 if (!strcmp(quality, "low"))
1033 pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT;
1034 else if (!strcmp(quality, "high"))
1035 pwg_print_quality = _PWG_PRINT_QUALITY_HIGH;
1036 else
1037 pwg_print_quality = _PWG_PRINT_QUALITY_NORMAL;
1038
1039 /*
1040 * Ignore graphicsType "Photo" presets that are not high quality.
1041 */
1042
1043 graphicsType = cupsGetOption("com.apple.print.preset.graphicsType",
1044 num_options, options);
1045
1046 if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH && graphicsType &&
1047 !strcmp(graphicsType, "Photo"))
1048 continue;
1049
1050 /*
1051 * Ignore presets for normal and draft quality where the coating
1052 * isn't "none" or "autodetect".
1053 */
1054
1055 media_front_coating = cupsGetOption(
1056 "com.apple.print.preset.media-front-coating",
1057 num_options, options);
1058
1059 if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH &&
1060 media_front_coating &&
1061 strcmp(media_front_coating, "none") &&
1062 strcmp(media_front_coating, "autodetect"))
1063 continue;
1064
1065 /*
1066 * Get the output mode for this preset...
1067 */
1068
1069 output_mode = cupsGetOption("com.apple.print.preset.output-mode",
1070 num_options, options);
1071 color_model_val = cupsGetOption("ColorModel", num_options, options);
1072
1073 if (output_mode)
1074 {
1075 if (!strcmp(output_mode, "monochrome"))
1076 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
1077 else
1078 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
1079 }
1080 else if (color_model_val)
1081 {
1082 if (!_cups_strcasecmp(color_model_val, "Gray"))
1083 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
1084 else
1085 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
1086 }
1087 else
1088 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
1089
1090 /*
1091 * Save the options for this combination as needed...
1092 */
1093
1094 if (!pc->num_presets[pwg_print_color_mode][pwg_print_quality])
1095 pc->num_presets[pwg_print_color_mode][pwg_print_quality] =
1096 _ppdParseOptions(ppd_attr->value, 0,
1097 pc->presets[pwg_print_color_mode] +
1098 pwg_print_quality, _PPD_PARSE_OPTIONS);
1099 }
1100
1101 cupsFreeOptions(num_options, options);
1102 }
1103 while ((ppd_attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL)) != NULL);
1104 }
1105
1106 if (!pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_DRAFT] &&
1107 !pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_NORMAL] &&
1108 !pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_HIGH])
1109 {
1110 /*
1111 * Try adding some common color options to create grayscale presets. These
1112 * are listed in order of popularity...
1113 */
1114
1115 const char *color_option = NULL, /* Color control option */
1116 *gray_choice = NULL; /* Choice to select grayscale */
1117
1118 if ((color_model = ppdFindOption(ppd, "ColorModel")) != NULL &&
1119 ppdFindChoice(color_model, "Gray"))
1120 {
1121 color_option = "ColorModel";
1122 gray_choice = "Gray";
1123 }
1124 else if ((color_model = ppdFindOption(ppd, "HPColorMode")) != NULL &&
1125 ppdFindChoice(color_model, "grayscale"))
1126 {
1127 color_option = "HPColorMode";
1128 gray_choice = "grayscale";
1129 }
1130 else if ((color_model = ppdFindOption(ppd, "BRMonoColor")) != NULL &&
1131 ppdFindChoice(color_model, "Mono"))
1132 {
1133 color_option = "BRMonoColor";
1134 gray_choice = "Mono";
1135 }
1136 else if ((color_model = ppdFindOption(ppd, "CNIJSGrayScale")) != NULL &&
1137 ppdFindChoice(color_model, "1"))
1138 {
1139 color_option = "CNIJSGrayScale";
1140 gray_choice = "1";
1141 }
1142 else if ((color_model = ppdFindOption(ppd, "HPColorAsGray")) != NULL &&
1143 ppdFindChoice(color_model, "True"))
1144 {
1145 color_option = "HPColorAsGray";
1146 gray_choice = "True";
1147 }
1148
1149 if (color_option && gray_choice)
1150 {
1151 /*
1152 * Copy and convert ColorModel (output-mode) data...
1153 */
1154
1155 cups_option_t *coption, /* Color option */
1156 *moption; /* Monochrome option */
1157
1158 for (pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT;
1159 pwg_print_quality < _PWG_PRINT_QUALITY_MAX;
1160 pwg_print_quality ++)
1161 {
1162 if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][pwg_print_quality])
1163 {
1164 /*
1165 * Copy the color options...
1166 */
1167
1168 num_options = pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR]
1169 [pwg_print_quality];
1170 options = calloc(sizeof(cups_option_t), (size_t)num_options);
1171
1172 if (options)
1173 {
1174 for (i = num_options, moption = options,
1175 coption = pc->presets[_PWG_PRINT_COLOR_MODE_COLOR]
1176 [pwg_print_quality];
1177 i > 0;
1178 i --, moption ++, coption ++)
1179 {
1180 moption->name = _cupsStrRetain(coption->name);
1181 moption->value = _cupsStrRetain(coption->value);
1182 }
1183
1184 pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
1185 num_options;
1186 pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
1187 options;
1188 }
1189 }
1190 else if (pwg_print_quality != _PWG_PRINT_QUALITY_NORMAL)
1191 continue;
1192
1193 /*
1194 * Add the grayscale option to the preset...
1195 */
1196
1197 pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
1198 cupsAddOption(color_option, gray_choice,
1199 pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME]
1200 [pwg_print_quality],
1201 pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME] +
1202 pwg_print_quality);
1203 }
1204 }
1205 }
1206
1207 /*
1208 * Copy and convert Duplex (sides) data...
1209 */
1210
1211 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
1212 if ((duplex = ppdFindOption(ppd, "JCLDuplex")) == NULL)
1213 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
1214 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
1215 duplex = ppdFindOption(ppd, "KD03Duplex");
1216
1217 if (duplex)
1218 {
1219 pc->sides_option = _cupsStrAlloc(duplex->keyword);
1220
1221 for (i = duplex->num_choices, choice = duplex->choices;
1222 i > 0;
1223 i --, choice ++)
1224 {
1225 if ((!_cups_strcasecmp(choice->choice, "None") ||
1226 !_cups_strcasecmp(choice->choice, "False")) && !pc->sides_1sided)
1227 pc->sides_1sided = _cupsStrAlloc(choice->choice);
1228 else if ((!_cups_strcasecmp(choice->choice, "DuplexNoTumble") ||
1229 !_cups_strcasecmp(choice->choice, "LongEdge") ||
1230 !_cups_strcasecmp(choice->choice, "Top")) && !pc->sides_2sided_long)
1231 pc->sides_2sided_long = _cupsStrAlloc(choice->choice);
1232 else if ((!_cups_strcasecmp(choice->choice, "DuplexTumble") ||
1233 !_cups_strcasecmp(choice->choice, "ShortEdge") ||
1234 !_cups_strcasecmp(choice->choice, "Bottom")) &&
1235 !pc->sides_2sided_short)
1236 pc->sides_2sided_short = _cupsStrAlloc(choice->choice);
1237 }
1238 }
1239
1240 /*
1241 * Copy filters and pre-filters...
1242 */
1243
1244 pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0,
1245 (cups_acopy_func_t)_cupsStrAlloc,
1246 (cups_afree_func_t)_cupsStrFree);
1247
1248 cupsArrayAdd(pc->filters,
1249 "application/vnd.cups-raw application/octet-stream 0 -");
1250
1251 if ((ppd_attr = ppdFindAttr(ppd, "cupsFilter2", NULL)) != NULL)
1252 {
1253 do
1254 {
1255 cupsArrayAdd(pc->filters, ppd_attr->value);
1256 }
1257 while ((ppd_attr = ppdFindNextAttr(ppd, "cupsFilter2", NULL)) != NULL);
1258 }
1259 else if (ppd->num_filters > 0)
1260 {
1261 for (i = 0; i < ppd->num_filters; i ++)
1262 cupsArrayAdd(pc->filters, ppd->filters[i]);
1263 }
1264 else
1265 cupsArrayAdd(pc->filters, "application/vnd.cups-postscript 0 -");
1266
1267 /*
1268 * See if we have a command filter...
1269 */
1270
1271 for (filter = (const char *)cupsArrayFirst(pc->filters);
1272 filter;
1273 filter = (const char *)cupsArrayNext(pc->filters))
1274 if (!_cups_strncasecmp(filter, "application/vnd.cups-command", 28) &&
1275 _cups_isspace(filter[28]))
1276 break;
1277
1278 if (!filter &&
1279 ((ppd_attr = ppdFindAttr(ppd, "cupsCommands", NULL)) == NULL ||
1280 _cups_strcasecmp(ppd_attr->value, "none")))
1281 {
1282 /*
1283 * No command filter and no cupsCommands keyword telling us not to use one.
1284 * See if this is a PostScript printer, and if so add a PostScript command
1285 * filter...
1286 */
1287
1288 for (filter = (const char *)cupsArrayFirst(pc->filters);
1289 filter;
1290 filter = (const char *)cupsArrayNext(pc->filters))
1291 if (!_cups_strncasecmp(filter, "application/vnd.cups-postscript", 31) &&
1292 _cups_isspace(filter[31]))
1293 break;
1294
1295 if (filter)
1296 cupsArrayAdd(pc->filters,
1297 "application/vnd.cups-command application/postscript 100 "
1298 "commandtops");
1299 }
1300
1301 if ((ppd_attr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
1302 {
1303 pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0,
1304 (cups_acopy_func_t)_cupsStrAlloc,
1305 (cups_afree_func_t)_cupsStrFree);
1306
1307 do
1308 {
1309 cupsArrayAdd(pc->prefilters, ppd_attr->value);
1310 }
1311 while ((ppd_attr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL)) != NULL);
1312 }
1313
1314 if ((ppd_attr = ppdFindAttr(ppd, "cupsSingleFile", NULL)) != NULL)
1315 pc->single_file = !_cups_strcasecmp(ppd_attr->value, "true");
1316
1317 /*
1318 * Copy the product string, if any...
1319 */
1320
1321 if (ppd->product)
1322 pc->product = _cupsStrAlloc(ppd->product);
1323
1324 /*
1325 * Copy finishings mapping data...
1326 */
1327
1328 if ((ppd_attr = ppdFindAttr(ppd, "cupsIPPFinishings", NULL)) != NULL)
1329 {
1330 pc->finishings = cupsArrayNew3((cups_array_func_t)pwg_compare_finishings,
1331 NULL, NULL, 0, NULL,
1332 (cups_afree_func_t)pwg_free_finishings);
1333
1334 do
1335 {
1336 if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL)
1337 goto create_error;
1338
1339 finishings->value = (ipp_finishings_t)atoi(ppd_attr->spec);
1340 finishings->num_options = _ppdParseOptions(ppd_attr->value, 0,
1341 &(finishings->options),
1342 _PPD_PARSE_OPTIONS);
1343
1344 cupsArrayAdd(pc->finishings, finishings);
1345 }
1346 while ((ppd_attr = ppdFindNextAttr(ppd, "cupsIPPFinishings",
1347 NULL)) != NULL);
1348 }
1349
1350 /*
1351 * Max copies...
1352 */
1353
1354 if ((ppd_attr = ppdFindAttr(ppd, "cupsMaxCopies", NULL)) != NULL)
1355 pc->max_copies = atoi(ppd_attr->value);
1356 else if (ppd->manual_copies)
1357 pc->max_copies = 1;
1358 else
1359 pc->max_copies = 9999;
1360
1361 /*
1362 * cupsChargeInfoURI, cupsJobAccountId, cupsJobAccountingUserId,
1363 * cupsJobPassword, and cupsMandatory.
1364 */
1365
1366 if ((ppd_attr = ppdFindAttr(ppd, "cupsChargeInfoURI", NULL)) != NULL)
1367 pc->charge_info_uri = _cupsStrAlloc(ppd_attr->value);
1368
1369 if ((ppd_attr = ppdFindAttr(ppd, "cupsJobAccountId", NULL)) != NULL)
1370 pc->account_id = !_cups_strcasecmp(ppd_attr->value, "true");
1371
1372 if ((ppd_attr = ppdFindAttr(ppd, "cupsJobAccountingUserId", NULL)) != NULL)
1373 pc->accounting_user_id = !_cups_strcasecmp(ppd_attr->value, "true");
1374
1375 if ((ppd_attr = ppdFindAttr(ppd, "cupsJobPassword", NULL)) != NULL)
1376 pc->password = _cupsStrAlloc(ppd_attr->value);
1377
1378 if ((ppd_attr = ppdFindAttr(ppd, "cupsMandatory", NULL)) != NULL)
1379 pc->mandatory = _cupsArrayNewStrings(ppd_attr->value, ' ');
1380
1381 /*
1382 * Support files...
1383 */
1384
1385 pc->support_files = cupsArrayNew3(NULL, NULL, NULL, 0,
1386 (cups_acopy_func_t)_cupsStrAlloc,
1387 (cups_afree_func_t)_cupsStrFree);
1388
1389 for (ppd_attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
1390 ppd_attr;
1391 ppd_attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL))
1392 cupsArrayAdd(pc->support_files, ppd_attr->value);
1393
1394 if ((ppd_attr = ppdFindAttr(ppd, "APPrinterIconPath", NULL)) != NULL)
1395 cupsArrayAdd(pc->support_files, ppd_attr->value);
1396
1397 /*
1398 * Return the cache data...
1399 */
1400
1401 return (pc);
1402
1403 /*
1404 * If we get here we need to destroy the PWG mapping data and return NULL...
1405 */
1406
1407 create_error:
1408
1409 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Out of memory."), 1);
1410 _ppdCacheDestroy(pc);
1411
1412 return (NULL);
1413 }
1414
1415
1416 /*
1417 * '_ppdCacheDestroy()' - Free all memory used for PWG mapping data.
1418 */
1419
1420 void
1421 _ppdCacheDestroy(_ppd_cache_t *pc) /* I - PPD cache and mapping data */
1422 {
1423 int i; /* Looping var */
1424 pwg_map_t *map; /* Current map */
1425 pwg_size_t *size; /* Current size */
1426
1427
1428 /*
1429 * Range check input...
1430 */
1431
1432 if (!pc)
1433 return;
1434
1435 /*
1436 * Free memory as needed...
1437 */
1438
1439 if (pc->bins)
1440 {
1441 for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++)
1442 {
1443 _cupsStrFree(map->pwg);
1444 _cupsStrFree(map->ppd);
1445 }
1446
1447 free(pc->bins);
1448 }
1449
1450 if (pc->sizes)
1451 {
1452 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
1453 {
1454 _cupsStrFree(size->map.pwg);
1455 _cupsStrFree(size->map.ppd);
1456 }
1457
1458 free(pc->sizes);
1459 }
1460
1461 if (pc->source_option)
1462 _cupsStrFree(pc->source_option);
1463
1464 if (pc->sources)
1465 {
1466 for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++)
1467 {
1468 _cupsStrFree(map->pwg);
1469 _cupsStrFree(map->ppd);
1470 }
1471
1472 free(pc->sources);
1473 }
1474
1475 if (pc->types)
1476 {
1477 for (i = pc->num_types, map = pc->types; i > 0; i --, map ++)
1478 {
1479 _cupsStrFree(map->pwg);
1480 _cupsStrFree(map->ppd);
1481 }
1482
1483 free(pc->types);
1484 }
1485
1486 if (pc->custom_max_keyword)
1487 _cupsStrFree(pc->custom_max_keyword);
1488
1489 if (pc->custom_min_keyword)
1490 _cupsStrFree(pc->custom_min_keyword);
1491
1492 _cupsStrFree(pc->product);
1493 cupsArrayDelete(pc->filters);
1494 cupsArrayDelete(pc->prefilters);
1495 cupsArrayDelete(pc->finishings);
1496
1497 _cupsStrFree(pc->charge_info_uri);
1498 _cupsStrFree(pc->password);
1499
1500 cupsArrayDelete(pc->mandatory);
1501
1502 cupsArrayDelete(pc->support_files);
1503
1504 free(pc);
1505 }
1506
1507
1508 /*
1509 * '_ppdCacheGetBin()' - Get the PWG output-bin keyword associated with a PPD
1510 * OutputBin.
1511 */
1512
1513 const char * /* O - output-bin or NULL */
1514 _ppdCacheGetBin(
1515 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1516 const char *output_bin) /* I - PPD OutputBin string */
1517 {
1518 int i; /* Looping var */
1519
1520
1521 /*
1522 * Range check input...
1523 */
1524
1525 if (!pc || !output_bin)
1526 return (NULL);
1527
1528 /*
1529 * Look up the OutputBin string...
1530 */
1531
1532
1533 for (i = 0; i < pc->num_bins; i ++)
1534 if (!_cups_strcasecmp(output_bin, pc->bins[i].ppd))
1535 return (pc->bins[i].pwg);
1536
1537 return (NULL);
1538 }
1539
1540
1541 /*
1542 * '_ppdCacheGetFinishingOptions()' - Get PPD finishing options for the given
1543 * IPP finishings value(s).
1544 */
1545
1546 int /* O - New number of options */
1547 _ppdCacheGetFinishingOptions(
1548 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1549 ipp_t *job, /* I - Job attributes or NULL */
1550 ipp_finishings_t value, /* I - IPP finishings value of IPP_FINISHINGS_NONE */
1551 int num_options, /* I - Number of options */
1552 cups_option_t **options) /* IO - Options */
1553 {
1554 int i; /* Looping var */
1555 _pwg_finishings_t *f, /* PWG finishings options */
1556 key; /* Search key */
1557 ipp_attribute_t *attr; /* Finishings attribute */
1558 cups_option_t *option; /* Current finishings option */
1559
1560
1561 /*
1562 * Range check input...
1563 */
1564
1565 if (!pc || cupsArrayCount(pc->finishings) == 0 || !options ||
1566 (!job && value == IPP_FINISHINGS_NONE))
1567 return (num_options);
1568
1569 /*
1570 * Apply finishing options...
1571 */
1572
1573 if (job && (attr = ippFindAttribute(job, "finishings", IPP_TAG_ENUM)) != NULL)
1574 {
1575 int num_values = ippGetCount(attr); /* Number of values */
1576
1577 for (i = 0; i < num_values; i ++)
1578 {
1579 key.value = (ipp_finishings_t)ippGetInteger(attr, i);
1580
1581 if ((f = cupsArrayFind(pc->finishings, &key)) != NULL)
1582 {
1583 int j; /* Another looping var */
1584
1585 for (j = f->num_options, option = f->options; j > 0; j --, option ++)
1586 num_options = cupsAddOption(option->name, option->value,
1587 num_options, options);
1588 }
1589 }
1590 }
1591 else if (value != IPP_FINISHINGS_NONE)
1592 {
1593 key.value = value;
1594
1595 if ((f = cupsArrayFind(pc->finishings, &key)) != NULL)
1596 {
1597 int j; /* Another looping var */
1598
1599 for (j = f->num_options, option = f->options; j > 0; j --, option ++)
1600 num_options = cupsAddOption(option->name, option->value,
1601 num_options, options);
1602 }
1603 }
1604
1605 return (num_options);
1606 }
1607
1608
1609 /*
1610 * '_ppdCacheGetFinishingValues()' - Get IPP finishings value(s) from the given
1611 * PPD options.
1612 */
1613
1614 int /* O - Number of finishings values */
1615 _ppdCacheGetFinishingValues(
1616 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1617 int num_options, /* I - Number of options */
1618 cups_option_t *options, /* I - Options */
1619 int max_values, /* I - Maximum number of finishings values */
1620 int *values) /* O - Finishings values */
1621 {
1622 int i, /* Looping var */
1623 num_values = 0; /* Number of values */
1624 _pwg_finishings_t *f; /* Current finishings option */
1625 cups_option_t *option; /* Current option */
1626 const char *val; /* Value for option */
1627
1628
1629 /*
1630 * Range check input...
1631 */
1632
1633 DEBUG_printf(("_ppdCacheGetFinishingValues(pc=%p, num_options=%d, options=%p, max_values=%d, values=%p)", pc, num_options, options, max_values, values));
1634
1635 if (!pc || !pc->finishings || num_options < 1 || max_values < 1 || !values)
1636 {
1637 DEBUG_puts("_ppdCacheGetFinishingValues: Bad arguments, returning 0.");
1638 return (0);
1639 }
1640
1641 /*
1642 * Go through the finishings options and see what is set...
1643 */
1644
1645 for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings);
1646 f;
1647 f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings))
1648 {
1649 DEBUG_printf(("_ppdCacheGetFinishingValues: Checking %d (%s)", f->value, ippEnumString("finishings", f->value)));
1650
1651 for (i = f->num_options, option = f->options; i > 0; i --, option ++)
1652 {
1653 DEBUG_printf(("_ppdCacheGetFinishingValues: %s=%s?", option->name, option->value));
1654
1655 if ((val = cupsGetOption(option->name, num_options, options)) == NULL ||
1656 _cups_strcasecmp(option->value, val))
1657 {
1658 DEBUG_puts("_ppdCacheGetFinishingValues: NO");
1659 break;
1660 }
1661 }
1662
1663 if (i == 0)
1664 {
1665 DEBUG_printf(("_ppdCacheGetFinishingValues: Adding %d.", f->value));
1666
1667 values[num_values ++] = f->value;
1668
1669 if (num_values >= max_values)
1670 break;
1671 }
1672 }
1673
1674 DEBUG_printf(("_ppdCacheGetFinishingValues: Returning %d.", num_values));
1675
1676 return (num_values);
1677 }
1678
1679
1680 /*
1681 * '_ppdCacheGetInputSlot()' - Get the PPD InputSlot associated with the job
1682 * attributes or a keyword string.
1683 */
1684
1685 const char * /* O - PPD InputSlot or NULL */
1686 _ppdCacheGetInputSlot(
1687 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1688 ipp_t *job, /* I - Job attributes or NULL */
1689 const char *keyword) /* I - Keyword string or NULL */
1690 {
1691 /*
1692 * Range check input...
1693 */
1694
1695 if (!pc || pc->num_sources == 0 || (!job && !keyword))
1696 return (NULL);
1697
1698 if (job && !keyword)
1699 {
1700 /*
1701 * Lookup the media-col attribute and any media-source found there...
1702 */
1703
1704 ipp_attribute_t *media_col, /* media-col attribute */
1705 *media_source; /* media-source attribute */
1706 pwg_size_t size; /* Dimensional size */
1707 int margins_set; /* Were the margins set? */
1708
1709 media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION);
1710 if (media_col &&
1711 (media_source = ippFindAttribute(ippGetCollection(media_col, 0),
1712 "media-source",
1713 IPP_TAG_KEYWORD)) != NULL)
1714 {
1715 /*
1716 * Use the media-source value from media-col...
1717 */
1718
1719 keyword = ippGetString(media_source, 0, NULL);
1720 }
1721 else if (pwgInitSize(&size, job, &margins_set))
1722 {
1723 /*
1724 * For media <= 5x7, look for a photo tray...
1725 */
1726
1727 if (size.width <= (5 * 2540) && size.length <= (7 * 2540))
1728 keyword = "photo";
1729 }
1730 }
1731
1732 if (keyword)
1733 {
1734 int i; /* Looping var */
1735
1736 for (i = 0; i < pc->num_sources; i ++)
1737 if (!_cups_strcasecmp(keyword, pc->sources[i].pwg))
1738 return (pc->sources[i].ppd);
1739 }
1740
1741 return (NULL);
1742 }
1743
1744
1745 /*
1746 * '_ppdCacheGetMediaType()' - Get the PPD MediaType associated with the job
1747 * attributes or a keyword string.
1748 */
1749
1750 const char * /* O - PPD MediaType or NULL */
1751 _ppdCacheGetMediaType(
1752 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1753 ipp_t *job, /* I - Job attributes or NULL */
1754 const char *keyword) /* I - Keyword string or NULL */
1755 {
1756 /*
1757 * Range check input...
1758 */
1759
1760 if (!pc || pc->num_types == 0 || (!job && !keyword))
1761 return (NULL);
1762
1763 if (job && !keyword)
1764 {
1765 /*
1766 * Lookup the media-col attribute and any media-source found there...
1767 */
1768
1769 ipp_attribute_t *media_col, /* media-col attribute */
1770 *media_type; /* media-type attribute */
1771
1772 media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION);
1773 if (media_col)
1774 {
1775 if ((media_type = ippFindAttribute(media_col->values[0].collection,
1776 "media-type",
1777 IPP_TAG_KEYWORD)) == NULL)
1778 media_type = ippFindAttribute(media_col->values[0].collection,
1779 "media-type", IPP_TAG_NAME);
1780
1781 if (media_type)
1782 keyword = media_type->values[0].string.text;
1783 }
1784 }
1785
1786 if (keyword)
1787 {
1788 int i; /* Looping var */
1789
1790 for (i = 0; i < pc->num_types; i ++)
1791 if (!_cups_strcasecmp(keyword, pc->types[i].pwg))
1792 return (pc->types[i].ppd);
1793 }
1794
1795 return (NULL);
1796 }
1797
1798
1799 /*
1800 * '_ppdCacheGetOutputBin()' - Get the PPD OutputBin associated with the keyword
1801 * string.
1802 */
1803
1804 const char * /* O - PPD OutputBin or NULL */
1805 _ppdCacheGetOutputBin(
1806 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1807 const char *output_bin) /* I - Keyword string */
1808 {
1809 int i; /* Looping var */
1810
1811
1812 /*
1813 * Range check input...
1814 */
1815
1816 if (!pc || !output_bin)
1817 return (NULL);
1818
1819 /*
1820 * Look up the OutputBin string...
1821 */
1822
1823
1824 for (i = 0; i < pc->num_bins; i ++)
1825 if (!_cups_strcasecmp(output_bin, pc->bins[i].pwg))
1826 return (pc->bins[i].ppd);
1827
1828 return (NULL);
1829 }
1830
1831
1832 /*
1833 * '_ppdCacheGetPageSize()' - Get the PPD PageSize associated with the job
1834 * attributes or a keyword string.
1835 */
1836
1837 const char * /* O - PPD PageSize or NULL */
1838 _ppdCacheGetPageSize(
1839 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1840 ipp_t *job, /* I - Job attributes or NULL */
1841 const char *keyword, /* I - Keyword string or NULL */
1842 int *exact) /* O - 1 if exact match, 0 otherwise */
1843 {
1844 int i; /* Looping var */
1845 pwg_size_t *size, /* Current size */
1846 *closest, /* Closest size */
1847 jobsize; /* Size data from job */
1848 int margins_set, /* Were the margins set? */
1849 dwidth, /* Difference in width */
1850 dlength, /* Difference in length */
1851 dleft, /* Difference in left margins */
1852 dright, /* Difference in right margins */
1853 dbottom, /* Difference in bottom margins */
1854 dtop, /* Difference in top margins */
1855 dmin, /* Minimum difference */
1856 dclosest; /* Closest difference */
1857 const char *ppd_name; /* PPD media name */
1858
1859
1860 DEBUG_printf(("_ppdCacheGetPageSize(pc=%p, job=%p, keyword=\"%s\", exact=%p)",
1861 pc, job, keyword, exact));
1862
1863 /*
1864 * Range check input...
1865 */
1866
1867 if (!pc || (!job && !keyword))
1868 return (NULL);
1869
1870 if (exact)
1871 *exact = 0;
1872
1873 ppd_name = keyword;
1874
1875 if (job)
1876 {
1877 /*
1878 * Try getting the PPD media name from the job attributes...
1879 */
1880
1881 ipp_attribute_t *attr; /* Job attribute */
1882
1883 if ((attr = ippFindAttribute(job, "PageSize", IPP_TAG_ZERO)) == NULL)
1884 if ((attr = ippFindAttribute(job, "PageRegion", IPP_TAG_ZERO)) == NULL)
1885 attr = ippFindAttribute(job, "media", IPP_TAG_ZERO);
1886
1887 #ifdef DEBUG
1888 if (attr)
1889 DEBUG_printf(("1_ppdCacheGetPageSize: Found attribute %s (%s)",
1890 attr->name, ippTagString(attr->value_tag)));
1891 else
1892 DEBUG_puts("1_ppdCacheGetPageSize: Did not find media attribute.");
1893 #endif /* DEBUG */
1894
1895 if (attr && (attr->value_tag == IPP_TAG_NAME ||
1896 attr->value_tag == IPP_TAG_KEYWORD))
1897 ppd_name = attr->values[0].string.text;
1898 }
1899
1900 DEBUG_printf(("1_ppdCacheGetPageSize: ppd_name=\"%s\"", ppd_name));
1901
1902 if (ppd_name)
1903 {
1904 /*
1905 * Try looking up the named PPD size first...
1906 */
1907
1908 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
1909 {
1910 DEBUG_printf(("2_ppdCacheGetPageSize: size[%d]=[\"%s\" \"%s\"]",
1911 (int)(size - pc->sizes), size->map.pwg, size->map.ppd));
1912
1913 if (!_cups_strcasecmp(ppd_name, size->map.ppd) ||
1914 !_cups_strcasecmp(ppd_name, size->map.pwg))
1915 {
1916 if (exact)
1917 *exact = 1;
1918
1919 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", ppd_name));
1920
1921 return (size->map.ppd);
1922 }
1923 }
1924 }
1925
1926 if (job && !keyword)
1927 {
1928 /*
1929 * Get the size using media-col or media, with the preference being
1930 * media-col.
1931 */
1932
1933 if (!pwgInitSize(&jobsize, job, &margins_set))
1934 return (NULL);
1935 }
1936 else
1937 {
1938 /*
1939 * Get the size using a media keyword...
1940 */
1941
1942 pwg_media_t *media; /* Media definition */
1943
1944
1945 if ((media = pwgMediaForPWG(keyword)) == NULL)
1946 if ((media = pwgMediaForLegacy(keyword)) == NULL)
1947 if ((media = pwgMediaForPPD(keyword)) == NULL)
1948 return (NULL);
1949
1950 jobsize.width = media->width;
1951 jobsize.length = media->length;
1952 margins_set = 0;
1953 }
1954
1955 /*
1956 * Now that we have the dimensions and possibly the margins, look at the
1957 * available sizes and find the match...
1958 */
1959
1960 closest = NULL;
1961 dclosest = 999999999;
1962
1963 if (!ppd_name || _cups_strncasecmp(ppd_name, "Custom.", 7) ||
1964 _cups_strncasecmp(ppd_name, "custom_", 7))
1965 {
1966 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
1967 {
1968 /*
1969 * Adobe uses a size matching algorithm with an epsilon of 5 points, which
1970 * is just about 176/2540ths...
1971 */
1972
1973 dwidth = size->width - jobsize.width;
1974 dlength = size->length - jobsize.length;
1975
1976 if (dwidth <= -176 || dwidth >= 176 || dlength <= -176 || dlength >= 176)
1977 continue;
1978
1979 if (margins_set)
1980 {
1981 /*
1982 * Use a tighter epsilon of 1 point (35/2540ths) for margins...
1983 */
1984
1985 dleft = size->left - jobsize.left;
1986 dright = size->right - jobsize.right;
1987 dtop = size->top - jobsize.top;
1988 dbottom = size->bottom - jobsize.bottom;
1989
1990 if (dleft <= -35 || dleft >= 35 || dright <= -35 || dright >= 35 ||
1991 dtop <= -35 || dtop >= 35 || dbottom <= -35 || dbottom >= 35)
1992 {
1993 dleft = dleft < 0 ? -dleft : dleft;
1994 dright = dright < 0 ? -dright : dright;
1995 dbottom = dbottom < 0 ? -dbottom : dbottom;
1996 dtop = dtop < 0 ? -dtop : dtop;
1997 dmin = dleft + dright + dbottom + dtop;
1998
1999 if (dmin < dclosest)
2000 {
2001 dclosest = dmin;
2002 closest = size;
2003 }
2004
2005 continue;
2006 }
2007 }
2008
2009 if (exact)
2010 *exact = 1;
2011
2012 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", size->map.ppd));
2013
2014 return (size->map.ppd);
2015 }
2016 }
2017
2018 if (closest)
2019 {
2020 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (closest)",
2021 closest->map.ppd));
2022
2023 return (closest->map.ppd);
2024 }
2025
2026 /*
2027 * If we get here we need to check for custom page size support...
2028 */
2029
2030 if (jobsize.width >= pc->custom_min_width &&
2031 jobsize.width <= pc->custom_max_width &&
2032 jobsize.length >= pc->custom_min_length &&
2033 jobsize.length <= pc->custom_max_length)
2034 {
2035 /*
2036 * In range, format as Custom.WWWWxLLLL (points).
2037 */
2038
2039 snprintf(pc->custom_ppd_size, sizeof(pc->custom_ppd_size), "Custom.%dx%d",
2040 (int)PWG_TO_POINTS(jobsize.width), (int)PWG_TO_POINTS(jobsize.length));
2041
2042 if (margins_set && exact)
2043 {
2044 dleft = pc->custom_size.left - jobsize.left;
2045 dright = pc->custom_size.right - jobsize.right;
2046 dtop = pc->custom_size.top - jobsize.top;
2047 dbottom = pc->custom_size.bottom - jobsize.bottom;
2048
2049 if (dleft > -35 && dleft < 35 && dright > -35 && dright < 35 &&
2050 dtop > -35 && dtop < 35 && dbottom > -35 && dbottom < 35)
2051 *exact = 1;
2052 }
2053 else if (exact)
2054 *exact = 1;
2055
2056 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (custom)",
2057 pc->custom_ppd_size));
2058
2059 return (pc->custom_ppd_size);
2060 }
2061
2062 /*
2063 * No custom page size support or the size is out of range - return NULL.
2064 */
2065
2066 DEBUG_puts("1_ppdCacheGetPageSize: Returning NULL");
2067
2068 return (NULL);
2069 }
2070
2071
2072 /*
2073 * '_ppdCacheGetSize()' - Get the PWG size associated with a PPD PageSize.
2074 */
2075
2076 pwg_size_t * /* O - PWG size or NULL */
2077 _ppdCacheGetSize(
2078 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2079 const char *page_size) /* I - PPD PageSize */
2080 {
2081 int i; /* Looping var */
2082 pwg_media_t *media; /* Media */
2083 pwg_size_t *size; /* Current size */
2084
2085
2086 /*
2087 * Range check input...
2088 */
2089
2090 if (!pc || !page_size)
2091 return (NULL);
2092
2093 if (!_cups_strncasecmp(page_size, "Custom.", 7))
2094 {
2095 /*
2096 * Custom size; size name can be one of the following:
2097 *
2098 * Custom.WIDTHxLENGTHin - Size in inches
2099 * Custom.WIDTHxLENGTHft - Size in feet
2100 * Custom.WIDTHxLENGTHcm - Size in centimeters
2101 * Custom.WIDTHxLENGTHmm - Size in millimeters
2102 * Custom.WIDTHxLENGTHm - Size in meters
2103 * Custom.WIDTHxLENGTH[pt] - Size in points
2104 */
2105
2106 double w, l; /* Width and length of page */
2107 char *ptr; /* Pointer into PageSize */
2108 struct lconv *loc; /* Locale data */
2109
2110 loc = localeconv();
2111 w = (float)_cupsStrScand(page_size + 7, &ptr, loc);
2112 if (!ptr || *ptr != 'x')
2113 return (NULL);
2114
2115 l = (float)_cupsStrScand(ptr + 1, &ptr, loc);
2116 if (!ptr)
2117 return (NULL);
2118
2119 if (!_cups_strcasecmp(ptr, "in"))
2120 {
2121 w *= 2540.0;
2122 l *= 2540.0;
2123 }
2124 else if (!_cups_strcasecmp(ptr, "ft"))
2125 {
2126 w *= 12.0 * 2540.0;
2127 l *= 12.0 * 2540.0;
2128 }
2129 else if (!_cups_strcasecmp(ptr, "mm"))
2130 {
2131 w *= 100.0;
2132 l *= 100.0;
2133 }
2134 else if (!_cups_strcasecmp(ptr, "cm"))
2135 {
2136 w *= 1000.0;
2137 l *= 1000.0;
2138 }
2139 else if (!_cups_strcasecmp(ptr, "m"))
2140 {
2141 w *= 100000.0;
2142 l *= 100000.0;
2143 }
2144 else
2145 {
2146 w *= 2540.0 / 72.0;
2147 l *= 2540.0 / 72.0;
2148 }
2149
2150 pc->custom_size.width = (int)w;
2151 pc->custom_size.length = (int)l;
2152
2153 return (&(pc->custom_size));
2154 }
2155
2156 /*
2157 * Not a custom size - look it up...
2158 */
2159
2160 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
2161 if (!_cups_strcasecmp(page_size, size->map.ppd) ||
2162 !_cups_strcasecmp(page_size, size->map.pwg))
2163 return (size);
2164
2165 /*
2166 * Look up standard sizes...
2167 */
2168
2169 if ((media = pwgMediaForPPD(page_size)) == NULL)
2170 if ((media = pwgMediaForLegacy(page_size)) == NULL)
2171 media = pwgMediaForPWG(page_size);
2172
2173 if (media)
2174 {
2175 pc->custom_size.width = media->width;
2176 pc->custom_size.length = media->length;
2177
2178 return (&(pc->custom_size));
2179 }
2180
2181 return (NULL);
2182 }
2183
2184
2185 /*
2186 * '_ppdCacheGetSource()' - Get the PWG media-source associated with a PPD
2187 * InputSlot.
2188 */
2189
2190 const char * /* O - PWG media-source keyword */
2191 _ppdCacheGetSource(
2192 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2193 const char *input_slot) /* I - PPD InputSlot */
2194 {
2195 int i; /* Looping var */
2196 pwg_map_t *source; /* Current source */
2197
2198
2199 /*
2200 * Range check input...
2201 */
2202
2203 if (!pc || !input_slot)
2204 return (NULL);
2205
2206 for (i = pc->num_sources, source = pc->sources; i > 0; i --, source ++)
2207 if (!_cups_strcasecmp(input_slot, source->ppd))
2208 return (source->pwg);
2209
2210 return (NULL);
2211 }
2212
2213
2214 /*
2215 * '_ppdCacheGetType()' - Get the PWG media-type associated with a PPD
2216 * MediaType.
2217 */
2218
2219 const char * /* O - PWG media-type keyword */
2220 _ppdCacheGetType(
2221 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2222 const char *media_type) /* I - PPD MediaType */
2223 {
2224 int i; /* Looping var */
2225 pwg_map_t *type; /* Current type */
2226
2227
2228 /*
2229 * Range check input...
2230 */
2231
2232 if (!pc || !media_type)
2233 return (NULL);
2234
2235 for (i = pc->num_types, type = pc->types; i > 0; i --, type ++)
2236 if (!_cups_strcasecmp(media_type, type->ppd))
2237 return (type->pwg);
2238
2239 return (NULL);
2240 }
2241
2242
2243 /*
2244 * '_ppdCacheWriteFile()' - Write PWG mapping data to a file.
2245 */
2246
2247 int /* O - 1 on success, 0 on failure */
2248 _ppdCacheWriteFile(
2249 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2250 const char *filename, /* I - File to write */
2251 ipp_t *attrs) /* I - Attributes to write, if any */
2252 {
2253 int i, j, k; /* Looping vars */
2254 cups_file_t *fp; /* Output file */
2255 pwg_size_t *size; /* Current size */
2256 pwg_map_t *map; /* Current map */
2257 _pwg_finishings_t *f; /* Current finishing option */
2258 cups_option_t *option; /* Current option */
2259 const char *value; /* Filter/pre-filter value */
2260 char newfile[1024]; /* New filename */
2261
2262
2263 /*
2264 * Range check input...
2265 */
2266
2267 if (!pc || !filename)
2268 {
2269 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
2270 return (0);
2271 }
2272
2273 /*
2274 * Open the file and write with compression...
2275 */
2276
2277 snprintf(newfile, sizeof(newfile), "%s.N", filename);
2278 if ((fp = cupsFileOpen(newfile, "w9")) == NULL)
2279 {
2280 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
2281 return (0);
2282 }
2283
2284 /*
2285 * Standard header...
2286 */
2287
2288 cupsFilePrintf(fp, "#CUPS-PPD-CACHE-%d\n", _PPD_CACHE_VERSION);
2289
2290 /*
2291 * Output bins...
2292 */
2293
2294 if (pc->num_bins > 0)
2295 {
2296 cupsFilePrintf(fp, "NumBins %d\n", pc->num_bins);
2297 for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++)
2298 cupsFilePrintf(fp, "Bin %s %s\n", map->pwg, map->ppd);
2299 }
2300
2301 /*
2302 * Media sizes...
2303 */
2304
2305 cupsFilePrintf(fp, "NumSizes %d\n", pc->num_sizes);
2306 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
2307 cupsFilePrintf(fp, "Size %s %s %d %d %d %d %d %d\n", size->map.pwg,
2308 size->map.ppd, size->width, size->length, size->left,
2309 size->bottom, size->right, size->top);
2310 if (pc->custom_max_width > 0)
2311 cupsFilePrintf(fp, "CustomSize %d %d %d %d %d %d %d %d\n",
2312 pc->custom_max_width, pc->custom_max_length,
2313 pc->custom_min_width, pc->custom_min_length,
2314 pc->custom_size.left, pc->custom_size.bottom,
2315 pc->custom_size.right, pc->custom_size.top);
2316
2317 /*
2318 * Media sources...
2319 */
2320
2321 if (pc->source_option)
2322 cupsFilePrintf(fp, "SourceOption %s\n", pc->source_option);
2323
2324 if (pc->num_sources > 0)
2325 {
2326 cupsFilePrintf(fp, "NumSources %d\n", pc->num_sources);
2327 for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++)
2328 cupsFilePrintf(fp, "Source %s %s\n", map->pwg, map->ppd);
2329 }
2330
2331 /*
2332 * Media types...
2333 */
2334
2335 if (pc->num_types > 0)
2336 {
2337 cupsFilePrintf(fp, "NumTypes %d\n", pc->num_types);
2338 for (i = pc->num_types, map = pc->types; i > 0; i --, map ++)
2339 cupsFilePrintf(fp, "Type %s %s\n", map->pwg, map->ppd);
2340 }
2341
2342 /*
2343 * Presets...
2344 */
2345
2346 for (i = _PWG_PRINT_COLOR_MODE_MONOCHROME; i < _PWG_PRINT_COLOR_MODE_MAX; i ++)
2347 for (j = _PWG_PRINT_QUALITY_DRAFT; j < _PWG_PRINT_QUALITY_MAX; j ++)
2348 if (pc->num_presets[i][j])
2349 {
2350 cupsFilePrintf(fp, "Preset %d %d", i, j);
2351 for (k = pc->num_presets[i][j], option = pc->presets[i][j];
2352 k > 0;
2353 k --, option ++)
2354 cupsFilePrintf(fp, " %s=%s", option->name, option->value);
2355 cupsFilePutChar(fp, '\n');
2356 }
2357
2358 /*
2359 * Duplex/sides...
2360 */
2361
2362 if (pc->sides_option)
2363 cupsFilePrintf(fp, "SidesOption %s\n", pc->sides_option);
2364
2365 if (pc->sides_1sided)
2366 cupsFilePrintf(fp, "Sides1Sided %s\n", pc->sides_1sided);
2367
2368 if (pc->sides_2sided_long)
2369 cupsFilePrintf(fp, "Sides2SidedLong %s\n", pc->sides_2sided_long);
2370
2371 if (pc->sides_2sided_short)
2372 cupsFilePrintf(fp, "Sides2SidedShort %s\n", pc->sides_2sided_short);
2373
2374 /*
2375 * Product, cupsFilter, cupsFilter2, and cupsPreFilter...
2376 */
2377
2378 if (pc->product)
2379 cupsFilePutConf(fp, "Product", pc->product);
2380
2381 for (value = (const char *)cupsArrayFirst(pc->filters);
2382 value;
2383 value = (const char *)cupsArrayNext(pc->filters))
2384 cupsFilePutConf(fp, "Filter", value);
2385
2386 for (value = (const char *)cupsArrayFirst(pc->prefilters);
2387 value;
2388 value = (const char *)cupsArrayNext(pc->prefilters))
2389 cupsFilePutConf(fp, "PreFilter", value);
2390
2391 cupsFilePrintf(fp, "SingleFile %s\n", pc->single_file ? "true" : "false");
2392
2393 /*
2394 * Finishing options...
2395 */
2396
2397 for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings);
2398 f;
2399 f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings))
2400 {
2401 cupsFilePrintf(fp, "Finishings %d", f->value);
2402 for (i = f->num_options, option = f->options; i > 0; i --, option ++)
2403 cupsFilePrintf(fp, " %s=%s", option->name, option->value);
2404 cupsFilePutChar(fp, '\n');
2405 }
2406
2407 /*
2408 * Max copies...
2409 */
2410
2411 cupsFilePrintf(fp, "MaxCopies %d\n", pc->max_copies);
2412
2413 /*
2414 * Accounting/quota/PIN/managed printing values...
2415 */
2416
2417 if (pc->charge_info_uri)
2418 cupsFilePutConf(fp, "ChargeInfoURI", pc->charge_info_uri);
2419
2420 cupsFilePrintf(fp, "AccountId %s\n", pc->account_id ? "true" : "false");
2421 cupsFilePrintf(fp, "AccountingUserId %s\n",
2422 pc->accounting_user_id ? "true" : "false");
2423
2424 if (pc->password)
2425 cupsFilePutConf(fp, "Password", pc->password);
2426
2427 for (value = (char *)cupsArrayFirst(pc->mandatory);
2428 value;
2429 value = (char *)cupsArrayNext(pc->mandatory))
2430 cupsFilePutConf(fp, "Mandatory", value);
2431
2432 /*
2433 * Support files...
2434 */
2435
2436 for (value = (char *)cupsArrayFirst(pc->support_files);
2437 value;
2438 value = (char *)cupsArrayNext(pc->support_files))
2439 cupsFilePutConf(fp, "SupportFile", value);
2440
2441 /*
2442 * IPP attributes, if any...
2443 */
2444
2445 if (attrs)
2446 {
2447 cupsFilePrintf(fp, "IPP " CUPS_LLFMT "\n", CUPS_LLCAST ippLength(attrs));
2448
2449 attrs->state = IPP_STATE_IDLE;
2450 ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL, attrs);
2451 }
2452
2453 /*
2454 * Close and return...
2455 */
2456
2457 if (cupsFileClose(fp))
2458 {
2459 unlink(newfile);
2460 return (0);
2461 }
2462
2463 unlink(filename);
2464 return (!rename(newfile, filename));
2465 }
2466
2467
2468 /*
2469 * '_pwgInputSlotForSource()' - Get the InputSlot name for the given PWG
2470 * media-source.
2471 */
2472
2473 const char * /* O - InputSlot name */
2474 _pwgInputSlotForSource(
2475 const char *media_source, /* I - PWG media-source */
2476 char *name, /* I - Name buffer */
2477 size_t namesize) /* I - Size of name buffer */
2478 {
2479 /*
2480 * Range check input...
2481 */
2482
2483 if (!media_source || !name || namesize < PPD_MAX_NAME)
2484 return (NULL);
2485
2486 if (_cups_strcasecmp(media_source, "main"))
2487 strlcpy(name, "Cassette", namesize);
2488 else if (_cups_strcasecmp(media_source, "alternate"))
2489 strlcpy(name, "Multipurpose", namesize);
2490 else if (_cups_strcasecmp(media_source, "large-capacity"))
2491 strlcpy(name, "LargeCapacity", namesize);
2492 else if (_cups_strcasecmp(media_source, "bottom"))
2493 strlcpy(name, "Lower", namesize);
2494 else if (_cups_strcasecmp(media_source, "middle"))
2495 strlcpy(name, "Middle", namesize);
2496 else if (_cups_strcasecmp(media_source, "top"))
2497 strlcpy(name, "Upper", namesize);
2498 else if (_cups_strcasecmp(media_source, "rear"))
2499 strlcpy(name, "Rear", namesize);
2500 else if (_cups_strcasecmp(media_source, "side"))
2501 strlcpy(name, "Side", namesize);
2502 else if (_cups_strcasecmp(media_source, "envelope"))
2503 strlcpy(name, "Envelope", namesize);
2504 else if (_cups_strcasecmp(media_source, "main-roll"))
2505 strlcpy(name, "Roll", namesize);
2506 else if (_cups_strcasecmp(media_source, "alternate-roll"))
2507 strlcpy(name, "Roll2", namesize);
2508 else
2509 pwg_ppdize_name(media_source, name, namesize);
2510
2511 return (name);
2512 }
2513
2514
2515 /*
2516 * '_pwgMediaTypeForType()' - Get the MediaType name for the given PWG
2517 * media-type.
2518 */
2519
2520 const char * /* O - MediaType name */
2521 _pwgMediaTypeForType(
2522 const char *media_type, /* I - PWG media-type */
2523 char *name, /* I - Name buffer */
2524 size_t namesize) /* I - Size of name buffer */
2525 {
2526 /*
2527 * Range check input...
2528 */
2529
2530 if (!media_type || !name || namesize < PPD_MAX_NAME)
2531 return (NULL);
2532
2533 if (_cups_strcasecmp(media_type, "auto"))
2534 strlcpy(name, "Auto", namesize);
2535 else if (_cups_strcasecmp(media_type, "cardstock"))
2536 strlcpy(name, "Cardstock", namesize);
2537 else if (_cups_strcasecmp(media_type, "envelope"))
2538 strlcpy(name, "Envelope", namesize);
2539 else if (_cups_strcasecmp(media_type, "photographic-glossy"))
2540 strlcpy(name, "Glossy", namesize);
2541 else if (_cups_strcasecmp(media_type, "photographic-high-gloss"))
2542 strlcpy(name, "HighGloss", namesize);
2543 else if (_cups_strcasecmp(media_type, "photographic-matte"))
2544 strlcpy(name, "Matte", namesize);
2545 else if (_cups_strcasecmp(media_type, "stationery"))
2546 strlcpy(name, "Plain", namesize);
2547 else if (_cups_strcasecmp(media_type, "stationery-coated"))
2548 strlcpy(name, "Coated", namesize);
2549 else if (_cups_strcasecmp(media_type, "stationery-inkjet"))
2550 strlcpy(name, "Inkjet", namesize);
2551 else if (_cups_strcasecmp(media_type, "stationery-letterhead"))
2552 strlcpy(name, "Letterhead", namesize);
2553 else if (_cups_strcasecmp(media_type, "stationery-preprinted"))
2554 strlcpy(name, "Preprinted", namesize);
2555 else if (_cups_strcasecmp(media_type, "transparency"))
2556 strlcpy(name, "Transparency", namesize);
2557 else
2558 pwg_ppdize_name(media_type, name, namesize);
2559
2560 return (name);
2561 }
2562
2563
2564 /*
2565 * '_pwgPageSizeForMedia()' - Get the PageSize name for the given media.
2566 */
2567
2568 const char * /* O - PageSize name */
2569 _pwgPageSizeForMedia(
2570 pwg_media_t *media, /* I - Media */
2571 char *name, /* I - PageSize name buffer */
2572 size_t namesize) /* I - Size of name buffer */
2573 {
2574 const char *sizeptr, /* Pointer to size in PWG name */
2575 *dimptr; /* Pointer to dimensions in PWG name */
2576
2577
2578 /*
2579 * Range check input...
2580 */
2581
2582 if (!media || !name || namesize < PPD_MAX_NAME)
2583 return (NULL);
2584
2585 /*
2586 * Copy or generate a PageSize name...
2587 */
2588
2589 if (media->ppd)
2590 {
2591 /*
2592 * Use a standard Adobe name...
2593 */
2594
2595 strlcpy(name, media->ppd, namesize);
2596 }
2597 else if (!media->pwg || !strncmp(media->pwg, "custom_", 7) ||
2598 (sizeptr = strchr(media->pwg, '_')) == NULL ||
2599 (dimptr = strchr(sizeptr + 1, '_')) == NULL ||
2600 (size_t)(dimptr - sizeptr) > namesize)
2601 {
2602 /*
2603 * Use a name of the form "wNNNhNNN"...
2604 */
2605
2606 snprintf(name, namesize, "w%dh%d", (int)PWG_TO_POINTS(media->width),
2607 (int)PWG_TO_POINTS(media->length));
2608 }
2609 else
2610 {
2611 /*
2612 * Copy the size name from class_sizename_dimensions...
2613 */
2614
2615 memcpy(name, sizeptr + 1, dimptr - sizeptr - 1);
2616 name[dimptr - sizeptr - 1] = '\0';
2617 }
2618
2619 return (name);
2620 }
2621
2622
2623 /*
2624 * 'pwg_compare_finishings()' - Compare two finishings values.
2625 */
2626
2627 static int /* O- Result of comparison */
2628 pwg_compare_finishings(
2629 _pwg_finishings_t *a, /* I - First finishings value */
2630 _pwg_finishings_t *b) /* I - Second finishings value */
2631 {
2632 return (b->value - a->value);
2633 }
2634
2635
2636 /*
2637 * 'pwg_free_finishings()' - Free a finishings value.
2638 */
2639
2640 static void
2641 pwg_free_finishings(
2642 _pwg_finishings_t *f) /* I - Finishings value */
2643 {
2644 cupsFreeOptions(f->num_options, f->options);
2645 free(f);
2646 }
2647
2648
2649 /*
2650 * 'pwg_ppdize_name()' - Convert an IPP keyword to a PPD keyword.
2651 */
2652
2653 static void
2654 pwg_ppdize_name(const char *ipp, /* I - IPP keyword */
2655 char *name, /* I - Name buffer */
2656 size_t namesize) /* I - Size of name buffer */
2657 {
2658 char *ptr, /* Pointer into name buffer */
2659 *end; /* End of name buffer */
2660
2661
2662 *name = (char)toupper(*ipp++);
2663
2664 for (ptr = name + 1, end = name + namesize - 1; *ipp && ptr < end;)
2665 {
2666 if (*ipp == '-' && _cups_isalpha(ipp[1]))
2667 {
2668 ipp ++;
2669 *ptr++ = (char)toupper(*ipp++ & 255);
2670 }
2671 else
2672 *ptr++ = *ipp++;
2673 }
2674
2675 *ptr = '\0';
2676 }
2677
2678
2679 /*
2680 * 'pwg_unppdize_name()' - Convert a PPD keyword to a lowercase IPP keyword.
2681 */
2682
2683 static void
2684 pwg_unppdize_name(const char *ppd, /* I - PPD keyword */
2685 char *name, /* I - Name buffer */
2686 size_t namesize, /* I - Size of name buffer */
2687 const char *dashchars)/* I - Characters to be replaced by dashes */
2688 {
2689 char *ptr, /* Pointer into name buffer */
2690 *end; /* End of name buffer */
2691
2692
2693 for (ptr = name, end = name + namesize - 1; *ppd && ptr < end; ppd ++)
2694 {
2695 if (_cups_isalnum(*ppd) || *ppd == '-')
2696 *ptr++ = (char)tolower(*ppd & 255);
2697 else if (strchr(dashchars, *ppd))
2698 *ptr++ = '-';
2699 else
2700 *ptr++ = *ppd;
2701
2702 if (!_cups_isupper(*ppd) && _cups_isalnum(*ppd) &&
2703 _cups_isupper(ppd[1]) && ptr < end)
2704 *ptr++ = '-';
2705 }
2706
2707 *ptr = '\0';
2708 }
2709
2710
2711 /*
2712 * End of "$Id$".
2713 */