+ cupsArrayRemove(PrintingJobs, job);
+
+ /*
+ * Clear the printer <-> job association...
+ */
+
+ job->printer->job = NULL;
+ job->printer = NULL;
+
+ /*
+ * Try printing another job...
+ */
+
+ if (printer_state != IPP_PRINTER_STOPPED)
+ cupsdCheckJobs();
+}
+
+
+/*
+ * 'get_options()' - Get a string containing the job options.
+ */
+
+static char * /* O - Options string */
+get_options(cupsd_job_t *job, /* I - Job */
+ int banner_page, /* I - Printing a banner page? */
+ char *copies, /* I - Copies buffer */
+ size_t copies_size, /* I - Size of copies buffer */
+ char *title, /* I - Title buffer */
+ size_t title_size) /* I - Size of title buffer */
+{
+ int i; /* Looping var */
+ size_t newlength; /* New option buffer length */
+ char *optptr, /* Pointer to options */
+ *valptr; /* Pointer in value string */
+ ipp_attribute_t *attr; /* Current attribute */
+ _pwg_t *pwg; /* PWG->PPD mapping data */
+ int num_pwgppds; /* Number of PWG->PPD options */
+ cups_option_t *pwgppds, /* PWG->PPD options */
+ *pwgppd, /* Current PWG->PPD option */
+ *preset; /* Current preset option */
+ int output_mode, /* Output mode (if any) */
+ print_quality; /* Print quality (if any) */
+ const char *ppd; /* PPD option choice */
+ int exact; /* Did we get an exact match? */
+ static char *options = NULL;/* Full list of options */
+ static size_t optlength = 0; /* Length of option buffer */
+
+
+ /*
+ * Building the options string is harder than it needs to be, but for the
+ * moment we need to pass strings for command-line args and not IPP attribute
+ * pointers... :)
+ *
+ * First build an options array for any PWG->PPD mapped option/choice pairs.
+ */
+
+ pwg = job->printer->pwg;
+ num_pwgppds = 0;
+ pwgppds = NULL;
+
+ if (pwg &&
+ !ippFindAttribute(job->attrs,
+ "com.apple.print.DocumentTicket.PMSpoolFormat",
+ IPP_TAG_ZERO) &&
+ (ippFindAttribute(job->attrs, "output-mode", IPP_TAG_ZERO) ||
+ ippFindAttribute(job->attrs, "print-quality", IPP_TAG_ZERO)))
+ {
+ /*
+ * Map output-mode and print-quality to a preset...
+ */
+
+ if ((attr = ippFindAttribute(job->attrs, "output-mode",
+ IPP_TAG_KEYWORD)) != NULL &&
+ !strcmp(attr->values[0].string.text, "monochrome"))
+ output_mode = _PWG_OUTPUT_MODE_MONOCHROME;
+ else
+ output_mode = _PWG_OUTPUT_MODE_COLOR;
+
+ if ((attr = ippFindAttribute(job->attrs, "print-quality",
+ IPP_TAG_ENUM)) != NULL &&
+ attr->values[0].integer >= IPP_QUALITY_DRAFT &&
+ attr->values[0].integer <= IPP_QUALITY_HIGH)
+ print_quality = attr->values[0].integer - IPP_QUALITY_DRAFT;
+ else
+ print_quality = _PWG_PRINT_QUALITY_NORMAL;
+
+ if (pwg->num_presets[output_mode][print_quality] == 0)
+ {
+ /*
+ * Try to find a preset that works so that we maximize the chances of us
+ * getting a good print using IPP attributes.
+ */
+
+ if (pwg->num_presets[output_mode][_PWG_PRINT_QUALITY_NORMAL] > 0)
+ print_quality = _PWG_PRINT_QUALITY_NORMAL;
+ else if (pwg->num_presets[_PWG_OUTPUT_MODE_COLOR][print_quality] > 0)
+ output_mode = _PWG_OUTPUT_MODE_COLOR;
+ else
+ {
+ print_quality = _PWG_PRINT_QUALITY_NORMAL;
+ output_mode = _PWG_OUTPUT_MODE_COLOR;
+ }
+ }
+
+ if (pwg->num_presets[output_mode][print_quality] > 0)
+ {
+ /*
+ * Copy the preset options as long as the corresponding names are not
+ * already defined in the IPP request...
+ */
+
+ for (i = pwg->num_presets[output_mode][print_quality],
+ preset = pwg->presets[output_mode][print_quality];
+ i > 0;
+ i --, preset ++)
+ {
+ if (!ippFindAttribute(job->attrs, preset->name, IPP_TAG_ZERO))
+ num_pwgppds = cupsAddOption(preset->name, preset->value, num_pwgppds,
+ &pwgppds);
+ }
+ }
+ }
+
+ if (pwg)
+ {
+ if (pwg->sides_option &&
+ !ippFindAttribute(job->attrs, pwg->sides_option, IPP_TAG_ZERO) &&
+ (attr = ippFindAttribute(job->attrs, "sides", IPP_TAG_KEYWORD)) != NULL)
+ {
+ /*
+ * Add a duplex option...
+ */
+
+ if (!strcmp(attr->values[0].string.text, "one-sided"))
+ num_pwgppds = cupsAddOption(pwg->sides_option, pwg->sides_1sided,
+ num_pwgppds, &pwgppds);
+ else if (!strcmp(attr->values[0].string.text, "two-sided-long-edge"))
+ num_pwgppds = cupsAddOption(pwg->sides_option, pwg->sides_2sided_long,
+ num_pwgppds, &pwgppds);
+ else if (!strcmp(attr->values[0].string.text, "two-sided-short-edge"))
+ num_pwgppds = cupsAddOption(pwg->sides_option, pwg->sides_2sided_short,
+ num_pwgppds, &pwgppds);
+ }
+
+ if (!ippFindAttribute(job->attrs, "InputSlot", IPP_TAG_ZERO) &&
+ !ippFindAttribute(job->attrs, "HPPaperSource", IPP_TAG_ZERO))
+ {
+ if ((ppd = _pwgGetInputSlot(pwg, job->attrs, NULL)) != NULL)
+ num_pwgppds = cupsAddOption(pwg->source_option, ppd, num_pwgppds,
+ &pwgppds);
+ else if (!ippFindAttribute(job->attrs, "AP_D_InputSlot", IPP_TAG_ZERO))
+ num_pwgppds = cupsAddOption("AP_D_InputSlot", "", num_pwgppds,
+ &pwgppds);
+ }
+
+ if (!ippFindAttribute(job->attrs, "MediaType", IPP_TAG_ZERO) &&
+ (ppd = _pwgGetMediaType(pwg, job->attrs, NULL)) != NULL)
+ num_pwgppds = cupsAddOption("MediaType", ppd, num_pwgppds, &pwgppds);
+
+ if (!ippFindAttribute(job->attrs, "PageRegion", IPP_TAG_ZERO) &&
+ !ippFindAttribute(job->attrs, "PageSize", IPP_TAG_ZERO) &&
+ (ppd = _pwgGetPageSize(pwg, job->attrs, NULL, &exact)) != NULL)
+ {
+ num_pwgppds = cupsAddOption("PageSize", ppd, num_pwgppds, &pwgppds);
+
+ if (!ippFindAttribute(job->attrs, "media", IPP_TAG_ZERO))
+ num_pwgppds = cupsAddOption("media", ppd, num_pwgppds, &pwgppds);
+ }
+
+ if (!ippFindAttribute(job->attrs, "OutputBin", IPP_TAG_ZERO) &&
+ (attr = ippFindAttribute(job->attrs, "output-bin",
+ IPP_TAG_ZERO)) != NULL &&
+ (attr->value_tag == IPP_TAG_KEYWORD ||
+ attr->value_tag == IPP_TAG_NAME) &&
+ (ppd = _pwgGetOutputBin(pwg, attr->values[0].string.text)) != NULL)
+ num_pwgppds = cupsAddOption("OutputBin", ppd, num_pwgppds, &pwgppds);
+ }
+
+ /*
+ * Figure out how much room we need...
+ */
+
+ newlength = ipp_length(job->attrs);
+
+ for (i = num_pwgppds, pwgppd = pwgppds; i > 0; i --, pwgppd ++)
+ newlength += 1 + strlen(pwgppd->name) + 1 + strlen(pwgppd->value);
+
+ /*
+ * Then allocate/reallocate the option buffer as needed...
+ */
+
+ if (newlength > optlength || !options)
+ {
+ if (!options)
+ optptr = malloc(newlength);
+ else
+ optptr = realloc(options, newlength);
+
+ if (!optptr)
+ {
+ cupsdLogJob(job, CUPSD_LOG_CRIT,
+ "Unable to allocate " CUPS_LLFMT " bytes for option buffer!",
+ CUPS_LLCAST newlength);
+ return (NULL);
+ }
+
+ options = optptr;
+ optlength = newlength;
+ }
+
+ /*
+ * Now loop through the attributes and convert them to the textual
+ * representation used by the filters...
+ */
+
+ optptr = options;
+ *optptr = '\0';
+
+ snprintf(title, title_size, "%s-%d", job->printer->name, job->id);
+ strlcpy(copies, "1", copies_size);
+
+ for (attr = job->attrs->attrs; attr != NULL; attr = attr->next)
+ {
+ if (!strcmp(attr->name, "copies") &&
+ attr->value_tag == IPP_TAG_INTEGER)
+ {
+ /*
+ * Don't use the # copies attribute if we are printing the job sheets...
+ */
+
+ if (!banner_page)
+ snprintf(copies, copies_size, "%d", attr->values[0].integer);
+ }
+ else if (!strcmp(attr->name, "job-name") &&
+ (attr->value_tag == IPP_TAG_NAME ||