2 * Option conflict management routines for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * PostScript is a trademark of Adobe Systems, Inc.
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
22 #include "cups-private.h"
23 #include "ppd-private.h"
32 _PPD_NORMAL_CONSTRAINTS
,
33 _PPD_OPTION_CONSTRAINTS
,
34 _PPD_INSTALLABLE_CONSTRAINTS
,
43 static int ppd_is_installable(ppd_group_t
*installable
,
45 static void ppd_load_constraints(ppd_file_t
*ppd
);
46 static cups_array_t
*ppd_test_constraints(ppd_file_t
*ppd
,
50 cups_option_t
*options
,
55 * 'cupsGetConflicts()' - Get a list of conflicting options in a marked PPD.
57 * This function gets a list of options that would conflict if "option" and
58 * "choice" were marked in the PPD. You would typically call this function
59 * after marking the currently selected options in the PPD in order to
60 * determine whether a new option selection would cause a conflict.
62 * The number of conflicting options are returned with "options" pointing to
63 * the conflicting options. The returned option array must be freed using
64 * @link cupsFreeOptions@.
66 * @since CUPS 1.4/macOS 10.6@
69 int /* O - Number of conflicting options */
71 ppd_file_t
*ppd
, /* I - PPD file */
72 const char *option
, /* I - Option to test */
73 const char *choice
, /* I - Choice to test */
74 cups_option_t
**options
) /* O - Conflicting options */
76 int i
, /* Looping var */
77 num_options
; /* Number of conflicting options */
78 cups_array_t
*active
; /* Active conflicts */
79 _ppd_cups_uiconsts_t
*c
; /* Current constraints */
80 _ppd_cups_uiconst_t
*cptr
; /* Current constraint */
81 ppd_choice_t
*marked
; /* Marked choice */
85 * Range check input...
91 if (!ppd
|| !option
|| !choice
|| !options
)
95 * Test for conflicts...
98 active
= ppd_test_constraints(ppd
, option
, choice
, 0, NULL
,
99 _PPD_ALL_CONSTRAINTS
);
102 * Loop through all of the UI constraints and add any options that conflict...
105 for (num_options
= 0, c
= (_ppd_cups_uiconsts_t
*)cupsArrayFirst(active
);
107 c
= (_ppd_cups_uiconsts_t
*)cupsArrayNext(active
))
109 for (i
= c
->num_constraints
, cptr
= c
->constraints
;
112 if (_cups_strcasecmp(cptr
->option
->keyword
, option
))
115 num_options
= cupsAddOption(cptr
->option
->keyword
,
116 cptr
->choice
->choice
, num_options
,
118 else if ((marked
= ppdFindMarkedChoice(ppd
,
119 cptr
->option
->keyword
)) != NULL
)
120 num_options
= cupsAddOption(cptr
->option
->keyword
, marked
->choice
,
121 num_options
, options
);
125 cupsArrayDelete(active
);
127 return (num_options
);
132 * 'cupsResolveConflicts()' - Resolve conflicts in a marked PPD.
134 * This function attempts to resolve any conflicts in a marked PPD, returning
135 * a list of option changes that are required to resolve them. On input,
136 * "num_options" and "options" contain any pending option changes that have
137 * not yet been marked, while "option" and "choice" contain the most recent
138 * selection which may or may not be in "num_options" or "options".
140 * On successful return, "num_options" and "options" are updated to contain
141 * "option" and "choice" along with any changes required to resolve conflicts
142 * specified in the PPD file and 1 is returned.
144 * If option conflicts cannot be resolved, "num_options" and "options" are not
145 * changed and 0 is returned.
147 * When resolving conflicts, @code cupsResolveConflicts@ does not consider
148 * changes to the current page size (@code media@, @code PageSize@, and
149 * @code PageRegion@) or to the most recent option specified in "option".
150 * Thus, if the only way to resolve a conflict is to change the page size
151 * or the option the user most recently changed, @code cupsResolveConflicts@
152 * will return 0 to indicate it was unable to resolve the conflicts.
154 * The @code cupsResolveConflicts@ function uses one of two sources of option
155 * constraint information. The preferred constraint information is defined by
156 * @code cupsUIConstraints@ and @code cupsUIResolver@ attributes - in this
157 * case, the PPD file provides constraint resolution actions.
159 * The backup constraint information is defined by the
160 * @code UIConstraints@ and @code NonUIConstraints@ attributes. These
161 * constraints are resolved algorithmically by first selecting the default
162 * choice for the conflicting option, then iterating over all possible choices
163 * until a non-conflicting option choice is found.
165 * @since CUPS 1.4/macOS 10.6@
168 int /* O - 1 on success, 0 on failure */
169 cupsResolveConflicts(
170 ppd_file_t
*ppd
, /* I - PPD file */
171 const char *option
, /* I - Newly selected option or @code NULL@ for none */
172 const char *choice
, /* I - Newly selected choice or @code NULL@ for none */
173 int *num_options
, /* IO - Number of additional selected options */
174 cups_option_t
**options
) /* IO - Additional selected options */
176 int i
, /* Looping var */
177 tries
, /* Number of tries */
178 num_newopts
; /* Number of new options */
179 cups_option_t
*newopts
; /* New options */
180 cups_array_t
*active
= NULL
, /* Active constraints */
181 *pass
, /* Resolvers for this pass */
182 *resolvers
, /* Resolvers we have used */
183 *test
; /* Test array for conflicts */
184 _ppd_cups_uiconsts_t
*consts
; /* Current constraints */
185 _ppd_cups_uiconst_t
*constptr
; /* Current constraint */
186 ppd_attr_t
*resolver
; /* Current resolver */
187 const char *resval
; /* Pointer into resolver value */
188 char resoption
[PPD_MAX_NAME
],
189 /* Current resolver option */
190 reschoice
[PPD_MAX_NAME
],
191 /* Current resolver choice */
192 *resptr
, /* Pointer into option/choice */
193 firstpage
[255]; /* AP_FIRSTPAGE_Keyword string */
194 const char *value
; /* Selected option value */
195 int changed
; /* Did we change anything? */
196 ppd_choice_t
*marked
; /* Marked choice */
200 * Range check input...
203 if (!ppd
|| !num_options
|| !options
|| (option
== NULL
) != (choice
== NULL
))
207 * Build a shadow option array...
213 for (i
= 0; i
< *num_options
; i
++)
214 num_newopts
= cupsAddOption((*options
)[i
].name
, (*options
)[i
].value
,
215 num_newopts
, &newopts
);
216 if (option
&& _cups_strcasecmp(option
, "Collate"))
217 num_newopts
= cupsAddOption(option
, choice
, num_newopts
, &newopts
);
220 * Loop until we have no conflicts...
223 cupsArraySave(ppd
->sorted_attrs
);
226 pass
= cupsArrayNew((cups_array_func_t
)_cups_strcasecmp
, NULL
);
229 while (tries
< 100 &&
230 (active
= ppd_test_constraints(ppd
, NULL
, NULL
, num_newopts
, newopts
,
231 _PPD_ALL_CONSTRAINTS
)) != NULL
)
236 resolvers
= cupsArrayNew((cups_array_func_t
)_cups_strcasecmp
, NULL
);
238 for (consts
= (_ppd_cups_uiconsts_t
*)cupsArrayFirst(active
), changed
= 0;
240 consts
= (_ppd_cups_uiconsts_t
*)cupsArrayNext(active
))
242 if (consts
->resolver
[0])
245 * Look up the resolver...
248 if (cupsArrayFind(pass
, consts
->resolver
))
249 continue; /* Already applied this resolver... */
251 if (cupsArrayFind(resolvers
, consts
->resolver
))
257 DEBUG_printf(("1cupsResolveConflicts: Resolver loop with %s!",
262 if ((resolver
= ppdFindAttr(ppd
, "cupsUIResolver",
263 consts
->resolver
)) == NULL
)
265 DEBUG_printf(("1cupsResolveConflicts: Resolver %s not found!",
270 if (!resolver
->value
)
272 DEBUG_printf(("1cupsResolveConflicts: Resolver %s has no value!",
278 * Add the options from the resolver...
281 cupsArrayAdd(pass
, consts
->resolver
);
282 cupsArrayAdd(resolvers
, consts
->resolver
);
284 for (resval
= resolver
->value
; *resval
&& !changed
;)
286 while (_cups_isspace(*resval
))
292 for (resval
++, resptr
= resoption
;
293 *resval
&& !_cups_isspace(*resval
);
295 if (resptr
< (resoption
+ sizeof(resoption
) - 1))
300 while (_cups_isspace(*resval
))
303 for (resptr
= reschoice
;
304 *resval
&& !_cups_isspace(*resval
);
306 if (resptr
< (reschoice
+ sizeof(reschoice
) - 1))
311 if (!resoption
[0] || !reschoice
[0])
315 * Is this the option we are changing?
318 snprintf(firstpage
, sizeof(firstpage
), "AP_FIRSTPAGE_%s", resoption
);
321 (!_cups_strcasecmp(resoption
, option
) ||
322 !_cups_strcasecmp(firstpage
, option
) ||
323 (!_cups_strcasecmp(option
, "PageSize") &&
324 !_cups_strcasecmp(resoption
, "PageRegion")) ||
325 (!_cups_strcasecmp(option
, "AP_FIRSTPAGE_PageSize") &&
326 !_cups_strcasecmp(resoption
, "PageSize")) ||
327 (!_cups_strcasecmp(option
, "AP_FIRSTPAGE_PageSize") &&
328 !_cups_strcasecmp(resoption
, "PageRegion")) ||
329 (!_cups_strcasecmp(option
, "PageRegion") &&
330 !_cups_strcasecmp(resoption
, "PageSize")) ||
331 (!_cups_strcasecmp(option
, "AP_FIRSTPAGE_PageRegion") &&
332 !_cups_strcasecmp(resoption
, "PageSize")) ||
333 (!_cups_strcasecmp(option
, "AP_FIRSTPAGE_PageRegion") &&
334 !_cups_strcasecmp(resoption
, "PageRegion"))))
341 if ((test
= ppd_test_constraints(ppd
, resoption
, reschoice
,
342 num_newopts
, newopts
,
343 _PPD_ALL_CONSTRAINTS
)) == NULL
)
352 cupsArrayDelete(test
);
355 * Add the option/choice from the resolver regardless of whether it
356 * worked; this makes sure that we can cascade several changes to
357 * make things resolve...
360 num_newopts
= cupsAddOption(resoption
, reschoice
, num_newopts
,
367 * Try resolving by choosing the default values for non-installable
368 * options, then by iterating through the possible choices...
371 int j
; /* Looping var */
372 ppd_choice_t
*cptr
; /* Current choice */
373 ppd_size_t
*size
; /* Current page size */
376 for (i
= consts
->num_constraints
, constptr
= consts
->constraints
;
381 * Can't resolve by changing an installable option...
384 if (constptr
->installable
)
388 * Is this the option we are changing?
392 (!_cups_strcasecmp(constptr
->option
->keyword
, option
) ||
393 (!_cups_strcasecmp(option
, "PageSize") &&
394 !_cups_strcasecmp(constptr
->option
->keyword
, "PageRegion")) ||
395 (!_cups_strcasecmp(option
, "PageRegion") &&
396 !_cups_strcasecmp(constptr
->option
->keyword
, "PageSize"))))
400 * Get the current option choice...
403 if ((value
= cupsGetOption(constptr
->option
->keyword
, num_newopts
,
406 if (!_cups_strcasecmp(constptr
->option
->keyword
, "PageSize") ||
407 !_cups_strcasecmp(constptr
->option
->keyword
, "PageRegion"))
409 if ((value
= cupsGetOption("PageSize", num_newopts
,
411 value
= cupsGetOption("PageRegion", num_newopts
, newopts
);
415 if ((size
= ppdPageSize(ppd
, NULL
)) != NULL
)
423 marked
= ppdFindMarkedChoice(ppd
, constptr
->option
->keyword
);
424 value
= marked
? marked
->choice
: "";
428 if (!_cups_strncasecmp(value
, "Custom.", 7))
432 * Try the default choice...
437 if (_cups_strcasecmp(value
, constptr
->option
->defchoice
) &&
438 (test
= ppd_test_constraints(ppd
, constptr
->option
->keyword
,
439 constptr
->option
->defchoice
,
440 num_newopts
, newopts
,
441 _PPD_OPTION_CONSTRAINTS
)) == NULL
)
447 num_newopts
= cupsAddOption(constptr
->option
->keyword
,
448 constptr
->option
->defchoice
,
449 num_newopts
, &newopts
);
455 * Try each choice instead...
458 for (j
= constptr
->option
->num_choices
,
459 cptr
= constptr
->option
->choices
;
463 cupsArrayDelete(test
);
466 if (_cups_strcasecmp(value
, cptr
->choice
) &&
467 _cups_strcasecmp(constptr
->option
->defchoice
, cptr
->choice
) &&
468 _cups_strcasecmp("Custom", cptr
->choice
) &&
469 (test
= ppd_test_constraints(ppd
, constptr
->option
->keyword
,
470 cptr
->choice
, num_newopts
,
472 _PPD_OPTION_CONSTRAINTS
)) == NULL
)
475 * This choice works...
478 num_newopts
= cupsAddOption(constptr
->option
->keyword
,
479 cptr
->choice
, num_newopts
,
486 cupsArrayDelete(test
);
494 DEBUG_puts("1cupsResolveConflicts: Unable to automatically resolve "
499 cupsArrayClear(pass
);
500 cupsArrayDelete(active
);
508 * Free the caller's option array...
511 cupsFreeOptions(*num_options
, *options
);
514 * If Collate is the option we are testing, add it here. Otherwise, remove
515 * any Collate option from the resolve list since the filters automatically
516 * handle manual collation...
519 if (option
&& !_cups_strcasecmp(option
, "Collate"))
520 num_newopts
= cupsAddOption(option
, choice
, num_newopts
, &newopts
);
522 num_newopts
= cupsRemoveOption("Collate", num_newopts
, &newopts
);
525 * Return the new list of options to the caller...
528 *num_options
= num_newopts
;
531 cupsArrayDelete(pass
);
532 cupsArrayDelete(resolvers
);
534 cupsArrayRestore(ppd
->sorted_attrs
);
536 DEBUG_printf(("1cupsResolveConflicts: Returning %d options:", num_newopts
));
538 for (i
= 0; i
< num_newopts
; i
++)
539 DEBUG_printf(("1cupsResolveConflicts: options[%d]: %s=%s", i
,
540 newopts
[i
].name
, newopts
[i
].value
));
546 * If we get here, we failed to resolve...
551 cupsFreeOptions(num_newopts
, newopts
);
553 cupsArrayDelete(active
);
554 cupsArrayDelete(pass
);
555 cupsArrayDelete(resolvers
);
557 cupsArrayRestore(ppd
->sorted_attrs
);
559 DEBUG_puts("1cupsResolveConflicts: Unable to resolve conflicts!");
566 * 'ppdConflicts()' - Check to see if there are any conflicts among the
567 * marked option choices.
569 * The returned value is the same as returned by @link ppdMarkOption@.
572 int /* O - Number of conflicts found */
573 ppdConflicts(ppd_file_t
*ppd
) /* I - PPD to check */
575 int i
, /* Looping variable */
576 conflicts
; /* Number of conflicts */
577 cups_array_t
*active
; /* Active conflicts */
578 _ppd_cups_uiconsts_t
*c
; /* Current constraints */
579 _ppd_cups_uiconst_t
*cptr
; /* Current constraint */
580 ppd_option_t
*o
; /* Current option */
587 * Clear all conflicts...
590 cupsArraySave(ppd
->options
);
592 for (o
= ppdFirstOption(ppd
); o
; o
= ppdNextOption(ppd
))
595 cupsArrayRestore(ppd
->options
);
598 * Test for conflicts...
601 active
= ppd_test_constraints(ppd
, NULL
, NULL
, 0, NULL
,
602 _PPD_ALL_CONSTRAINTS
);
603 conflicts
= cupsArrayCount(active
);
606 * Loop through all of the UI constraints and flag any options
610 for (c
= (_ppd_cups_uiconsts_t
*)cupsArrayFirst(active
);
612 c
= (_ppd_cups_uiconsts_t
*)cupsArrayNext(active
))
614 for (i
= c
->num_constraints
, cptr
= c
->constraints
;
617 cptr
->option
->conflicted
= 1;
620 cupsArrayDelete(active
);
623 * Return the number of conflicts found...
631 * 'ppdInstallableConflict()' - Test whether an option choice conflicts with
632 * an installable option.
634 * This function tests whether a particular option choice is available based
635 * on constraints against options in the "InstallableOptions" group.
637 * @since CUPS 1.4/macOS 10.6@
640 int /* O - 1 if conflicting, 0 if not conflicting */
641 ppdInstallableConflict(
642 ppd_file_t
*ppd
, /* I - PPD file */
643 const char *option
, /* I - Option */
644 const char *choice
) /* I - Choice */
646 cups_array_t
*active
; /* Active conflicts */
649 DEBUG_printf(("2ppdInstallableConflict(ppd=%p, option=\"%s\", choice=\"%s\")",
650 ppd
, option
, choice
));
653 * Range check input...
656 if (!ppd
|| !option
|| !choice
)
660 * Test constraints using the new option...
663 active
= ppd_test_constraints(ppd
, option
, choice
, 0, NULL
,
664 _PPD_INSTALLABLE_CONSTRAINTS
);
666 cupsArrayDelete(active
);
668 return (active
!= NULL
);
673 * 'ppd_is_installable()' - Determine whether an option is in the
674 * InstallableOptions group.
677 static int /* O - 1 if installable, 0 if normal */
679 ppd_group_t
*installable
, /* I - InstallableOptions group */
680 const char *name
) /* I - Option name */
684 int i
; /* Looping var */
685 ppd_option_t
*option
; /* Current option */
688 for (i
= installable
->num_options
, option
= installable
->options
;
691 if (!_cups_strcasecmp(option
->keyword
, name
))
700 * 'ppd_load_constraints()' - Load constraints from a PPD file.
704 ppd_load_constraints(ppd_file_t
*ppd
) /* I - PPD file */
706 int i
; /* Looping var */
707 ppd_const_t
*oldconst
; /* Current UIConstraints data */
708 ppd_attr_t
*constattr
; /* Current cupsUIConstraints attribute */
709 _ppd_cups_uiconsts_t
*consts
; /* Current cupsUIConstraints data */
710 _ppd_cups_uiconst_t
*constptr
; /* Current constraint */
711 ppd_group_t
*installable
; /* Installable options group */
712 const char *vptr
; /* Pointer into constraint value */
713 char option
[PPD_MAX_NAME
], /* Option name/MainKeyword */
714 choice
[PPD_MAX_NAME
], /* Choice/OptionKeyword */
715 *ptr
; /* Pointer into option or choice */
718 DEBUG_printf(("7ppd_load_constraints(ppd=%p)", ppd
));
721 * Create an array to hold the constraint data...
724 ppd
->cups_uiconstraints
= cupsArrayNew(NULL
, NULL
);
727 * Find the installable options group if it exists...
730 for (i
= ppd
->num_groups
, installable
= ppd
->groups
;
732 i
--, installable
++)
733 if (!_cups_strcasecmp(installable
->name
, "InstallableOptions"))
740 * Load old-style [Non]UIConstraints data...
743 for (i
= ppd
->num_consts
, oldconst
= ppd
->consts
; i
> 0; i
--, oldconst
++)
746 * Weed out nearby duplicates, since the PPD spec requires that you
747 * define both "*Foo foo *Bar bar" and "*Bar bar *Foo foo"...
751 !_cups_strcasecmp(oldconst
[0].option1
, oldconst
[1].option2
) &&
752 !_cups_strcasecmp(oldconst
[0].choice1
, oldconst
[1].choice2
) &&
753 !_cups_strcasecmp(oldconst
[0].option2
, oldconst
[1].option1
) &&
754 !_cups_strcasecmp(oldconst
[0].choice2
, oldconst
[1].choice1
))
761 if ((consts
= calloc(1, sizeof(_ppd_cups_uiconsts_t
))) == NULL
)
763 DEBUG_puts("8ppd_load_constraints: Unable to allocate memory for "
768 if ((constptr
= calloc(2, sizeof(_ppd_cups_uiconst_t
))) == NULL
)
771 DEBUG_puts("8ppd_load_constraints: Unable to allocate memory for "
777 * Fill in the information...
780 consts
->num_constraints
= 2;
781 consts
->constraints
= constptr
;
783 if (!_cups_strncasecmp(oldconst
->option1
, "Custom", 6) &&
784 !_cups_strcasecmp(oldconst
->choice1
, "True"))
786 constptr
[0].option
= ppdFindOption(ppd
, oldconst
->option1
+ 6);
787 constptr
[0].choice
= ppdFindChoice(constptr
[0].option
, "Custom");
788 constptr
[0].installable
= 0;
792 constptr
[0].option
= ppdFindOption(ppd
, oldconst
->option1
);
793 constptr
[0].choice
= ppdFindChoice(constptr
[0].option
,
795 constptr
[0].installable
= ppd_is_installable(installable
,
799 if (!constptr
[0].option
|| (!constptr
[0].choice
&& oldconst
->choice1
[0]))
801 DEBUG_printf(("8ppd_load_constraints: Unknown option *%s %s!",
802 oldconst
->option1
, oldconst
->choice1
));
803 free(consts
->constraints
);
808 if (!_cups_strncasecmp(oldconst
->option2
, "Custom", 6) &&
809 !_cups_strcasecmp(oldconst
->choice2
, "True"))
811 constptr
[1].option
= ppdFindOption(ppd
, oldconst
->option2
+ 6);
812 constptr
[1].choice
= ppdFindChoice(constptr
[1].option
, "Custom");
813 constptr
[1].installable
= 0;
817 constptr
[1].option
= ppdFindOption(ppd
, oldconst
->option2
);
818 constptr
[1].choice
= ppdFindChoice(constptr
[1].option
,
820 constptr
[1].installable
= ppd_is_installable(installable
,
824 if (!constptr
[1].option
|| (!constptr
[1].choice
&& oldconst
->choice2
[0]))
826 DEBUG_printf(("8ppd_load_constraints: Unknown option *%s %s!",
827 oldconst
->option2
, oldconst
->choice2
));
828 free(consts
->constraints
);
833 consts
->installable
= constptr
[0].installable
|| constptr
[1].installable
;
836 * Add it to the constraints array...
839 cupsArrayAdd(ppd
->cups_uiconstraints
, consts
);
843 * Then load new-style constraints...
846 for (constattr
= ppdFindAttr(ppd
, "cupsUIConstraints", NULL
);
848 constattr
= ppdFindNextAttr(ppd
, "cupsUIConstraints", NULL
))
850 if (!constattr
->value
)
852 DEBUG_puts("8ppd_load_constraints: Bad cupsUIConstraints value!");
856 for (i
= 0, vptr
= strchr(constattr
->value
, '*');
858 i
++, vptr
= strchr(vptr
+ 1, '*'));
862 DEBUG_puts("8ppd_load_constraints: Bad cupsUIConstraints value!");
866 if ((consts
= calloc(1, sizeof(_ppd_cups_uiconsts_t
))) == NULL
)
868 DEBUG_puts("8ppd_load_constraints: Unable to allocate memory for "
869 "cupsUIConstraints!");
873 if ((constptr
= calloc((size_t)i
, sizeof(_ppd_cups_uiconst_t
))) == NULL
)
876 DEBUG_puts("8ppd_load_constraints: Unable to allocate memory for "
877 "cupsUIConstraints!");
881 consts
->num_constraints
= i
;
882 consts
->constraints
= constptr
;
884 strlcpy(consts
->resolver
, constattr
->spec
, sizeof(consts
->resolver
));
886 for (i
= 0, vptr
= strchr(constattr
->value
, '*');
888 i
++, vptr
= strchr(vptr
, '*'), constptr
++)
891 * Extract "*Option Choice" or just "*Option"...
894 for (vptr
++, ptr
= option
; *vptr
&& !_cups_isspace(*vptr
); vptr
++)
895 if (ptr
< (option
+ sizeof(option
) - 1))
900 while (_cups_isspace(*vptr
))
907 for (ptr
= choice
; *vptr
&& !_cups_isspace(*vptr
); vptr
++)
908 if (ptr
< (choice
+ sizeof(choice
) - 1))
914 if (!_cups_strncasecmp(option
, "Custom", 6) && !_cups_strcasecmp(choice
, "True"))
916 _cups_strcpy(option
, option
+ 6);
917 strlcpy(choice
, "Custom", sizeof(choice
));
920 constptr
->option
= ppdFindOption(ppd
, option
);
921 constptr
->choice
= ppdFindChoice(constptr
->option
, choice
);
922 constptr
->installable
= ppd_is_installable(installable
, option
);
923 consts
->installable
|= constptr
->installable
;
925 if (!constptr
->option
|| (!constptr
->choice
&& choice
[0]))
927 DEBUG_printf(("8ppd_load_constraints: Unknown option *%s %s!",
934 cupsArrayAdd(ppd
->cups_uiconstraints
, consts
);
937 free(consts
->constraints
);
945 * 'ppd_test_constraints()' - See if any constraints are active.
948 static cups_array_t
* /* O - Array of active constraints */
949 ppd_test_constraints(
950 ppd_file_t
*ppd
, /* I - PPD file */
951 const char *option
, /* I - Current option */
952 const char *choice
, /* I - Current choice */
953 int num_options
, /* I - Number of additional options */
954 cups_option_t
*options
, /* I - Additional options */
955 int which
) /* I - Which constraints to test */
957 int i
; /* Looping var */
958 _ppd_cups_uiconsts_t
*consts
; /* Current constraints */
959 _ppd_cups_uiconst_t
*constptr
; /* Current constraint */
960 ppd_choice_t key
, /* Search key */
961 *marked
; /* Marked choice */
962 cups_array_t
*active
= NULL
; /* Active constraints */
963 const char *value
, /* Current value */
964 *firstvalue
; /* AP_FIRSTPAGE_Keyword value */
965 char firstpage
[255]; /* AP_FIRSTPAGE_Keyword string */
968 DEBUG_printf(("7ppd_test_constraints(ppd=%p, option=\"%s\", choice=\"%s\", "
969 "num_options=%d, options=%p, which=%d)", ppd
, option
, choice
,
970 num_options
, options
, which
));
972 if (!ppd
->cups_uiconstraints
)
973 ppd_load_constraints(ppd
);
975 DEBUG_printf(("9ppd_test_constraints: %d constraints!",
976 cupsArrayCount(ppd
->cups_uiconstraints
)));
978 cupsArraySave(ppd
->marked
);
980 for (consts
= (_ppd_cups_uiconsts_t
*)cupsArrayFirst(ppd
->cups_uiconstraints
);
982 consts
= (_ppd_cups_uiconsts_t
*)cupsArrayNext(ppd
->cups_uiconstraints
))
984 DEBUG_printf(("9ppd_test_constraints: installable=%d, resolver=\"%s\", "
985 "num_constraints=%d option1=\"%s\", choice1=\"%s\", "
986 "option2=\"%s\", choice2=\"%s\", ...",
987 consts
->installable
, consts
->resolver
, consts
->num_constraints
,
988 consts
->constraints
[0].option
->keyword
,
989 consts
->constraints
[0].choice
?
990 consts
->constraints
[0].choice
->choice
: "",
991 consts
->constraints
[1].option
->keyword
,
992 consts
->constraints
[1].choice
?
993 consts
->constraints
[1].choice
->choice
: ""));
995 if (consts
->installable
&& which
< _PPD_INSTALLABLE_CONSTRAINTS
)
996 continue; /* Skip installable option constraint */
998 if (!consts
->installable
&& which
== _PPD_INSTALLABLE_CONSTRAINTS
)
999 continue; /* Skip non-installable option constraint */
1001 if (which
== _PPD_OPTION_CONSTRAINTS
&& option
)
1004 * Skip constraints that do not involve the current option...
1007 for (i
= consts
->num_constraints
, constptr
= consts
->constraints
;
1011 if (!_cups_strcasecmp(constptr
->option
->keyword
, option
))
1014 if (!_cups_strncasecmp(option
, "AP_FIRSTPAGE_", 13) &&
1015 !_cups_strcasecmp(constptr
->option
->keyword
, option
+ 13))
1023 DEBUG_puts("9ppd_test_constraints: Testing...");
1025 for (i
= consts
->num_constraints
, constptr
= consts
->constraints
;
1029 DEBUG_printf(("9ppd_test_constraints: %s=%s?", constptr
->option
->keyword
,
1030 constptr
->choice
? constptr
->choice
->choice
: ""));
1032 if (constptr
->choice
&&
1033 (!_cups_strcasecmp(constptr
->option
->keyword
, "PageSize") ||
1034 !_cups_strcasecmp(constptr
->option
->keyword
, "PageRegion")))
1037 * PageSize and PageRegion are used depending on the selected input slot
1038 * and manual feed mode. Validate against the selected page size instead
1039 * of an individual option...
1042 if (option
&& choice
&&
1043 (!_cups_strcasecmp(option
, "PageSize") ||
1044 !_cups_strcasecmp(option
, "PageRegion")))
1048 else if ((value
= cupsGetOption("PageSize", num_options
,
1050 if ((value
= cupsGetOption("PageRegion", num_options
,
1052 if ((value
= cupsGetOption("media", num_options
, options
)) == NULL
)
1054 ppd_size_t
*size
= ppdPageSize(ppd
, NULL
);
1060 if (value
&& !_cups_strncasecmp(value
, "Custom.", 7))
1063 if (option
&& choice
&&
1064 (!_cups_strcasecmp(option
, "AP_FIRSTPAGE_PageSize") ||
1065 !_cups_strcasecmp(option
, "AP_FIRSTPAGE_PageRegion")))
1067 firstvalue
= choice
;
1069 else if ((firstvalue
= cupsGetOption("AP_FIRSTPAGE_PageSize",
1070 num_options
, options
)) == NULL
)
1071 firstvalue
= cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options
,
1074 if (firstvalue
&& !_cups_strncasecmp(firstvalue
, "Custom.", 7))
1075 firstvalue
= "Custom";
1077 if ((!value
|| _cups_strcasecmp(value
, constptr
->choice
->choice
)) &&
1078 (!firstvalue
|| _cups_strcasecmp(firstvalue
, constptr
->choice
->choice
)))
1080 DEBUG_puts("9ppd_test_constraints: NO");
1084 else if (constptr
->choice
)
1087 * Compare against the constrained choice...
1090 if (option
&& choice
&& !_cups_strcasecmp(option
, constptr
->option
->keyword
))
1092 if (!_cups_strncasecmp(choice
, "Custom.", 7))
1097 else if ((value
= cupsGetOption(constptr
->option
->keyword
, num_options
,
1100 if (!_cups_strncasecmp(value
, "Custom.", 7))
1103 else if (constptr
->choice
->marked
)
1104 value
= constptr
->choice
->choice
;
1109 * Now check AP_FIRSTPAGE_option...
1112 snprintf(firstpage
, sizeof(firstpage
), "AP_FIRSTPAGE_%s",
1113 constptr
->option
->keyword
);
1115 if (option
&& choice
&& !_cups_strcasecmp(option
, firstpage
))
1117 if (!_cups_strncasecmp(choice
, "Custom.", 7))
1118 firstvalue
= "Custom";
1120 firstvalue
= choice
;
1122 else if ((firstvalue
= cupsGetOption(firstpage
, num_options
,
1125 if (!_cups_strncasecmp(firstvalue
, "Custom.", 7))
1126 firstvalue
= "Custom";
1131 DEBUG_printf(("9ppd_test_constraints: value=%s, firstvalue=%s", value
,
1134 if ((!value
|| _cups_strcasecmp(value
, constptr
->choice
->choice
)) &&
1135 (!firstvalue
|| _cups_strcasecmp(firstvalue
, constptr
->choice
->choice
)))
1137 DEBUG_puts("9ppd_test_constraints: NO");
1141 else if (option
&& choice
&&
1142 !_cups_strcasecmp(option
, constptr
->option
->keyword
))
1144 if (!_cups_strcasecmp(choice
, "None") || !_cups_strcasecmp(choice
, "Off") ||
1145 !_cups_strcasecmp(choice
, "False"))
1147 DEBUG_puts("9ppd_test_constraints: NO");
1151 else if ((value
= cupsGetOption(constptr
->option
->keyword
, num_options
,
1154 if (!_cups_strcasecmp(value
, "None") || !_cups_strcasecmp(value
, "Off") ||
1155 !_cups_strcasecmp(value
, "False"))
1157 DEBUG_puts("9ppd_test_constraints: NO");
1163 key
.option
= constptr
->option
;
1165 if ((marked
= (ppd_choice_t
*)cupsArrayFind(ppd
->marked
, &key
))
1167 (!_cups_strcasecmp(marked
->choice
, "None") ||
1168 !_cups_strcasecmp(marked
->choice
, "Off") ||
1169 !_cups_strcasecmp(marked
->choice
, "False")))
1171 DEBUG_puts("9ppd_test_constraints: NO");
1180 active
= cupsArrayNew(NULL
, NULL
);
1182 cupsArrayAdd(active
, consts
);
1183 DEBUG_puts("9ppd_test_constraints: Added...");
1187 cupsArrayRestore(ppd
->marked
);
1189 DEBUG_printf(("8ppd_test_constraints: Found %d active constraints!",
1190 cupsArrayCount(active
)));