]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/mark.c
Merge changes from CUPS 1.5svn-r9602.
[thirdparty/cups.git] / cups / mark.c
CommitLineData
ef416fc2 1/*
71e16022 2 * "$Id: mark.c 9042 2010-03-24 00:45:34Z mike $"
ef416fc2 3 *
ba55dc12 4 * Option marking routines for CUPS.
ef416fc2 5 *
f14324a7 6 * Copyright 2007-2011 by Apple Inc.
b86bc4cf 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * PostScript is a trademark of Adobe Systems, Inc.
16 *
17 * This file is subject to the Apple OS-Developed Software exception.
18 *
19 * Contents:
20 *
5a738aea 21 * cupsMarkOptions() - Mark command-line options in a PPD file.
ef416fc2 22 * ppdFindChoice() - Return a pointer to an option choice.
23 * ppdFindMarkedChoice() - Return the marked choice for the specified option.
24 * ppdFindOption() - Return a pointer to the specified option.
5a738aea 25 * ppdIsMarked() - Check to see if an option is marked.
ef416fc2 26 * ppdMarkDefaults() - Mark all default options in the PPD file.
66ab9486
MS
27 * ppdMarkOption() - Mark an option in a PPD file and return the number
28 * of conflicts.
5a738aea
MS
29 * ppdFirstOption() - Return the first option in the PPD file.
30 * ppdNextOption() - Return the next option in the PPD file.
66ab9486 31 * _ppdParseOptions() - Parse options from a PPD file.
ba55dc12 32 * ppd_debug_marked() - Output the marked array to stdout...
ef416fc2 33 * ppd_defaults() - Set the defaults for this group and all sub-groups.
5a738aea 34 * ppd_mark_choices() - Mark one or more option choices from a string.
c168a833
MS
35 * ppd_mark_option() - Quickly mark an option without checking for
36 * conflicts.
ef416fc2 37 */
38
39/*
40 * Include necessary headers...
41 */
42
71e16022 43#include "cups-private.h"
ef416fc2 44
45
46/*
47 * Local functions...
48 */
49
5a738aea 50#ifdef DEBUG
ba55dc12 51static void ppd_debug_marked(ppd_file_t *ppd, const char *title);
5a738aea 52#else
ba55dc12 53# define ppd_debug_marked(ppd,title)
5a738aea 54#endif /* DEBUG */
ef416fc2 55static void ppd_defaults(ppd_file_t *ppd, ppd_group_t *g);
66ab9486
MS
56static void ppd_mark_choices(ppd_file_t *ppd, const char *s);
57static void ppd_mark_option(ppd_file_t *ppd, const char *option,
58 const char *choice);
ef416fc2 59
60
61/*
5a738aea
MS
62 * 'cupsMarkOptions()' - Mark command-line options in a PPD file.
63 *
64 * This function maps the IPP "finishings", "media", "mirror",
f14324a7
MS
65 * "multiple-document-handling", "output-bin", "print-color-mode",
66 * "print-quality", "printer-resolution", and "sides" attributes to their
67 * corresponding PPD options and choices.
5a738aea
MS
68 */
69
66ab9486 70int /* O - 1 if conflicts exist, 0 otherwise */
5a738aea
MS
71cupsMarkOptions(
72 ppd_file_t *ppd, /* I - PPD file */
73 int num_options, /* I - Number of options */
74 cups_option_t *options) /* I - Options */
75{
c7017ecc 76 int i, j; /* Looping vars */
d2354e63 77 char *ptr, /* Pointer into string */
5a738aea 78 s[255]; /* Temporary string */
d2354e63
MS
79 const char *val, /* Pointer into value */
80 *media, /* media option */
0268488e 81 *output_bin, /* output-bin option */
54afec33 82 *page_size, /* PageSize option */
0268488e 83 *ppd_keyword, /* PPD keyword */
f14324a7 84 *print_color_mode, /* print-color-mode option */
0268488e
MS
85 *print_quality, /* print-quality option */
86 *sides; /* sides option */
5a738aea 87 cups_option_t *optptr; /* Current option */
5a738aea 88 ppd_attr_t *attr; /* PPD attribute */
f14324a7 89 _ppd_cache_t *cache; /* PPD cache and mapping data */
5a738aea
MS
90
91
92 /*
93 * Check arguments...
94 */
95
96 if (!ppd || num_options <= 0 || !options)
97 return (0);
98
ba55dc12 99 ppd_debug_marked(ppd, "Before...");
5a738aea
MS
100
101 /*
0268488e
MS
102 * Do special handling for finishings, media, output-bin, output-mode,
103 * print-color-mode, print-quality, and PageSize...
5a738aea
MS
104 */
105
0268488e
MS
106 media = cupsGetOption("media", num_options, options);
107 output_bin = cupsGetOption("output-bin", num_options, options);
0268488e
MS
108 page_size = cupsGetOption("PageSize", num_options, options);
109 print_quality = cupsGetOption("print-quality", num_options, options);
110 sides = cupsGetOption("sides", num_options, options);
d2354e63 111
f14324a7
MS
112 if ((print_color_mode = cupsGetOption("print-color-mode", num_options,
113 options)) == NULL)
114 print_color_mode = cupsGetOption("output-mode", num_options, options);
115
116 if ((media || output_bin || print_color_mode || print_quality || sides) &&
117 !ppd->cache)
d2354e63
MS
118 {
119 /*
f14324a7 120 * Load PPD cache and mapping data as needed...
d2354e63
MS
121 */
122
f14324a7 123 ppd->cache = _ppdCacheCreateWithPPD(ppd);
0268488e
MS
124 }
125
f14324a7 126 cache = ppd->cache;
5a738aea 127
0268488e
MS
128 if (media)
129 {
d2354e63 130 /*
0268488e
MS
131 * Loop through the option string, separating it at commas and marking each
132 * individual option as long as the corresponding PPD option (PageSize,
133 * InputSlot, etc.) is not also set.
d2354e63 134 *
0268488e
MS
135 * For PageSize, we also check for an empty option value since some versions
136 * of MacOS X use it to specify auto-selection of the media based solely on
137 * the size.
d2354e63 138 */
5a738aea 139
d2354e63
MS
140 for (val = media; *val;)
141 {
142 /*
143 * Extract the sub-option from the string...
144 */
5a738aea 145
d2354e63
MS
146 for (ptr = s; *val && *val != ',' && (ptr - s) < (sizeof(s) - 1);)
147 *ptr++ = *val++;
148 *ptr++ = '\0';
149
150 if (*val == ',')
151 val ++;
152
153 /*
154 * Mark it...
155 */
156
cc754834
MS
157 if (!page_size || !page_size[0])
158 {
159 if (!strncasecmp(s, "Custom.", 7) || ppdPageSize(ppd, s))
160 ppd_mark_option(ppd, "PageSize", s);
f14324a7 161 else if ((ppd_keyword = _ppdCacheGetPageSize(cache, NULL, s, NULL)) != NULL)
cc754834
MS
162 ppd_mark_option(ppd, "PageSize", ppd_keyword);
163 }
d2354e63 164
f14324a7
MS
165 if (cache && cache->source_option &&
166 !cupsGetOption(cache->source_option, num_options, options) &&
167 (ppd_keyword = _ppdCacheGetInputSlot(cache, NULL, s)) != NULL)
168 ppd_mark_option(ppd, cache->source_option, ppd_keyword);
d2354e63 169
54afec33 170 if (!cupsGetOption("MediaType", num_options, options) &&
f14324a7 171 (ppd_keyword = _ppdCacheGetMediaType(cache, NULL, s)) != NULL)
54afec33 172 ppd_mark_option(ppd, "MediaType", ppd_keyword);
5a738aea 173 }
d2354e63
MS
174 }
175
f14324a7 176 if (cache)
0268488e
MS
177 {
178 if (!cupsGetOption("com.apple.print.DocumentTicket.PMSpoolFormat",
179 num_options, options) &&
180 !cupsGetOption("APPrinterPreset", num_options, options) &&
f14324a7 181 (print_color_mode || print_quality))
0268488e
MS
182 {
183 /*
184 * Map output-mode and print-quality to a preset...
185 */
186
f14324a7 187 _pwg_print_color_mode_t pwg_pcm;/* print-color-mode index */
0268488e
MS
188 _pwg_print_quality_t pwg_pq; /* print-quality index */
189 cups_option_t *preset;/* Current preset option */
190
f14324a7
MS
191 if (print_color_mode && !strcmp(print_color_mode, "monochrome"))
192 pwg_pcm = _PWG_PRINT_COLOR_MODE_MONOCHROME;
0268488e 193 else
f14324a7 194 pwg_pcm = _PWG_PRINT_COLOR_MODE_COLOR;
0268488e
MS
195
196 if (print_quality)
197 {
198 pwg_pq = atoi(print_quality) - IPP_QUALITY_DRAFT;
199 if (pwg_pq < _PWG_PRINT_QUALITY_DRAFT)
200 pwg_pq = _PWG_PRINT_QUALITY_DRAFT;
201 else if (pwg_pq > _PWG_PRINT_QUALITY_HIGH)
202 pwg_pq = _PWG_PRINT_QUALITY_HIGH;
203 }
204 else
205 pwg_pq = _PWG_PRINT_QUALITY_NORMAL;
206
f14324a7 207 if (cache->num_presets[pwg_pcm][pwg_pq] == 0)
0268488e
MS
208 {
209 /*
210 * Try to find a preset that works so that we maximize the chances of us
211 * getting a good print using IPP attributes.
212 */
213
f14324a7 214 if (cache->num_presets[pwg_pcm][_PWG_PRINT_QUALITY_NORMAL] > 0)
0268488e 215 pwg_pq = _PWG_PRINT_QUALITY_NORMAL;
f14324a7
MS
216 else if (cache->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][pwg_pq] > 0)
217 pwg_pcm = _PWG_PRINT_COLOR_MODE_COLOR;
0268488e
MS
218 else
219 {
f14324a7
MS
220 pwg_pq = _PWG_PRINT_QUALITY_NORMAL;
221 pwg_pcm = _PWG_PRINT_COLOR_MODE_COLOR;
0268488e
MS
222 }
223 }
224
f14324a7 225 if (cache->num_presets[pwg_pcm][pwg_pq] > 0)
0268488e
MS
226 {
227 /*
228 * Copy the preset options as long as the corresponding names are not
229 * already defined in the IPP request...
230 */
231
f14324a7
MS
232 for (i = cache->num_presets[pwg_pcm][pwg_pq],
233 preset = cache->presets[pwg_pcm][pwg_pq];
0268488e
MS
234 i > 0;
235 i --, preset ++)
236 {
237 if (!cupsGetOption(preset->name, num_options, options))
238 ppd_mark_option(ppd, preset->name, preset->value);
239 }
240 }
241 }
242
243 if (output_bin && !cupsGetOption("OutputBin", num_options, options) &&
f14324a7 244 (ppd_keyword = _ppdCacheGetOutputBin(cache, output_bin)) != NULL)
0268488e
MS
245 {
246 /*
247 * Map output-bin to OutputBin...
248 */
249
250 ppd_mark_option(ppd, "OutputBin", ppd_keyword);
251 }
252
f14324a7
MS
253 if (sides && cache->sides_option &&
254 !cupsGetOption(cache->sides_option, num_options, options))
0268488e
MS
255 {
256 /*
257 * Map sides to duplex option...
258 */
259
260 if (!strcmp(sides, "one-sided"))
f14324a7 261 ppd_mark_option(ppd, cache->sides_option, cache->sides_1sided);
0268488e 262 else if (!strcmp(sides, "two-sided-long-edge"))
f14324a7 263 ppd_mark_option(ppd, cache->sides_option, cache->sides_2sided_long);
0268488e 264 else if (!strcmp(sides, "two-sided-short-edge"))
f14324a7 265 ppd_mark_option(ppd, cache->sides_option, cache->sides_2sided_short);
0268488e
MS
266 }
267 }
268
d2354e63
MS
269 /*
270 * Mark other options...
271 */
272
273 for (i = num_options, optptr = options; i > 0; i --, optptr ++)
c7017ecc
MS
274 if (!strcasecmp(optptr->name, "media") ||
275 !strcasecmp(optptr->name, "output-bin") ||
276 !strcasecmp(optptr->name, "output-mode") ||
0268488e 277 !strcasecmp(optptr->name, "print-quality") ||
c7017ecc 278 !strcasecmp(optptr->name, "sides"))
d2354e63 279 continue;
5a738aea
MS
280 else if (!strcasecmp(optptr->name, "resolution") ||
281 !strcasecmp(optptr->name, "printer-resolution"))
282 {
66ab9486
MS
283 ppd_mark_option(ppd, "Resolution", optptr->value);
284 ppd_mark_option(ppd, "SetResolution", optptr->value);
5a738aea 285 /* Calcomp, Linotype, QMS, Summagraphics, Tektronix, Varityper */
66ab9486
MS
286 ppd_mark_option(ppd, "JCLResolution", optptr->value);
287 /* HP */
288 ppd_mark_option(ppd, "CNRes_PGP", optptr->value);
289 /* Canon */
5a738aea 290 }
5a738aea
MS
291 else if (!strcasecmp(optptr->name, "multiple-document-handling"))
292 {
293 if (!cupsGetOption("Collate", num_options, options) &&
294 ppdFindOption(ppd, "Collate"))
295 {
296 if (strcasecmp(optptr->value, "separate-documents-uncollated-copies"))
66ab9486 297 ppd_mark_option(ppd, "Collate", "True");
5a738aea 298 else
66ab9486 299 ppd_mark_option(ppd, "Collate", "False");
5a738aea
MS
300 }
301 }
302 else if (!strcasecmp(optptr->name, "finishings"))
303 {
304 /*
305 * Lookup cupsIPPFinishings attributes for each value...
306 */
307
308 for (ptr = optptr->value; *ptr;)
309 {
310 /*
311 * Get the next finishings number...
312 */
313
314 if (!isdigit(*ptr & 255))
315 break;
316
317 if ((j = strtol(ptr, &ptr, 10)) < 3)
318 break;
319
320 /*
321 * Skip separator as needed...
322 */
323
324 if (*ptr == ',')
325 ptr ++;
326
327 /*
328 * Look it up in the PPD file...
329 */
330
331 sprintf(s, "%d", j);
332
333 if ((attr = ppdFindAttr(ppd, "cupsIPPFinishings", s)) == NULL)
334 continue;
335
336 /*
337 * Apply "*Option Choice" settings from the attribute value...
338 */
339
66ab9486 340 ppd_mark_choices(ppd, attr->value);
5a738aea
MS
341 }
342 }
66ab9486 343 else if (!strcasecmp(optptr->name, "APPrinterPreset"))
ef416fc2 344 {
345 /*
66ab9486 346 * Lookup APPrinterPreset value...
ef416fc2 347 */
348
66ab9486
MS
349 if ((attr = ppdFindAttr(ppd, "APPrinterPreset", optptr->value)) != NULL)
350 {
351 /*
352 * Apply "*Option Choice" settings from the attribute value...
353 */
ef416fc2 354
66ab9486
MS
355 ppd_mark_choices(ppd, attr->value);
356 }
ef416fc2 357 }
66ab9486
MS
358 else if (!strcasecmp(optptr->name, "mirror"))
359 ppd_mark_option(ppd, "MirrorPrint", optptr->value);
360 else
361 ppd_mark_option(ppd, optptr->name, optptr->value);
355e94dc 362
ba55dc12 363 ppd_debug_marked(ppd, "After...");
ef416fc2 364
66ab9486 365 return (ppdConflicts(ppd) > 0);
ef416fc2 366}
367
368
369/*
370 * 'ppdFindChoice()' - Return a pointer to an option choice.
371 */
372
5a738aea 373ppd_choice_t * /* O - Choice pointer or @code NULL@ */
ef416fc2 374ppdFindChoice(ppd_option_t *o, /* I - Pointer to option */
375 const char *choice) /* I - Name of choice */
376{
b94498cf 377 int i; /* Looping var */
378 ppd_choice_t *c; /* Current choice */
ef416fc2 379
380
bdd6c45b 381 if (!o || !choice)
ef416fc2 382 return (NULL);
383
bdd6c45b
MS
384 if (choice[0] == '{' || !strncasecmp(choice, "Custom.", 7))
385 choice = "Custom";
386
ef416fc2 387 for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
bdd6c45b 388 if (!strcasecmp(c->choice, choice))
ef416fc2 389 return (c);
390
391 return (NULL);
392}
393
394
395/*
396 * 'ppdFindMarkedChoice()' - Return the marked choice for the specified option.
397 */
398
5a738aea 399ppd_choice_t * /* O - Pointer to choice or @code NULL@ */
ef416fc2 400ppdFindMarkedChoice(ppd_file_t *ppd, /* I - PPD file */
401 const char *option) /* I - Keyword/option name */
402{
d1c13e16
MS
403 ppd_choice_t key, /* Search key for choice */
404 *marked; /* Marked choice */
ef416fc2 405
406
e07d4801 407 DEBUG_printf(("2ppdFindMarkedChoice(ppd=%p, option=\"%s\")", ppd, option));
d1c13e16 408
b94498cf 409 if ((key.option = ppdFindOption(ppd, option)) == NULL)
d1c13e16 410 {
e07d4801 411 DEBUG_puts("3ppdFindMarkedChoice: Option not found, returning NULL");
ef416fc2 412 return (NULL);
d1c13e16
MS
413 }
414
415 marked = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key);
416
e07d4801 417 DEBUG_printf(("3ppdFindMarkedChoice: Returning %p(%s)...", marked,
d1c13e16 418 marked ? marked->choice : "NULL"));
ef416fc2 419
d1c13e16 420 return (marked);
ef416fc2 421}
422
423
424/*
425 * 'ppdFindOption()' - Return a pointer to the specified option.
426 */
427
5a738aea 428ppd_option_t * /* O - Pointer to option or @code NULL@ */
ef416fc2 429ppdFindOption(ppd_file_t *ppd, /* I - PPD file data */
430 const char *option) /* I - Option/Keyword name */
431{
fa73b229 432 /*
433 * Range check input...
434 */
ef416fc2 435
fa73b229 436 if (!ppd || !option)
ef416fc2 437 return (NULL);
438
f301802f 439 if (ppd->options)
440 {
441 /*
442 * Search in the array...
443 */
444
445 ppd_option_t key; /* Option search key */
446
447
448 strlcpy(key.keyword, option, sizeof(key.keyword));
ef416fc2 449
f301802f 450 return ((ppd_option_t *)cupsArrayFind(ppd->options, &key));
451 }
452 else
453 {
454 /*
455 * Search in each group...
456 */
ef416fc2 457
f301802f 458 int i, j; /* Looping vars */
459 ppd_group_t *group; /* Current group */
460 ppd_option_t *optptr; /* Current option */
461
462
463 for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
464 for (j = group->num_options, optptr = group->options;
465 j > 0;
466 j --, optptr ++)
467 if (!strcasecmp(optptr->keyword, option))
468 return (optptr);
469
470 return (NULL);
471 }
ef416fc2 472}
473
474
475/*
5a738aea 476 * 'ppdIsMarked()' - Check to see if an option is marked.
ef416fc2 477 */
478
b94498cf 479int /* O - Non-zero if option is marked */
480ppdIsMarked(ppd_file_t *ppd, /* I - PPD file data */
481 const char *option, /* I - Option/Keyword name */
482 const char *choice) /* I - Choice name */
ef416fc2 483{
b94498cf 484 ppd_choice_t key, /* Search key */
485 *c; /* Choice pointer */
ef416fc2 486
487
b94498cf 488 if (!ppd)
ef416fc2 489 return (0);
490
b94498cf 491 if ((key.option = ppdFindOption(ppd, option)) == NULL)
ef416fc2 492 return (0);
493
b94498cf 494 if ((c = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) == NULL)
ef416fc2 495 return (0);
496
b94498cf 497 return (!strcmp(c->choice, choice));
ef416fc2 498}
499
500
501/*
502 * 'ppdMarkDefaults()' - Mark all default options in the PPD file.
503 */
504
505void
b94498cf 506ppdMarkDefaults(ppd_file_t *ppd) /* I - PPD file record */
ef416fc2 507{
b94498cf 508 int i; /* Looping variables */
509 ppd_group_t *g; /* Current group */
510 ppd_choice_t *c; /* Current choice */
ef416fc2 511
512
b94498cf 513 if (!ppd)
ef416fc2 514 return;
515
b94498cf 516 /*
517 * Clean out the marked array...
518 */
519
520 for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked);
521 c;
522 c = (ppd_choice_t *)cupsArrayNext(ppd->marked))
7cf5915e 523 {
b94498cf 524 cupsArrayRemove(ppd->marked, c);
7cf5915e
MS
525 c->marked = 0;
526 }
b94498cf 527
528 /*
529 * Then repopulate it with the defaults...
530 */
531
ef416fc2 532 for (i = ppd->num_groups, g = ppd->groups; i > 0; i --, g ++)
533 ppd_defaults(ppd, g);
534}
535
536
537/*
66ab9486
MS
538 * 'ppdMarkOption()' - Mark an option in a PPD file and return the number of
539 * conflicts.
ef416fc2 540 */
541
542int /* O - Number of conflicts */
543ppdMarkOption(ppd_file_t *ppd, /* I - PPD file record */
544 const char *option, /* I - Keyword */
545 const char *choice) /* I - Option name */
546{
e07d4801 547 DEBUG_printf(("ppdMarkOption(ppd=%p, option=\"%s\", choice=\"%s\")",
bd7854cb 548 ppd, option, choice));
549
fa73b229 550 /*
551 * Range check input...
552 */
553
554 if (!ppd || !option || !choice)
ef416fc2 555 return (0);
556
66ab9486
MS
557 /*
558 * Mark the option...
559 */
560
561 ppd_mark_option(ppd, option, choice);
562
563 /*
564 * Return the number of conflicts...
565 */
566
567 return (ppdConflicts(ppd));
568}
569
570
571/*
572 * 'ppdFirstOption()' - Return the first option in the PPD file.
573 *
574 * Options are returned from all groups in ascending alphanumeric order.
575 *
426c6a59 576 * @since CUPS 1.2/Mac OS X 10.5@
66ab9486
MS
577 */
578
579ppd_option_t * /* O - First option or @code NULL@ */
580ppdFirstOption(ppd_file_t *ppd) /* I - PPD file */
581{
582 if (!ppd)
583 return (NULL);
584 else
585 return ((ppd_option_t *)cupsArrayFirst(ppd->options));
586}
587
588
589/*
590 * 'ppdNextOption()' - Return the next option in the PPD file.
591 *
592 * Options are returned from all groups in ascending alphanumeric order.
593 *
426c6a59 594 * @since CUPS 1.2/Mac OS X 10.5@
66ab9486
MS
595 */
596
597ppd_option_t * /* O - Next option or @code NULL@ */
598ppdNextOption(ppd_file_t *ppd) /* I - PPD file */
599{
600 if (!ppd)
601 return (NULL);
602 else
603 return ((ppd_option_t *)cupsArrayNext(ppd->options));
604}
605
606
607/*
608 * '_ppdParseOptions()' - Parse options from a PPD file.
609 *
610 * This function looks for strings of the form:
611 *
612 * *option choice ... *optionN choiceN
c7017ecc 613 * property value ... propertyN valueN
66ab9486
MS
614 *
615 * It stops when it finds a string that doesn't match this format.
616 */
617
618int /* O - Number of options */
619_ppdParseOptions(
620 const char *s, /* I - String to parse */
621 int num_options, /* I - Number of options */
c7017ecc
MS
622 cups_option_t **options, /* IO - Options */
623 _ppd_parse_t which) /* I - What to parse */
66ab9486 624{
7cf5915e 625 char option[PPD_MAX_NAME * 2 + 1], /* Current option/property */
c7017ecc 626 choice[PPD_MAX_NAME], /* Current choice/value */
66ab9486
MS
627 *ptr; /* Pointer into option or choice */
628
629
630 if (!s)
631 return (num_options);
632
633 /*
c7017ecc
MS
634 * Read all of the "*Option Choice" and "property value" pairs from the
635 * string, add them to an options array as we go...
66ab9486
MS
636 */
637
638 while (*s)
639 {
640 /*
641 * Skip leading whitespace...
642 */
643
7cf5915e 644 while (_cups_isspace(*s))
66ab9486
MS
645 s ++;
646
66ab9486 647 /*
c7017ecc 648 * Get the option/property name...
66ab9486
MS
649 */
650
66ab9486 651 ptr = option;
7cf5915e 652 while (*s && !_cups_isspace(*s) && ptr < (option + sizeof(option) - 1))
66ab9486
MS
653 *ptr++ = *s++;
654
7cf5915e 655 if (ptr == s || !_cups_isspace(*s))
66ab9486
MS
656 break;
657
658 *ptr = '\0';
659
660 /*
661 * Get the choice...
662 */
663
7cf5915e 664 while (_cups_isspace(*s))
66ab9486
MS
665 s ++;
666
667 if (!*s)
668 break;
669
670 ptr = choice;
7cf5915e 671 while (*s && !_cups_isspace(*s) && ptr < (choice + sizeof(choice) - 1))
66ab9486
MS
672 *ptr++ = *s++;
673
7cf5915e 674 if (*s && !_cups_isspace(*s))
c7017ecc
MS
675 break;
676
66ab9486
MS
677 *ptr = '\0';
678
679 /*
680 * Add it to the options array...
681 */
682
c7017ecc
MS
683 if (option[0] == '*' && which != _PPD_PARSE_PROPERTIES)
684 num_options = cupsAddOption(option + 1, choice, num_options, options);
685 else if (option[0] != '*' && which != _PPD_PARSE_OPTIONS)
686 num_options = cupsAddOption(option, choice, num_options, options);
66ab9486
MS
687 }
688
689 return (num_options);
690}
691
692
693#ifdef DEBUG
694/*
ba55dc12 695 * 'ppd_debug_marked()' - Output the marked array to stdout...
66ab9486
MS
696 */
697
698static void
ba55dc12 699ppd_debug_marked(ppd_file_t *ppd, /* I - PPD file data */
66ab9486
MS
700 const char *title) /* I - Title for list */
701{
702 ppd_choice_t *c; /* Current choice */
703
704
e07d4801 705 DEBUG_printf(("2cupsMarkOptions: %s", title));
66ab9486
MS
706
707 for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked);
708 c;
709 c = (ppd_choice_t *)cupsArrayNext(ppd->marked))
e07d4801 710 DEBUG_printf(("2cupsMarkOptions: %s=%s", c->option->keyword, c->choice));
66ab9486
MS
711}
712#endif /* DEBUG */
713
714
715/*
716 * 'ppd_defaults()' - Set the defaults for this group and all sub-groups.
717 */
718
719static void
720ppd_defaults(ppd_file_t *ppd, /* I - PPD file */
721 ppd_group_t *g) /* I - Group to default */
722{
723 int i; /* Looping var */
724 ppd_option_t *o; /* Current option */
725 ppd_group_t *sg; /* Current sub-group */
726
727
728 for (i = g->num_options, o = g->options; i > 0; i --, o ++)
729 if (strcasecmp(o->keyword, "PageRegion") != 0)
730 ppdMarkOption(ppd, o->keyword, o->defchoice);
731
732 for (i = g->num_subgroups, sg = g->subgroups; i > 0; i --, sg ++)
733 ppd_defaults(ppd, sg);
734}
735
736
737/*
738 * 'ppd_mark_choices()' - Mark one or more option choices from a string.
739 */
740
741static void
742ppd_mark_choices(ppd_file_t *ppd, /* I - PPD file */
743 const char *s) /* I - "*Option Choice ..." string */
744{
745 int i, /* Looping var */
746 num_options; /* Number of options */
747 cups_option_t *options, /* Options */
748 *option; /* Current option */
749
750
5d6412a9 751 if (!s)
66ab9486
MS
752 return;
753
754 options = NULL;
c7017ecc 755 num_options = _ppdParseOptions(s, 0, &options, 0);
66ab9486
MS
756
757 for (i = num_options, option = options; i > 0; i --, option ++)
758 ppd_mark_option(ppd, option->name, option->value);
759
760 cupsFreeOptions(num_options, options);
761}
762
763
764/*
765 * 'ppd_mark_option()' - Quick mark an option without checking for conflicts.
766 */
767
768static void
769ppd_mark_option(ppd_file_t *ppd, /* I - PPD file */
770 const char *option, /* I - Option name */
771 const char *choice) /* I - Choice name */
772{
773 int i, j; /* Looping vars */
774 ppd_option_t *o; /* Option pointer */
775 ppd_choice_t *c, /* Choice pointer */
776 *oldc, /* Old choice pointer */
777 key; /* Search key for choice */
778 struct lconv *loc; /* Locale data */
779
780
e07d4801 781 DEBUG_printf(("7ppd_mark_option(ppd=%p, option=\"%s\", choice=\"%s\")",
66ab9486
MS
782 ppd, option, choice));
783
fa73b229 784 /*
785 * AP_D_InputSlot is the "default input slot" on MacOS X, and setting
786 * it clears the regular InputSlot choices...
787 */
ef416fc2 788
fa73b229 789 if (!strcasecmp(option, "AP_D_InputSlot"))
790 {
ef55b745
MS
791 cupsArraySave(ppd->options);
792
fa73b229 793 if ((o = ppdFindOption(ppd, "InputSlot")) != NULL)
b94498cf 794 {
795 key.option = o;
796 if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
797 {
798 oldc->marked = 0;
799 cupsArrayRemove(ppd->marked, oldc);
800 }
801 }
ef55b745
MS
802
803 cupsArrayRestore(ppd->options);
ef416fc2 804 }
805
fa73b229 806 /*
807 * Check for custom options...
808 */
809
ef55b745
MS
810 cupsArraySave(ppd->options);
811
812 o = ppdFindOption(ppd, option);
813
814 cupsArrayRestore(ppd->options);
815
816 if (!o)
66ab9486 817 return;
ef416fc2 818
757d2cad 819 loc = localeconv();
ef416fc2 820
b423cd4c 821 if (!strncasecmp(choice, "Custom.", 7))
ef416fc2 822 {
823 /*
fa73b229 824 * Handle a custom option...
ef416fc2 825 */
826
fa73b229 827 if ((c = ppdFindChoice(o, "Custom")) == NULL)
66ab9486 828 return;
ef416fc2 829
fa73b229 830 if (!strcasecmp(option, "PageSize"))
831 {
832 /*
833 * Handle custom page sizes...
834 */
ef416fc2 835
fa73b229 836 ppdPageSize(ppd, choice);
837 }
838 else
ef416fc2 839 {
840 /*
fa73b229 841 * Handle other custom options...
ef416fc2 842 */
843
fa73b229 844 ppd_coption_t *coption; /* Custom option */
845 ppd_cparam_t *cparam; /* Custom parameter */
757d2cad 846 char *units; /* Custom points units */
fa73b229 847
8ca02f3c 848
fa73b229 849 if ((coption = ppdFindCustomOption(ppd, option)) != NULL)
ef416fc2 850 {
fa73b229 851 if ((cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params)) == NULL)
66ab9486 852 return;
fa73b229 853
854 switch (cparam->type)
855 {
856 case PPD_CUSTOM_CURVE :
857 case PPD_CUSTOM_INVCURVE :
858 case PPD_CUSTOM_REAL :
b86bc4cf 859 cparam->current.custom_real = (float)_cupsStrScand(choice + 7,
860 NULL, loc);
fa73b229 861 break;
862
863 case PPD_CUSTOM_POINTS :
b86bc4cf 864 cparam->current.custom_points = (float)_cupsStrScand(choice + 7,
865 &units,
866 loc);
757d2cad 867
868 if (units)
869 {
870 if (!strcasecmp(units, "cm"))
ef55b745 871 cparam->current.custom_points *= 72.0f / 2.54f;
757d2cad 872 else if (!strcasecmp(units, "mm"))
ef55b745 873 cparam->current.custom_points *= 72.0f / 25.4f;
757d2cad 874 else if (!strcasecmp(units, "m"))
ef55b745 875 cparam->current.custom_points *= 72.0f / 0.0254f;
757d2cad 876 else if (!strcasecmp(units, "in"))
ef55b745 877 cparam->current.custom_points *= 72.0f;
757d2cad 878 else if (!strcasecmp(units, "ft"))
ef55b745 879 cparam->current.custom_points *= 12.0f * 72.0f;
757d2cad 880 }
fa73b229 881 break;
882
883 case PPD_CUSTOM_INT :
884 cparam->current.custom_int = atoi(choice + 7);
885 break;
886
887 case PPD_CUSTOM_PASSCODE :
888 case PPD_CUSTOM_PASSWORD :
889 case PPD_CUSTOM_STRING :
890 if (cparam->current.custom_string)
2e4ff8af 891 _cupsStrFree(cparam->current.custom_string);
fa73b229 892
2e4ff8af 893 cparam->current.custom_string = _cupsStrAlloc(choice + 7);
fa73b229 894 break;
895 }
ef416fc2 896 }
897 }
8ca02f3c 898
899 /*
900 * Make sure that we keep the option marked below...
901 */
902
903 choice = "Custom";
fa73b229 904 }
b423cd4c 905 else if (choice[0] == '{')
906 {
907 /*
908 * Handle multi-value custom options...
909 */
910
911 ppd_coption_t *coption; /* Custom option */
912 ppd_cparam_t *cparam; /* Custom parameter */
757d2cad 913 char *units; /* Custom points units */
b423cd4c 914 int num_vals; /* Number of values */
915 cups_option_t *vals, /* Values */
916 *val; /* Value */
917
918
919 if ((c = ppdFindChoice(o, "Custom")) == NULL)
66ab9486 920 return;
b423cd4c 921
922 if ((coption = ppdFindCustomOption(ppd, option)) != NULL)
923 {
ee571f26 924 num_vals = cupsParseOptions(choice, 0, &vals);
b423cd4c 925
926 for (i = 0, val = vals; i < num_vals; i ++, val ++)
927 {
928 if ((cparam = ppdFindCustomParam(coption, val->name)) == NULL)
929 continue;
930
931 switch (cparam->type)
932 {
933 case PPD_CUSTOM_CURVE :
934 case PPD_CUSTOM_INVCURVE :
935 case PPD_CUSTOM_REAL :
b86bc4cf 936 cparam->current.custom_real = (float)_cupsStrScand(val->value,
937 NULL, loc);
b423cd4c 938 break;
939
940 case PPD_CUSTOM_POINTS :
b86bc4cf 941 cparam->current.custom_points = (float)_cupsStrScand(val->value,
942 &units,
943 loc);
757d2cad 944
945 if (units)
946 {
947 if (!strcasecmp(units, "cm"))
b86bc4cf 948 cparam->current.custom_points *= 72.0f / 2.54f;
757d2cad 949 else if (!strcasecmp(units, "mm"))
b86bc4cf 950 cparam->current.custom_points *= 72.0f / 25.4f;
757d2cad 951 else if (!strcasecmp(units, "m"))
b86bc4cf 952 cparam->current.custom_points *= 72.0f / 0.0254f;
757d2cad 953 else if (!strcasecmp(units, "in"))
b86bc4cf 954 cparam->current.custom_points *= 72.0f;
757d2cad 955 else if (!strcasecmp(units, "ft"))
b86bc4cf 956 cparam->current.custom_points *= 12.0f * 72.0f;
757d2cad 957 }
b423cd4c 958 break;
959
960 case PPD_CUSTOM_INT :
961 cparam->current.custom_int = atoi(val->value);
962 break;
963
964 case PPD_CUSTOM_PASSCODE :
965 case PPD_CUSTOM_PASSWORD :
966 case PPD_CUSTOM_STRING :
967 if (cparam->current.custom_string)
2e4ff8af 968 _cupsStrFree(cparam->current.custom_string);
b423cd4c 969
426c6a59 970 cparam->current.custom_string = _cupsStrRetain(val->value);
b423cd4c 971 break;
972 }
973 }
974
975 cupsFreeOptions(num_vals, vals);
976 }
977 }
fa73b229 978 else
979 {
980 for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
981 if (!strcasecmp(c->choice, choice))
982 break;
ef416fc2 983
fa73b229 984 if (!i)
66ab9486 985 return;
fa73b229 986 }
ef416fc2 987
fa73b229 988 /*
989 * Option found; mark it and then handle unmarking any other options.
990 */
991
fa73b229 992 if (o->ui != PPD_UI_PICKMANY)
993 {
994 /*
995 * Unmark all other choices...
996 */
997
b94498cf 998 if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, c)) != NULL)
999 {
1000 oldc->marked = 0;
1001 cupsArrayRemove(ppd->marked, oldc);
1002 }
1003
1004 if (!strcasecmp(option, "PageSize") || !strcasecmp(option, "PageRegion"))
1005 {
1006 /*
1007 * Mark current page size...
1008 */
1009
1010 for (j = 0; j < ppd->num_sizes; j ++)
1011 ppd->sizes[j].marked = !strcasecmp(ppd->sizes[j].name,
1012 choice);
1013
1014 /*
1015 * Unmark the current PageSize or PageRegion setting, as
1016 * appropriate...
1017 */
1018
ef55b745
MS
1019 cupsArraySave(ppd->options);
1020
b94498cf 1021 if (!strcasecmp(option, "PageSize"))
fa73b229 1022 {
b94498cf 1023 if ((o = ppdFindOption(ppd, "PageRegion")) != NULL)
1024 {
1025 key.option = o;
1026 if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
1027 {
1028 oldc->marked = 0;
1029 cupsArrayRemove(ppd->marked, oldc);
1030 }
1031 }
1032 }
1033 else
1034 {
1035 if ((o = ppdFindOption(ppd, "PageSize")) != NULL)
1036 {
1037 key.option = o;
1038 if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
1039 {
1040 oldc->marked = 0;
1041 cupsArrayRemove(ppd->marked, oldc);
1042 }
1043 }
1044 }
ef55b745
MS
1045
1046 cupsArrayRestore(ppd->options);
b94498cf 1047 }
1048 else if (!strcasecmp(option, "InputSlot"))
1049 {
1050 /*
355e94dc 1051 * Unmark ManualFeed option...
b94498cf 1052 */
fa73b229 1053
ef55b745
MS
1054 cupsArraySave(ppd->options);
1055
b94498cf 1056 if ((o = ppdFindOption(ppd, "ManualFeed")) != NULL)
1057 {
1058 key.option = o;
1059 if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
1060 {
1061 oldc->marked = 0;
1062 cupsArrayRemove(ppd->marked, oldc);
1063 }
1064 }
ef55b745
MS
1065
1066 cupsArrayRestore(ppd->options);
b94498cf 1067 }
1068 else if (!strcasecmp(option, "ManualFeed") &&
1069 !strcasecmp(choice, "True"))
1070 {
1071 /*
1072 * Unmark InputSlot option...
1073 */
fa73b229 1074
ef55b745
MS
1075 cupsArraySave(ppd->options);
1076
b94498cf 1077 if ((o = ppdFindOption(ppd, "InputSlot")) != NULL)
1078 {
1079 key.option = o;
1080 if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
1081 {
1082 oldc->marked = 0;
1083 cupsArrayRemove(ppd->marked, oldc);
1084 }
fa73b229 1085 }
ef55b745
MS
1086
1087 cupsArrayRestore(ppd->options);
b94498cf 1088 }
ef416fc2 1089 }
1090
b94498cf 1091 c->marked = 1;
1092
1093 cupsArrayAdd(ppd->marked, c);
5a738aea
MS
1094}
1095
1096
1097/*
71e16022 1098 * End of "$Id: mark.c 9042 2010-03-24 00:45:34Z mike $".
ef416fc2 1099 */