#include "globals.h"
#include <stdlib.h>
#include <ctype.h>
#include <sys/stat.h>
#ifdef HAVE_NOTIFY_H
# include <notify.h>
#endif
static int cups_get_dests(const char *filename, int num_dests,
cups_dest_t **dests);
static int cups_get_sdests(http_t *http, ipp_op_t op, int num_dests,
cups_dest_t **dests);
int
cupsAddDest(const char *name,
const char *instance,
int num_dests,
cups_dest_t **dests)
{
int i;
cups_dest_t *dest;
cups_dest_t *parent;
cups_option_t *option;
if (!name || !dests)
return (0);
if ((dest = cupsGetDest(name, instance, num_dests, *dests)) != NULL)
return (num_dests);
if (num_dests == 0)
dest = malloc(sizeof(cups_dest_t));
else
dest = realloc(*dests, sizeof(cups_dest_t) * (num_dests + 1));
if (dest == NULL)
return (num_dests);
*dests = dest;
for (i = num_dests; i > 0; i --, dest ++)
if (strcasecmp(name, dest->name) < 0)
break;
else if (!instance && dest->instance)
break;
else if (!strcasecmp(name, dest->name) &&
instance && dest->instance &&
strcasecmp(instance, dest->instance) < 0)
break;
if (i > 0)
memmove(dest + 1, dest, i * sizeof(cups_dest_t));
dest->name = strdup(name);
dest->is_default = 0;
dest->num_options = 0;
dest->options = (cups_option_t *)0;
if (!instance)
dest->instance = NULL;
else
{
dest->instance = strdup(instance);
if ((parent = cupsGetDest(name, NULL, num_dests + 1, *dests)) != NULL)
{
for (i = parent->num_options, option = parent->options;
i > 0;
i --, option ++)
dest->num_options = cupsAddOption(option->name, option->value,
dest->num_options,
&(dest->options));
}
}
return (num_dests + 1);
}
void
cupsFreeDests(int num_dests,
cups_dest_t *dests)
{
int i;
cups_dest_t *dest;
if (num_dests == 0 || dests == NULL)
return;
for (i = num_dests, dest = dests; i > 0; i --, dest ++)
{
free(dest->name);
if (dest->instance)
free(dest->instance);
cupsFreeOptions(dest->num_options, dest->options);
}
free(dests);
}
cups_dest_t *
cupsGetDest(const char *name,
const char *instance,
int num_dests,
cups_dest_t *dests)
{
int comp;
if (num_dests <= 0 || !dests)
return (NULL);
if (!name)
{
while (num_dests > 0)
{
if (dests->is_default)
return (dests);
num_dests --;
dests ++;
}
}
else
{
while (num_dests > 0)
{
if ((comp = strcasecmp(name, dests->name)) < 0)
return (NULL);
else if (comp == 0)
{
if ((!instance && !dests->instance) ||
(instance != NULL && dests->instance != NULL &&
!strcasecmp(instance, dests->instance)))
return (dests);
}
num_dests --;
dests ++;
}
}
return (NULL);
}
int
cupsGetDests(cups_dest_t **dests)
{
int num_dests;
http_t *http;
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
num_dests = cupsGetDests2(http, dests);
if (http)
httpClose(http);
return (num_dests);
}
int
cupsGetDests2(http_t *http,
cups_dest_t **dests)
{
int i;
int num_dests;
cups_dest_t *dest;
const char *home;
char filename[1024];
const char *defprinter;
char name[1024],
*instance;
int num_reals;
cups_dest_t *reals;
_cups_globals_t *cg = _cupsGlobals();
if (!http || !dests)
return (0);
num_dests = 0;
*dests = (cups_dest_t *)0;
num_dests = cups_get_sdests(http, CUPS_GET_PRINTERS, num_dests, dests);
num_dests = cups_get_sdests(http, CUPS_GET_CLASSES, num_dests, dests);
if (num_dests > 0)
{
num_reals = num_dests;
reals = calloc(num_reals, sizeof(cups_dest_t));
if (reals)
memcpy(reals, *dests, num_reals * sizeof(cups_dest_t));
else
num_reals = 0;
}
else
{
num_reals = 0;
reals = NULL;
}
if ((defprinter = cupsGetDefault2(http)) != NULL)
{
strlcpy(name, defprinter, sizeof(name));
if ((instance = strchr(name, '/')) != NULL)
*instance++ = '\0';
if ((dest = cupsGetDest(name, instance, num_dests, *dests)) != NULL)
dest->is_default = 1;
}
else
{
instance = NULL;
}
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
num_dests = cups_get_dests(filename, num_dests, dests);
if ((home = getenv("HOME")) != NULL)
{
snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
if (access(filename, 0))
snprintf(filename, sizeof(filename), "%s/.lpoptions", home);
num_dests = cups_get_dests(filename, num_dests, dests);
}
if (num_reals)
{
if ((dest = cupsGetDest(NULL, NULL, num_dests, *dests)) != NULL)
{
dest = cupsGetDest(dest->name, NULL, num_reals, reals);
}
if (dest == NULL && defprinter != NULL)
{
for (i = 0; i < num_dests; i ++)
(*dests)[i].is_default = 0;
if ((dest = cupsGetDest(name, instance, num_dests, *dests)) != NULL)
dest->is_default = 1;
}
free(reals);
}
return (num_dests);
}
int
cupsRemoveDest(const char *name,
const char *instance,
int num_dests,
cups_dest_t **dests)
{
int i;
cups_dest_t *dest;
if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL)
return (num_dests);
cupsFreeOptions(dest->num_options, dest->options);
num_dests --;
i = dest - *dests;
if (i < num_dests)
memmove(dest, dest + 1, (num_dests - i) * sizeof(cups_dest_t));
return (num_dests);
}
void
cupsSetDefaultDest(
const char *name,
const char *instance,
int num_dests,
cups_dest_t *dests)
{
int i;
cups_dest_t *dest;
if (!name || num_dests <= 0 || !dests)
return;
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)));
}
void
cupsSetDests(int num_dests,
cups_dest_t *dests)
{
http_t *http;
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
cupsSetDests2(http, num_dests, dests);
if (http)
httpClose(http);
}
int
cupsSetDests2(http_t *http,
int num_dests,
cups_dest_t *dests)
{
int i, j;
int wrote;
cups_dest_t *dest;
cups_option_t *option;
_ipp_option_t *match;
FILE *fp;
#ifndef WIN32
const char *home;
#endif
char filename[1024];
int num_temps;
cups_dest_t *temps,
*temp;
const char *val;
_cups_globals_t *cg = _cupsGlobals();
if (!http || !num_dests || !dests)
return (-1);
num_temps = cups_get_sdests(http, CUPS_GET_PRINTERS, 0, &temps);
num_temps = cups_get_sdests(http, CUPS_GET_CLASSES, num_temps, &temps);
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
#ifndef WIN32
if (getuid())
{
num_temps = cups_get_dests(filename, num_temps, &temps);
if ((home = getenv("HOME")) != NULL)
{
snprintf(filename, sizeof(filename), "%s/.lpoptions", home);
unlink(filename);
snprintf(filename, sizeof(filename), "%s/.cups", home);
if (access(filename, 0))
mkdir(filename, 0700);
snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
}
}
#endif
if ((fp = fopen(filename, "w")) == NULL)
{
cupsFreeDests(num_temps, temps);
return (-1);
}
#ifndef WIN32
if (!getuid())
fchmod(fileno(fp), 0644);
#endif
for (i = num_dests, dest = dests; i > 0; i --, dest ++)
if (dest->instance != NULL || dest->num_options != 0 || dest->is_default)
{
if (dest->is_default)
{
fprintf(fp, "Default %s", dest->name);
if (dest->instance)
fprintf(fp, "/%s", dest->instance);
wrote = 1;
}
else
wrote = 0;
if ((temp = cupsGetDest(dest->name, dest->instance, num_temps, temps)) == NULL)
temp = cupsGetDest(dest->name, NULL, num_temps, temps);
for (j = dest->num_options, option = dest->options; j > 0; j --, option ++)
{
if ((match = _ippFindOption(option->name)) != NULL &&
match->group_tag == IPP_TAG_PRINTER)
continue;
if (temp &&
(val = cupsGetOption(option->name, temp->num_options,
temp->options)) != NULL &&
!strcasecmp(val, option->value))
continue;
if (!wrote)
{
fprintf(fp, "Dest %s", dest->name);
if (dest->instance)
fprintf(fp, "/%s", dest->instance);
wrote = 1;
}
if (option->value[0])
{
if (strchr(option->value, ' ') ||
strchr(option->value, '\\') ||
strchr(option->value, '\"') ||
strchr(option->value, '\''))
{
fprintf(fp, " %s=\"", option->name);
for (val = option->value; *val; val ++)
{
if (strchr("\"\'\\", *val))
putc('\\', fp);
putc(*val, fp);
}
putc('\"', fp);
}
else
{
fprintf(fp, " %s=%s", option->name, option->value);
}
}
else
fprintf(fp, " %s", option->name);
}
if (wrote)
fputs("\n", fp);
}
cupsFreeDests(num_temps, temps);
fclose(fp);
#ifdef HAVE_NOTIFY_POST
notify_post("com.apple.printerListChange");
#endif
return (0);
}
static int
cups_get_dests(const char *filename,
int num_dests,
cups_dest_t **dests)
{
int i;
cups_dest_t *dest;
FILE *fp;
char line[8192],
*lineptr,
*name,
*instance;
const char *printer;
if ((printer = getenv("LPDEST")) == NULL)
if ((printer = getenv("PRINTER")) != NULL)
if (strcmp(printer, "lp") == 0)
printer = NULL;
if ((fp = fopen(filename, "r")) == NULL)
return (num_dests);
while (fgets(line, sizeof(line), fp) != NULL)
{
if (strncasecmp(line, "dest", 4) == 0 && isspace(line[4] & 255))
lineptr = line + 4;
else if (strncasecmp(line, "default", 7) == 0 && isspace(line[7] & 255))
lineptr = line + 7;
else
continue;
while (isspace(*lineptr & 255))
lineptr ++;
if (!*lineptr)
continue;
name = lineptr;
while (!isspace(*lineptr & 255) && *lineptr && *lineptr != '/')
lineptr ++;
if (!*lineptr)
continue;
if (*lineptr == '/')
{
*lineptr++ = '\0';
instance = lineptr;
while (!isspace(*lineptr & 255) && *lineptr)
lineptr ++;
}
else
instance = NULL;
*lineptr++ = '\0';
if (cupsGetDest(name, NULL, num_dests, *dests) == NULL)
continue;
num_dests = cupsAddDest(name, instance, num_dests, dests);
if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL)
{
fclose(fp);
return (num_dests);
}
dest->num_options = cupsParseOptions(lineptr, dest->num_options,
&(dest->options));
if (strncasecmp(line, "default", 7) == 0 && printer == NULL)
{
for (i = 0; i < num_dests; i ++)
(*dests)[i].is_default = 0;
dest->is_default = 1;
}
}
fclose(fp);
return (num_dests);
}
static int
cups_get_sdests(http_t *http,
ipp_op_t op,
int num_dests,
cups_dest_t **dests)
{
int i;
cups_dest_t *dest;
ipp_t *request,
*response;
ipp_attribute_t *attr;
int accepting,
shared,
state,
change_time,
type;
const char *info,
*location,
*make_model,
*name;
char job_sheets[1024],
auth_info_req[1024],
reasons[1024];
int num_options;
cups_option_t *options;
char optname[1024],
value[2048],
*ptr;
static const char * const pattrs[] =
{
"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-defaults"
};
request = ippNewRequest(op);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
NULL, pattrs);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
for (attr = response->attrs; attr != NULL; attr = attr->next)
{
while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
attr = attr->next;
if (attr == NULL)
break;
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;
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, "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",
attr->values[0].string.text, attr->values[1].string.text);
else
strlcpy(job_sheets, attr->values[0].string.text,
sizeof(job_sheets));
}
else if (!strcmp(attr->name, "printer-info") &&
attr->value_tag == IPP_TAG_TEXT)
info = attr->values[0].string.text;
else if (!strcmp(attr->name, "printer-is-accepting-jobs") &&
attr->value_tag == IPP_TAG_BOOLEAN)
accepting = attr->values[0].boolean;
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;
else if (!strcmp(attr->name, "printer-name") &&
attr->value_tag == IPP_TAG_NAME)
name = attr->values[0].string.text;
else if (!strcmp(attr->name, "printer-state") &&
attr->value_tag == IPP_TAG_ENUM)
state = attr->values[0].integer;
else if (!strcmp(attr->name, "printer-state-change-time") &&
attr->value_tag == IPP_TAG_INTEGER)
change_time = attr->values[0].integer;
else if (!strcmp(attr->name, "printer-state-reasons") &&
attr->value_tag == IPP_TAG_KEYWORD)
{
strlcpy(reasons, attr->values[0].string.text, sizeof(reasons));
for (i = 1, ptr = reasons + strlen(reasons);
i < attr->num_values;
i ++)
{
snprintf(ptr, sizeof(reasons) - (ptr - reasons), ",%s",
attr->values[i].string.text);
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;
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
continue;
}
num_dests = cupsAddDest(name, NULL, num_dests, dests);
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->options));
if (info)
dest->num_options = cupsAddOption("printer-info", info,
dest->num_options,
&(dest->options));
sprintf(value, "%d", accepting);
dest->num_options = cupsAddOption("printer-is-accepting-jobs", value,
dest->num_options,
&(dest->options));
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(value, "%d", state);
dest->num_options = cupsAddOption("printer-state", value,
dest->num_options,
&(dest->options));
if (change_time)
{
sprintf(value, "%d", change_time);
dest->num_options = cupsAddOption("printer-state-change-time", value,
dest->num_options,
&(dest->options));
}
if (reasons[0])
dest->num_options = cupsAddOption("printer-state-reasons", reasons,
dest->num_options,
&(dest->options));
sprintf(value, "%d", type);
dest->num_options = cupsAddOption("printer-type", value,
dest->num_options,
&(dest->options));
}
cupsFreeOptions(num_options, options);
if (attr == NULL)
break;
}
ippDelete(response);
}
return (num_dests);
}