/*
- * "$Id: dest.c 6044 2006-10-17 20:32:59Z mike $"
+ * "$Id: dest.c 6649 2007-07-11 21:46:42Z mike $"
*
* User-defined destination (and option) support for the Common UNIX
* Printing System (CUPS).
*
- * Copyright 1997-2006 by Easy Software Products.
+ * Copyright 2007 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
- * property of Easy Software Products and are protected by Federal
- * copyright law. Distribution and use rights are outlined in the file
- * "LICENSE.txt" which should have been included with this file. If this
- * file is missing or damaged please contact Easy Software Products
- * at:
- *
- * Attn: CUPS Licensing Information
- * Easy Software Products
- * 44141 Airport View Drive, Suite 204
- * Hollywood, Maryland 20636 USA
- *
- * Voice: (301) 373-9600
- * EMail: cups-info@cups.org
- * WWW: http://www.cups.org
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*
* This file is subject to the Apple OS-Developed Software exception.
*
* Contents:
*
- * cupsAddDest() - Add a destination to the list of destinations.
- * cupsFreeDests() - Free the memory used by the list of destinations.
- * cupsGetDest() - Get the named destination from the list.
- * cupsGetDests() - Get the list of destinations from the default server.
- * cupsGetDests2() - Get the list of destinations from the specified server.
- * cupsSetDests() - Set the list of destinations for the default server.
- * cupsSetDests2() - Set the list of destinations for the specified server.
- * cups_get_dests() - Get destinations from a file.
- * cups_get_sdests() - Get destinations from a server.
+ * cupsAddDest() - Add a destination to the list of destinations.
+ * cupsFreeDests() - Free the memory used by the list of
+ * destinations.
+ * cupsGetDest() - Get the named destination from the list.
+ * cupsGetDests() - Get the list of destinations from the default
+ * server.
+ * cupsGetDests2() - Get the list of destinations from the
+ * specified server.
+ * cupsRemoveDest() - Remove a destination from the destination list.
+ * cupsDestSetDefaultDest() - Set the default destination.
+ * cupsSetDests() - Set the list of destinations for the default
+ * server.
+ * cupsSetDests2() - Set the list of destinations for the specified
+ * server.
+ * cups_get_dests() - Get destinations from a file.
+ * cups_get_sdests() - Get destinations from a server.
*/
/*
}
+/*
+ * 'cupsRemoveDest()' - Remove a destination from the destination list.
+ *
+ * Removing a destination/instance does not delete the class or printer
+ * queue, merely the lpoptions for that destination/instance. Use the
+ * cupsSetDests() or cupsSetDests2() functions to save the new options
+ * for the user.
+ *
+ * @since CUPS 1.3@
+ */
+
+int /* O - New number of destinations */
+cupsRemoveDest(const char *name, /* I - Destination name */
+ const char *instance, /* I - Instance name or NULL */
+ int num_dests, /* I - Number of destinations */
+ cups_dest_t **dests) /* IO - Destinations */
+{
+ int i; /* Index into destinations */
+ cups_dest_t *dest; /* Pointer to destination */
+
+
+ /*
+ * Find the destination...
+ */
+
+ if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL)
+ return (num_dests);
+
+ /*
+ * Free memory...
+ */
+
+ cupsFreeOptions(dest->num_options, dest->options);
+
+ /*
+ * Remove the destination from the array...
+ */
+
+ num_dests --;
+
+ i = dest - *dests;
+
+ if (i < num_dests)
+ memmove(dest, dest + 1, (num_dests - i) * sizeof(cups_dest_t));
+
+ return (num_dests);
+}
+
+
+/*
+ * 'cupsDestSetDefaultDest()' - Set the default destination.
+ *
+ * @since CUPS 1.3@
+ */
+
+void
+cupsSetDefaultDest(
+ const char *name, /* I - Destination name */
+ const char *instance, /* I - Instance name or NULL */
+ int num_dests, /* I - Number of destinations */
+ cups_dest_t *dests) /* I - Destinations */
+{
+ int i; /* Looping var */
+ cups_dest_t *dest; /* Current destination */
+
+
+ /*
+ * Range check input...
+ */
+
+ if (!name || num_dests <= 0 || !dests)
+ return;
+
+ /*
+ * Loop through the array and set the "is_default" flag for the matching
+ * destination...
+ */
+
+ for (i = num_dests, dest = dests; i > 0; i --, dest ++)
+ dest->is_default = !strcasecmp(name, dest->name) &&
+ ((!instance && !dest->instance) ||
+ (instance && dest->instance &&
+ !strcasecmp(instance, dest->instance)));
+}
+
+
/*
* 'cupsSetDests()' - Save the list of destinations for the default server.
*
cups_option_t *option; /* Current option */
_ipp_option_t *match; /* Matching attribute for option */
FILE *fp; /* File pointer */
+#ifndef WIN32
const char *home; /* HOME environment variable */
+#endif /* WIN32 */
char filename[1024]; /* lpoptions file */
int num_temps; /* Number of temporary destinations */
cups_dest_t *temps, /* Temporary destinations */
change_time, /* printer-state-change-time attribute */
type; /* printer-type attribute */
const char *info, /* printer-info attribute */
+ *location, /* printer-location attribute */
*make_model, /* printer-make-and-model attribute */
*name; /* printer-name attribute */
- char job_sheets[1024], /* job-sheets option */
- reasons[1024], /* printer-state-reasons attribute */
- *rptr, /* Pointer into reasons string */
- temp[255]; /* Temporary string for numbers */
+ char job_sheets[1024], /* job-sheets-default attribute */
+ auth_info_req[1024], /* auth-info-required attribute */
+ reasons[1024]; /* printer-state-reasons attribute */
+ int num_options; /* Number of options */
+ cups_option_t *options; /* Options */
+ char optname[1024], /* Option name */
+ value[2048], /* Option value */
+ *ptr; /* Pointer into name/value */
static const char * const pattrs[] = /* Attributes we're interested in */
{
+ "auth-info-required",
"job-sheets-default",
"printer-info",
"printer-is-accepting-jobs",
"printer-is-shared",
+ "printer-location",
"printer-make-and-model",
"printer-name",
"printer-state",
"printer-state-change-time",
"printer-state-reasons",
- "printer-type"
+ "printer-type",
+ "printer-defaults"
};
break;
/*
- * Pull the needed attributes from this job...
+ * Pull the needed attributes from this printer...
*/
accepting = 0;
change_time = 0;
info = NULL;
+ location = NULL;
make_model = NULL;
name = NULL;
+ num_options = 0;
+ options = NULL;
shared = 1;
state = IPP_PRINTER_IDLE;
type = CUPS_PRINTER_LOCAL;
- strcpy(job_sheets, "");
- strcpy(reasons, "");
+ auth_info_req[0] = '\0';
+ job_sheets[0] = '\0';
+ reasons[0] = '\0';
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
- if (!strcmp(attr->name, "job-sheets-default") &&
- (attr->value_tag == IPP_TAG_KEYWORD ||
- attr->value_tag == IPP_TAG_NAME))
+ if (!strcmp(attr->name, "auth-info-required") &&
+ attr->value_tag == IPP_TAG_KEYWORD)
+ {
+ strlcpy(auth_info_req, attr->values[0].string.text,
+ sizeof(auth_info_req));
+
+ for (i = 1, ptr = auth_info_req + strlen(auth_info_req);
+ i < attr->num_values;
+ i ++)
+ {
+ snprintf(ptr, sizeof(auth_info_req) - (ptr - auth_info_req), ",%s",
+ attr->values[i].string.text);
+ ptr += strlen(ptr);
+ }
+ }
+ else if (!strcmp(attr->name, "job-sheets-default") &&
+ (attr->value_tag == IPP_TAG_KEYWORD ||
+ attr->value_tag == IPP_TAG_NAME))
{
if (attr->num_values == 2)
snprintf(job_sheets, sizeof(job_sheets), "%s,%s",
else if (!strcmp(attr->name, "printer-is-shared") &&
attr->value_tag == IPP_TAG_BOOLEAN)
shared = attr->values[0].boolean;
+ else if (!strcmp(attr->name, "printer-location") &&
+ attr->value_tag == IPP_TAG_TEXT)
+ location = attr->values[0].string.text;
else if (!strcmp(attr->name, "printer-make-and-model") &&
attr->value_tag == IPP_TAG_TEXT)
make_model = attr->values[0].string.text;
attr->value_tag == IPP_TAG_KEYWORD)
{
strlcpy(reasons, attr->values[0].string.text, sizeof(reasons));
- for (i = 1, rptr = reasons + strlen(reasons);
+ for (i = 1, ptr = reasons + strlen(reasons);
i < attr->num_values;
i ++)
{
- snprintf(rptr, sizeof(reasons) - (rptr - reasons), ",%s",
+ snprintf(ptr, sizeof(reasons) - (ptr - reasons), ",%s",
attr->values[i].string.text);
- rptr += strlen(rptr);
+ ptr += strlen(ptr);
}
}
else if (!strcmp(attr->name, "printer-type") &&
attr->value_tag == IPP_TAG_ENUM)
type = attr->values[0].integer;
+ else if (strncmp(attr->name, "notify-", 7) &&
+ (attr->value_tag == IPP_TAG_BOOLEAN ||
+ attr->value_tag == IPP_TAG_ENUM ||
+ attr->value_tag == IPP_TAG_INTEGER ||
+ attr->value_tag == IPP_TAG_KEYWORD ||
+ attr->value_tag == IPP_TAG_NAME ||
+ attr->value_tag == IPP_TAG_RANGE) &&
+ strstr(attr->name, "-default"))
+ {
+ char *valptr; /* Pointer into attribute value */
+
+
+ /*
+ * Add a default option...
+ */
+
+ strlcpy(optname, attr->name, sizeof(optname));
+ if ((ptr = strstr(optname, "-default")) != NULL)
+ *ptr = '\0';
+
+ value[0] = '\0';
+ for (i = 0, ptr = value; i < attr->num_values; i ++)
+ {
+ if (ptr >= (value + sizeof(value) - 1))
+ break;
+
+ if (i)
+ *ptr++ = ',';
+
+ switch (attr->value_tag)
+ {
+ case IPP_TAG_INTEGER :
+ case IPP_TAG_ENUM :
+ snprintf(ptr, sizeof(value) - (ptr - value), "%d",
+ attr->values[i].integer);
+ break;
+
+ case IPP_TAG_BOOLEAN :
+ if (attr->values[i].boolean)
+ strlcpy(ptr, "true", sizeof(value) - (ptr - value));
+ else
+ strlcpy(ptr, "false", sizeof(value) - (ptr - value));
+ break;
+
+ case IPP_TAG_RANGE :
+ if (attr->values[i].range.lower ==
+ attr->values[i].range.upper)
+ snprintf(ptr, sizeof(value) - (ptr - value), "%d",
+ attr->values[i].range.lower);
+ else
+ snprintf(ptr, sizeof(value) - (ptr - value), "%d-%d",
+ attr->values[i].range.lower,
+ attr->values[i].range.upper);
+ break;
+
+ default :
+ for (valptr = attr->values[i].string.text;
+ *valptr && ptr < (value + sizeof(value) - 2);)
+ {
+ if (strchr(" \t\n\\\'\"", *valptr))
+ *ptr++ = '\\';
+
+ *ptr++ = *valptr++;
+ }
+
+ *ptr = '\0';
+ break;
+ }
+
+ ptr += strlen(ptr);
+ }
+
+ num_options = cupsAddOption(optname, value, num_options, &options);
+ }
attr = attr->next;
}
if (!name)
{
+ cupsFreeOptions(num_options, options);
+
if (attr == NULL)
break;
else
if ((dest = cupsGetDest(name, NULL, num_dests, *dests)) != NULL)
{
+ dest->num_options = num_options;
+ dest->options = options;
+
+ num_options = 0;
+ options = NULL;
+
+ if (auth_info_req[0])
+ dest->num_options = cupsAddOption("auth-info-required", auth_info_req,
+ dest->num_options,
+ &(dest->options));
+
if (job_sheets[0])
dest->num_options = cupsAddOption("job-sheets", job_sheets,
dest->num_options,
dest->num_options,
&(dest->options));
- sprintf(temp, "%d", accepting);
- dest->num_options = cupsAddOption("printer-is-accepting-jobs", temp,
+ sprintf(value, "%d", accepting);
+ dest->num_options = cupsAddOption("printer-is-accepting-jobs", value,
dest->num_options,
&(dest->options));
- sprintf(temp, "%d", shared);
- dest->num_options = cupsAddOption("printer-is-shared", temp,
+ sprintf(value, "%d", shared);
+ dest->num_options = cupsAddOption("printer-is-shared", value,
dest->num_options,
&(dest->options));
+ if (location)
+ dest->num_options = cupsAddOption("printer-location",
+ location, dest->num_options,
+ &(dest->options));
+
if (make_model)
dest->num_options = cupsAddOption("printer-make-and-model",
make_model, dest->num_options,
&(dest->options));
- sprintf(temp, "%d", state);
- dest->num_options = cupsAddOption("printer-state", temp,
+ sprintf(value, "%d", state);
+ dest->num_options = cupsAddOption("printer-state", value,
dest->num_options,
&(dest->options));
if (change_time)
{
- sprintf(temp, "%d", change_time);
- dest->num_options = cupsAddOption("printer-state-change-time", temp,
+ sprintf(value, "%d", change_time);
+ dest->num_options = cupsAddOption("printer-state-change-time", value,
dest->num_options,
&(dest->options));
}
dest->num_options,
&(dest->options));
- sprintf(temp, "%d", type);
- dest->num_options = cupsAddOption("printer-type", temp,
+ sprintf(value, "%d", type);
+ dest->num_options = cupsAddOption("printer-type", value,
dest->num_options,
&(dest->options));
}
+ cupsFreeOptions(num_options, options);
+
if (attr == NULL)
break;
}
/*
- * End of "$Id: dest.c 6044 2006-10-17 20:32:59Z mike $".
+ * End of "$Id: dest.c 6649 2007-07-11 21:46:42Z mike $".
*/