#include <cups/cups-private.h>
static int compare_strings(const char *, const char *, size_t);
static void do_command(http_t *, const char *, const char *);
static void show_help(const char *);
static void show_status(http_t *, const char *);
int
main(int argc,
char *argv[])
{
http_t *http;
char line[1024],
*params;
_cupsSetLocale(argv);
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
if (argc > 1)
{
do_command(http, argv[1], argv[2]);
}
else
{
_cupsLangPuts(stdout, _("lpc> "));
while (fgets(line, sizeof(line), stdin) != NULL)
{
for (params = line + strlen(line) - 1; params >= line;)
if (!isspace(*params & 255))
break;
else
*params-- = '\0';
for (params = line; isspace(*params & 255); params ++);
if (params > line)
_cups_strcpy(line, params);
if (!line[0])
{
_cupsLangPuts(stdout, _("lpc> "));
continue;
}
for (params = line; *params != '\0'; params ++)
if (isspace(*params & 255))
break;
while (isspace(*params & 255))
*params++ = '\0';
if (!compare_strings(line, "quit", 1) ||
!compare_strings(line, "exit", 2))
break;
if (*params == '\0')
do_command(http, line, NULL);
else
do_command(http, line, params);
_cupsLangPuts(stdout, _("lpc> "));
}
}
httpClose(http);
return (0);
}
static int
compare_strings(const char *s,
const char *t,
size_t tmin)
{
size_t slen;
slen = strlen(s);
if (slen < tmin)
return (-1);
else
return (strncmp(s, t, slen));
}
static void
do_command(http_t *http,
const char *command,
const char *params)
{
if (!compare_strings(command, "status", 4))
show_status(http, params);
else if (!compare_strings(command, "help", 1) || !strcmp(command, "?"))
show_help(params);
else
_cupsLangPrintf(stdout,
_("%s is not implemented by the CUPS version of lpc."),
command);
}
static void
show_help(const char *command)
{
if (!command)
{
_cupsLangPrintf(stdout,
_("Commands may be abbreviated. Commands are:\n"
"\n"
"exit help quit status ?"));
}
else if (!compare_strings(command, "help", 1) || !strcmp(command, "?"))
_cupsLangPrintf(stdout, _("help\t\tGet help on commands."));
else if (!compare_strings(command, "status", 4))
_cupsLangPrintf(stdout, _("status\t\tShow status of daemon and queue."));
else
_cupsLangPrintf(stdout, _("?Invalid help command unknown."));
}
static void
show_status(http_t *http,
const char *dests)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
char *printer,
*device,
*delimiter;
ipp_pstate_t pstate;
int accepting;
int jobcount;
const char *dptr,
*ptr;
int match;
static const char *requested[] =
{
"device-uri",
"printer-is-accepting-jobs",
"printer-name",
"printer-state",
"queued-job-count"
};
DEBUG_printf(("show_status(http=%p, dests=\"%s\")\n", http, dests));
if (http == NULL)
return;
request = ippNewRequest(CUPS_GET_PRINTERS);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", sizeof(requested) / sizeof(requested[0]),
NULL, requested);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
DEBUG_puts("show_status: request succeeded...");
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;
printer = NULL;
device = "file:/dev/null";
pstate = IPP_PRINTER_IDLE;
jobcount = 0;
accepting = 1;
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
if (!strcmp(attr->name, "device-uri") &&
attr->value_tag == IPP_TAG_URI)
device = 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-name") &&
attr->value_tag == IPP_TAG_NAME)
printer = attr->values[0].string.text;
else if (!strcmp(attr->name, "printer-state") &&
attr->value_tag == IPP_TAG_ENUM)
pstate = (ipp_pstate_t)attr->values[0].integer;
else if (!strcmp(attr->name, "queued-job-count") &&
attr->value_tag == IPP_TAG_INTEGER)
jobcount = attr->values[0].integer;
attr = attr->next;
}
if (printer == NULL)
{
if (attr == NULL)
break;
else
continue;
}
if (dests != NULL && !strcmp(dests, "all"))
dests = NULL;
match = dests == NULL;
if (dests != NULL)
{
for (dptr = dests; *dptr != '\0';)
{
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
for (ptr = printer;
*ptr != '\0' && *dptr != '\0' && *ptr == *dptr;
ptr ++, dptr ++)
;
if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' ||
isspace(*dptr & 255)))
{
match = 1;
break;
}
while (!isspace(*dptr & 255) && *dptr != '\0')
dptr ++;
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
}
}
if (match)
{
printf("%s:\n", printer);
if (!strncmp(device, "file:", 5))
_cupsLangPrintf(stdout,
_("\tprinter is on device \'%s\' speed -1"),
device + 5);
else
{
if ((delimiter = strchr(device, ':')) != NULL )
{
*delimiter = '\0';
_cupsLangPrintf(stdout,
_("\tprinter is on device \'%s\' speed -1"),
device);
}
}
if (accepting)
_cupsLangPuts(stdout, _("\tqueuing is enabled"));
else
_cupsLangPuts(stdout, _("\tqueuing is disabled"));
if (pstate != IPP_PRINTER_STOPPED)
_cupsLangPuts(stdout, _("\tprinting is enabled"));
else
_cupsLangPuts(stdout, _("\tprinting is disabled"));
if (jobcount == 0)
_cupsLangPuts(stdout, _("\tno entries"));
else
_cupsLangPrintf(stdout, _("\t%d entries"), jobcount);
_cupsLangPuts(stdout, _("\tdaemon present"));
}
if (attr == NULL)
break;
}
ippDelete(response);
}
}