#include "ppd.h"
#include "string.h"
#include "debug.h"
static void ppd_defaults(ppd_file_t *ppd, ppd_group_t *g);
int
ppdConflicts(ppd_file_t *ppd)
{
int i, j, k,
conflicts;
ppd_const_t *c;
ppd_group_t *g, *sg;
ppd_option_t *o1, *o2;
ppd_choice_t *c1, *c2;
if (ppd == NULL)
return (0);
conflicts = 0;
for (i = ppd->num_groups, g = ppd->groups; i > 0; i --, g ++)
{
for (j = g->num_options, o1 = g->options; j > 0; j --, o1 ++)
o1->conflicted = 0;
for (j = g->num_subgroups, sg = g->subgroups; j > 0; j --, sg ++)
for (k = sg->num_options, o1 = sg->options; k > 0; k --, o1 ++)
o1->conflicted = 0;
}
for (i = ppd->num_consts, c = ppd->consts; i > 0; i --, c ++)
{
o1 = ppdFindOption(ppd, c->option1);
if (o1 == NULL)
continue;
else if (c->choice1[0] != '\0')
{
c1 = ppdFindChoice(o1, c->choice1);
}
else
{
for (j = o1->num_choices, c1 = o1->choices; j > 0; j --, c1 ++)
if (c1->marked)
break;
if (j == 0 ||
strcasecmp(c1->choice, "None") == 0 ||
strcasecmp(c1->choice, "Off") == 0 ||
strcasecmp(c1->choice, "False") == 0)
c1 = NULL;
}
o2 = ppdFindOption(ppd, c->option2);
if (o2 == NULL)
continue;
else if (c->choice2[0] != '\0')
{
c2 = ppdFindChoice(o2, c->choice2);
}
else
{
for (j = o2->num_choices, c2 = o2->choices; j > 0; j --, c2 ++)
if (c2->marked)
break;
if (j == 0 ||
strcasecmp(c2->choice, "None") == 0 ||
strcasecmp(c2->choice, "Off") == 0 ||
strcasecmp(c2->choice, "False") == 0)
c2 = NULL;
}
if (c1 != NULL && c1->marked &&
c2 != NULL && c2->marked)
{
DEBUG_printf(("%s->%s conflicts with %s->%s (%s %s %s %s)\n",
o1->keyword, c1->choice, o2->keyword, c2->choice,
c->option1, c->choice1, c->option2, c->choice2));
conflicts ++;
o1->conflicted = 1;
o2->conflicted = 1;
}
}
return (conflicts);
}
ppd_choice_t *
ppdFindChoice(ppd_option_t *o,
const char *choice)
{
int i;
ppd_choice_t *c;
if (o == NULL || choice == NULL)
return (NULL);
for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
if (strcasecmp(c->choice, choice) == 0)
return (c);
return (NULL);
}
ppd_choice_t *
ppdFindMarkedChoice(ppd_file_t *ppd,
const char *option)
{
int i;
ppd_option_t *o;
ppd_choice_t *c;
if ((o = ppdFindOption(ppd, option)) == NULL)
return (NULL);
for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
if (c->marked)
return (c);
return (NULL);
}
ppd_option_t *
ppdFindOption(ppd_file_t *ppd,
const char *option)
{
int i, j, k;
ppd_option_t *o;
ppd_group_t *g,
*sg;
if (ppd == NULL || option == NULL)
return (NULL);
for (i = ppd->num_groups, g = ppd->groups; i > 0; i --, g ++)
{
for (j = g->num_options, o = g->options; j > 0; j --, o ++)
if (strcasecmp(o->keyword, option) == 0)
return (o);
for (j = g->num_subgroups, sg = g->subgroups; j > 0; j --, sg ++)
for (k = sg->num_options, o = sg->options; k > 0; k --, o ++)
if (strcasecmp(o->keyword, option) == 0)
return (o);
}
return (NULL);
}
int
ppdIsMarked(ppd_file_t *ppd,
const char *option,
const char *choice)
{
ppd_option_t *o;
ppd_choice_t *c;
if (ppd == NULL)
return (0);
if ((o = ppdFindOption(ppd, option)) == NULL)
return (0);
if ((c = ppdFindChoice(o, choice)) == NULL)
return (0);
return (c->marked);
}
void
ppdMarkDefaults(ppd_file_t *ppd)
{
int i;
ppd_group_t *g;
if (ppd == NULL)
return;
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)
{
int i;
ppd_option_t *o;
ppd_choice_t *c;
if (ppd == NULL)
return (0);
if (strcasecmp(option, "AP_D_InputSlot") == 0){
if ((o = ppdFindOption(ppd, "InputSlot")) != NULL)
for (i = 0; i < o->num_choices; i ++)
o->choices[i].marked = 0;
}
if (strcasecmp(option, "PageSize") == 0 && strncasecmp(choice, "Custom.", 7) == 0)
{
ppdPageSize(ppd, choice);
choice = "Custom";
}
if ((o = ppdFindOption(ppd, option)) == NULL)
return (0);
for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
if (strcasecmp(c->choice, choice) == 0)
break;
if (i)
{
c->marked = 1;
if (o->ui != PPD_UI_PICKMANY)
for (i = o->num_choices, c = o->choices; i > 0; i --, c ++)
if (strcasecmp(c->choice, choice) != 0)
c->marked = 0;
if (strcasecmp(option, "PageSize") == 0 || strcasecmp(option, "PageRegion") == 0)
{
for (i = 0; i < ppd->num_sizes; i ++)
ppd->sizes[i].marked = strcasecmp(ppd->sizes[i].name, choice) == 0;
if (strcasecmp(option, "PageSize") == 0)
{
if ((o = ppdFindOption(ppd, "PageRegion")) != NULL)
for (i = 0; i < o->num_choices; i ++)
o->choices[i].marked = 0;
}
else
{
if ((o = ppdFindOption(ppd, "PageSize")) != NULL)
for (i = 0; i < o->num_choices; i ++)
o->choices[i].marked = 0;
}
}
else if (strcasecmp(option, "InputSlot") == 0)
{
if ((o = ppdFindOption(ppd, "ManualFeed")) != NULL)
for (i = 0; i < o->num_choices; i ++)
o->choices[i].marked = 0;
}
else if (strcasecmp(option, "ManualFeed") == 0)
{
if ((o = ppdFindOption(ppd, "InputSlot")) != NULL)
for (i = 0; i < o->num_choices; i ++)
o->choices[i].marked = 0;
}
}
return (ppdConflicts(ppd));
}
static void
ppd_defaults(ppd_file_t *ppd,
ppd_group_t *g)
{
int i;
ppd_option_t *o;
ppd_group_t *sg;
if (g == NULL)
return;
for (i = g->num_options, o = g->options; i > 0; i --, o ++)
if (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);
}