#include "cups-private.h"
#ifdef DEBUG
static void ppd_debug_marked(ppd_file_t *ppd, const char *title);
#else
# define ppd_debug_marked(ppd,title)
#endif
static void ppd_defaults(ppd_file_t *ppd, ppd_group_t *g);
static void ppd_mark_choices(ppd_file_t *ppd, const char *s);
static void ppd_mark_option(ppd_file_t *ppd, const char *option,
const char *choice);
int
cupsMarkOptions(
ppd_file_t *ppd,
int num_options,
cups_option_t *options)
{
int i, j;
char *ptr,
s[255];
const char *val,
*media,
*output_bin,
*page_size,
*ppd_keyword,
*print_color_mode,
*print_quality,
*sides;
cups_option_t *optptr;
ppd_attr_t *attr;
_ppd_cache_t *cache;
if (!ppd || num_options <= 0 || !options)
return (0);
ppd_debug_marked(ppd, "Before...");
media = cupsGetOption("media", num_options, options);
output_bin = cupsGetOption("output-bin", num_options, options);
page_size = cupsGetOption("PageSize", num_options, options);
print_quality = cupsGetOption("print-quality", num_options, options);
sides = cupsGetOption("sides", num_options, options);
if ((print_color_mode = cupsGetOption("print-color-mode", num_options,
options)) == NULL)
print_color_mode = cupsGetOption("output-mode", num_options, options);
if ((media || output_bin || print_color_mode || print_quality || sides) &&
!ppd->cache)
{
ppd->cache = _ppdCacheCreateWithPPD(ppd);
}
cache = ppd->cache;
if (media)
{
for (val = media; *val;)
{
for (ptr = s; *val && *val != ',' && (ptr - s) < (sizeof(s) - 1);)
*ptr++ = *val++;
*ptr++ = '\0';
if (*val == ',')
val ++;
if (!page_size || !page_size[0])
{
if (!_cups_strncasecmp(s, "Custom.", 7) || ppdPageSize(ppd, s))
ppd_mark_option(ppd, "PageSize", s);
else if ((ppd_keyword = _ppdCacheGetPageSize(cache, NULL, s, NULL)) != NULL)
ppd_mark_option(ppd, "PageSize", ppd_keyword);
}
if (cache && cache->source_option &&
!cupsGetOption(cache->source_option, num_options, options) &&
(ppd_keyword = _ppdCacheGetInputSlot(cache, NULL, s)) != NULL)
ppd_mark_option(ppd, cache->source_option, ppd_keyword);
if (!cupsGetOption("MediaType", num_options, options) &&
(ppd_keyword = _ppdCacheGetMediaType(cache, NULL, s)) != NULL)
ppd_mark_option(ppd, "MediaType", ppd_keyword);
}
}
if (cache)
{
if (!cupsGetOption("com.apple.print.DocumentTicket.PMSpoolFormat",
num_options, options) &&
!cupsGetOption("APPrinterPreset", num_options, options) &&
(print_color_mode || print_quality))
{
_pwg_print_color_mode_t pwg_pcm;
_pwg_print_quality_t pwg_pq;
cups_option_t *preset;
if (print_color_mode && !strcmp(print_color_mode, "monochrome"))
pwg_pcm = _PWG_PRINT_COLOR_MODE_MONOCHROME;
else
pwg_pcm = _PWG_PRINT_COLOR_MODE_COLOR;
if (print_quality)
{
pwg_pq = atoi(print_quality) - IPP_QUALITY_DRAFT;
if (pwg_pq < _PWG_PRINT_QUALITY_DRAFT)
pwg_pq = _PWG_PRINT_QUALITY_DRAFT;
else if (pwg_pq > _PWG_PRINT_QUALITY_HIGH)
pwg_pq = _PWG_PRINT_QUALITY_HIGH;
}
else
pwg_pq = _PWG_PRINT_QUALITY_NORMAL;
if (cache->num_presets[pwg_pcm][pwg_pq] == 0)
{
if (cache->num_presets[pwg_pcm][_PWG_PRINT_QUALITY_NORMAL] > 0)
pwg_pq = _PWG_PRINT_QUALITY_NORMAL;
else if (cache->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][pwg_pq] > 0)
pwg_pcm = _PWG_PRINT_COLOR_MODE_COLOR;
else
{
pwg_pq = _PWG_PRINT_QUALITY_NORMAL;
pwg_pcm = _PWG_PRINT_COLOR_MODE_COLOR;
}
}
if (cache->num_presets[pwg_pcm][pwg_pq] > 0)
{
for (i = cache->num_presets[pwg_pcm][pwg_pq],
preset = cache->presets[pwg_pcm][pwg_pq];
i > 0;
i --, preset ++)
{
if (!cupsGetOption(preset->name, num_options, options))
ppd_mark_option(ppd, preset->name, preset->value);
}
}
}
if (output_bin && !cupsGetOption("OutputBin", num_options, options) &&
(ppd_keyword = _ppdCacheGetOutputBin(cache, output_bin)) != NULL)
{
ppd_mark_option(ppd, "OutputBin", ppd_keyword);
}
if (sides && cache->sides_option &&
!cupsGetOption(cache->sides_option, num_options, options))
{
if (!strcmp(sides, "one-sided") && cache->sides_1sided)
ppd_mark_option(ppd, cache->sides_option, cache->sides_1sided);
else if (!strcmp(sides, "two-sided-long-edge") &&
cache->sides_2sided_long)
ppd_mark_option(ppd, cache->sides_option, cache->sides_2sided_long);
else if (!strcmp(sides, "two-sided-short-edge") &&
cache->sides_2sided_short)
ppd_mark_option(ppd, cache->sides_option, cache->sides_2sided_short);
}
}
for (i = num_options, optptr = options; i > 0; i --, optptr ++)
if (!_cups_strcasecmp(optptr->name, "media") ||
!_cups_strcasecmp(optptr->name, "output-bin") ||
!_cups_strcasecmp(optptr->name, "output-mode") ||
!_cups_strcasecmp(optptr->name, "print-quality") ||
!_cups_strcasecmp(optptr->name, "sides"))
continue;
else if (!_cups_strcasecmp(optptr->name, "resolution") ||
!_cups_strcasecmp(optptr->name, "printer-resolution"))
{
ppd_mark_option(ppd, "Resolution", optptr->value);
ppd_mark_option(ppd, "SetResolution", optptr->value);
ppd_mark_option(ppd, "JCLResolution", optptr->value);
ppd_mark_option(ppd, "CNRes_PGP", optptr->value);
}
else if (!_cups_strcasecmp(optptr->name, "multiple-document-handling"))
{
if (!cupsGetOption("Collate", num_options, options) &&
ppdFindOption(ppd, "Collate"))
{
if (_cups_strcasecmp(optptr->value, "separate-documents-uncollated-copies"))
ppd_mark_option(ppd, "Collate", "True");
else
ppd_mark_option(ppd, "Collate", "False");
}
}
else if (!_cups_strcasecmp(optptr->name, "finishings"))
{
for (ptr = optptr->value; *ptr;)
{
if (!isdigit(*ptr & 255))
break;
if ((j = strtol(ptr, &ptr, 10)) < 3)
break;
if (*ptr == ',')
ptr ++;
sprintf(s, "%d", j);
if ((attr = ppdFindAttr(ppd, "cupsIPPFinishings", s)) == NULL)
continue;
ppd_mark_choices(ppd, attr->value);
}
}
else if (!_cups_strcasecmp(optptr->name, "APPrinterPreset"))
{
if ((attr = ppdFindAttr(ppd, "APPrinterPreset", optptr->value)) != NULL)
{
ppd_mark_choices(ppd, attr->value);
}
}
else if (!_cups_strcasecmp(optptr->name, "mirror"))
ppd_mark_option(ppd, "MirrorPrint", optptr->value);
else
ppd_mark_option(ppd, optptr->name, optptr->value);
ppd_debug_marked(ppd, "After...");
return (ppdConflicts(ppd) > 0);
}
ppd_choice_t *
ppdFindChoice(ppd_option_t *o,
const char *choice)
{
int i;
ppd_choice_t *c;
if (!o || !choice)
return (NULL);
if (choice[0] == '{' || !_cups_strncasecmp(choice, "Custom.", 7))
choice = "Custom";
for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
if (!_cups_strcasecmp(c->choice, choice))
return (c);
return (NULL);
}
ppd_choice_t *
ppdFindMarkedChoice(ppd_file_t *ppd,
const char *option)
{
ppd_choice_t key,
*marked;
DEBUG_printf(("2ppdFindMarkedChoice(ppd=%p, option=\"%s\")", ppd, option));
if ((key.option = ppdFindOption(ppd, option)) == NULL)
{
DEBUG_puts("3ppdFindMarkedChoice: Option not found, returning NULL");
return (NULL);
}
marked = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key);
DEBUG_printf(("3ppdFindMarkedChoice: Returning %p(%s)...", marked,
marked ? marked->choice : "NULL"));
return (marked);
}
ppd_option_t *
ppdFindOption(ppd_file_t *ppd,
const char *option)
{
if (!ppd || !option)
return (NULL);
if (ppd->options)
{
ppd_option_t key;
strlcpy(key.keyword, option, sizeof(key.keyword));
return ((ppd_option_t *)cupsArrayFind(ppd->options, &key));
}
else
{
int i, j;
ppd_group_t *group;
ppd_option_t *optptr;
for (i = ppd->num_groups, group = ppd->groups; i > 0; i --, group ++)
for (j = group->num_options, optptr = group->options;
j > 0;
j --, optptr ++)
if (!_cups_strcasecmp(optptr->keyword, option))
return (optptr);
return (NULL);
}
}
int
ppdIsMarked(ppd_file_t *ppd,
const char *option,
const char *choice)
{
ppd_choice_t key,
*c;
if (!ppd)
return (0);
if ((key.option = ppdFindOption(ppd, option)) == NULL)
return (0);
if ((c = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) == NULL)
return (0);
return (!strcmp(c->choice, choice));
}
void
ppdMarkDefaults(ppd_file_t *ppd)
{
int i;
ppd_group_t *g;
ppd_choice_t *c;
if (!ppd)
return;
for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked);
c;
c = (ppd_choice_t *)cupsArrayNext(ppd->marked))
{
cupsArrayRemove(ppd->marked, c);
c->marked = 0;
}
for (i = ppd->num_groups, g = ppd->groups; i > 0; i --, g ++)
ppd_defaults(ppd, g);
}
int
ppdMarkOption(ppd_file_t *ppd,
const char *option,
const char *choice)
{
DEBUG_printf(("ppdMarkOption(ppd=%p, option=\"%s\", choice=\"%s\")",
ppd, option, choice));
if (!ppd || !option || !choice)
return (0);
ppd_mark_option(ppd, option, choice);
return (ppdConflicts(ppd));
}
ppd_option_t *
ppdFirstOption(ppd_file_t *ppd)
{
if (!ppd)
return (NULL);
else
return ((ppd_option_t *)cupsArrayFirst(ppd->options));
}
ppd_option_t *
ppdNextOption(ppd_file_t *ppd)
{
if (!ppd)
return (NULL);
else
return ((ppd_option_t *)cupsArrayNext(ppd->options));
}
int
_ppdParseOptions(
const char *s,
int num_options,
cups_option_t **options,
_ppd_parse_t which)
{
char option[PPD_MAX_NAME * 2 + 1],
choice[PPD_MAX_NAME],
*ptr;
if (!s)
return (num_options);
while (*s)
{
while (_cups_isspace(*s))
s ++;
ptr = option;
while (*s && !_cups_isspace(*s) && ptr < (option + sizeof(option) - 1))
*ptr++ = *s++;
if (ptr == s || !_cups_isspace(*s))
break;
*ptr = '\0';
while (_cups_isspace(*s))
s ++;
if (!*s)
break;
ptr = choice;
while (*s && !_cups_isspace(*s) && ptr < (choice + sizeof(choice) - 1))
*ptr++ = *s++;
if (*s && !_cups_isspace(*s))
break;
*ptr = '\0';
if (option[0] == '*' && which != _PPD_PARSE_PROPERTIES)
num_options = cupsAddOption(option + 1, choice, num_options, options);
else if (option[0] != '*' && which != _PPD_PARSE_OPTIONS)
num_options = cupsAddOption(option, choice, num_options, options);
}
return (num_options);
}
#ifdef DEBUG
static void
ppd_debug_marked(ppd_file_t *ppd,
const char *title)
{
ppd_choice_t *c;
DEBUG_printf(("2cupsMarkOptions: %s", title));
for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked);
c;
c = (ppd_choice_t *)cupsArrayNext(ppd->marked))
DEBUG_printf(("2cupsMarkOptions: %s=%s", c->option->keyword, c->choice));
}
#endif
static void
ppd_defaults(ppd_file_t *ppd,
ppd_group_t *g)
{
int i;
ppd_option_t *o;
ppd_group_t *sg;
for (i = g->num_options, o = g->options; i > 0; i --, o ++)
if (_cups_strcasecmp(o->keyword, "PageRegion") != 0)
ppdMarkOption(ppd, o->keyword, o->defchoice);
for (i = g->num_subgroups, sg = g->subgroups; i > 0; i --, sg ++)
ppd_defaults(ppd, sg);
}
static void
ppd_mark_choices(ppd_file_t *ppd,
const char *s)
{
int i,
num_options;
cups_option_t *options,
*option;
if (!s)
return;
options = NULL;
num_options = _ppdParseOptions(s, 0, &options, 0);
for (i = num_options, option = options; i > 0; i --, option ++)
ppd_mark_option(ppd, option->name, option->value);
cupsFreeOptions(num_options, options);
}
static void
ppd_mark_option(ppd_file_t *ppd,
const char *option,
const char *choice)
{
int i, j;
ppd_option_t *o;
ppd_choice_t *c,
*oldc,
key;
struct lconv *loc;
DEBUG_printf(("7ppd_mark_option(ppd=%p, option=\"%s\", choice=\"%s\")",
ppd, option, choice));
if (!_cups_strcasecmp(option, "AP_D_InputSlot"))
{
cupsArraySave(ppd->options);
if ((o = ppdFindOption(ppd, "InputSlot")) != NULL)
{
key.option = o;
if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
{
oldc->marked = 0;
cupsArrayRemove(ppd->marked, oldc);
}
}
cupsArrayRestore(ppd->options);
}
cupsArraySave(ppd->options);
o = ppdFindOption(ppd, option);
cupsArrayRestore(ppd->options);
if (!o)
return;
loc = localeconv();
if (!_cups_strncasecmp(choice, "Custom.", 7))
{
if ((c = ppdFindChoice(o, "Custom")) == NULL)
return;
if (!_cups_strcasecmp(option, "PageSize"))
{
ppdPageSize(ppd, choice);
}
else
{
ppd_coption_t *coption;
ppd_cparam_t *cparam;
char *units;
if ((coption = ppdFindCustomOption(ppd, option)) != NULL)
{
if ((cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params)) == NULL)
return;
switch (cparam->type)
{
case PPD_CUSTOM_CURVE :
case PPD_CUSTOM_INVCURVE :
case PPD_CUSTOM_REAL :
cparam->current.custom_real = (float)_cupsStrScand(choice + 7,
NULL, loc);
break;
case PPD_CUSTOM_POINTS :
cparam->current.custom_points = (float)_cupsStrScand(choice + 7,
&units,
loc);
if (units)
{
if (!_cups_strcasecmp(units, "cm"))
cparam->current.custom_points *= 72.0f / 2.54f;
else if (!_cups_strcasecmp(units, "mm"))
cparam->current.custom_points *= 72.0f / 25.4f;
else if (!_cups_strcasecmp(units, "m"))
cparam->current.custom_points *= 72.0f / 0.0254f;
else if (!_cups_strcasecmp(units, "in"))
cparam->current.custom_points *= 72.0f;
else if (!_cups_strcasecmp(units, "ft"))
cparam->current.custom_points *= 12.0f * 72.0f;
}
break;
case PPD_CUSTOM_INT :
cparam->current.custom_int = atoi(choice + 7);
break;
case PPD_CUSTOM_PASSCODE :
case PPD_CUSTOM_PASSWORD :
case PPD_CUSTOM_STRING :
if (cparam->current.custom_string)
_cupsStrFree(cparam->current.custom_string);
cparam->current.custom_string = _cupsStrAlloc(choice + 7);
break;
}
}
}
choice = "Custom";
}
else if (choice[0] == '{')
{
ppd_coption_t *coption;
ppd_cparam_t *cparam;
char *units;
int num_vals;
cups_option_t *vals,
*val;
if ((c = ppdFindChoice(o, "Custom")) == NULL)
return;
if ((coption = ppdFindCustomOption(ppd, option)) != NULL)
{
num_vals = cupsParseOptions(choice, 0, &vals);
for (i = 0, val = vals; i < num_vals; i ++, val ++)
{
if ((cparam = ppdFindCustomParam(coption, val->name)) == NULL)
continue;
switch (cparam->type)
{
case PPD_CUSTOM_CURVE :
case PPD_CUSTOM_INVCURVE :
case PPD_CUSTOM_REAL :
cparam->current.custom_real = (float)_cupsStrScand(val->value,
NULL, loc);
break;
case PPD_CUSTOM_POINTS :
cparam->current.custom_points = (float)_cupsStrScand(val->value,
&units,
loc);
if (units)
{
if (!_cups_strcasecmp(units, "cm"))
cparam->current.custom_points *= 72.0f / 2.54f;
else if (!_cups_strcasecmp(units, "mm"))
cparam->current.custom_points *= 72.0f / 25.4f;
else if (!_cups_strcasecmp(units, "m"))
cparam->current.custom_points *= 72.0f / 0.0254f;
else if (!_cups_strcasecmp(units, "in"))
cparam->current.custom_points *= 72.0f;
else if (!_cups_strcasecmp(units, "ft"))
cparam->current.custom_points *= 12.0f * 72.0f;
}
break;
case PPD_CUSTOM_INT :
cparam->current.custom_int = atoi(val->value);
break;
case PPD_CUSTOM_PASSCODE :
case PPD_CUSTOM_PASSWORD :
case PPD_CUSTOM_STRING :
if (cparam->current.custom_string)
_cupsStrFree(cparam->current.custom_string);
cparam->current.custom_string = _cupsStrRetain(val->value);
break;
}
}
cupsFreeOptions(num_vals, vals);
}
}
else
{
for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
if (!_cups_strcasecmp(c->choice, choice))
break;
if (!i)
return;
}
if (o->ui != PPD_UI_PICKMANY)
{
if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, c)) != NULL)
{
oldc->marked = 0;
cupsArrayRemove(ppd->marked, oldc);
}
if (!_cups_strcasecmp(option, "PageSize") || !_cups_strcasecmp(option, "PageRegion"))
{
for (j = 0; j < ppd->num_sizes; j ++)
ppd->sizes[j].marked = !_cups_strcasecmp(ppd->sizes[j].name,
choice);
cupsArraySave(ppd->options);
if (!_cups_strcasecmp(option, "PageSize"))
{
if ((o = ppdFindOption(ppd, "PageRegion")) != NULL)
{
key.option = o;
if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
{
oldc->marked = 0;
cupsArrayRemove(ppd->marked, oldc);
}
}
}
else
{
if ((o = ppdFindOption(ppd, "PageSize")) != NULL)
{
key.option = o;
if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
{
oldc->marked = 0;
cupsArrayRemove(ppd->marked, oldc);
}
}
}
cupsArrayRestore(ppd->options);
}
else if (!_cups_strcasecmp(option, "InputSlot"))
{
cupsArraySave(ppd->options);
if ((o = ppdFindOption(ppd, "ManualFeed")) != NULL)
{
key.option = o;
if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
{
oldc->marked = 0;
cupsArrayRemove(ppd->marked, oldc);
}
}
cupsArrayRestore(ppd->options);
}
else if (!_cups_strcasecmp(option, "ManualFeed") &&
!_cups_strcasecmp(choice, "True"))
{
cupsArraySave(ppd->options);
if ((o = ppdFindOption(ppd, "InputSlot")) != NULL)
{
key.option = o;
if ((oldc = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) != NULL)
{
oldc->marked = 0;
cupsArrayRemove(ppd->marked, oldc);
}
}
cupsArrayRestore(ppd->options);
}
}
c->marked = 1;
cupsArrayAdd(ppd->marked, c);
}