#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <cups/http-private.h>
#include <cups/string.h>
#include <cups/cups.h>
#include <cups/i18n.h>
#include <cups/debug.h>
static void check_dest(const char *, http_t *, const char *, int *,
cups_dest_t **);
static http_t *connect_server(const char *, http_t *);
static int show_accepting(http_t *, const char *, int, cups_dest_t *);
static int show_classes(http_t *, const char *);
static void show_default(int, cups_dest_t *);
static int show_devices(http_t *, const char *, int, cups_dest_t *);
static int show_jobs(http_t *, const char *, const char *, int, int,
const char *);
static int show_printers(http_t *, const char *, int, cups_dest_t *, int);
static void show_scheduler(http_t *);
int
main(int argc,
char *argv[])
{
int i,
status;
http_t *http;
int num_dests;
cups_dest_t *dests;
int long_status;
int ranking;
const char *which;
char op;
_cupsSetLocale(argv);
http = NULL;
num_dests = 0;
dests = NULL;
long_status = 0;
ranking = 0;
status = 0;
which = "not-completed";
op = 0;
for (i = 1; i < argc; i ++)
if (argv[i][0] == '-')
switch (argv[i][1])
{
case 'D' :
long_status = 1;
break;
case 'E' :
#ifdef HAVE_SSL
cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
if (http)
httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
#else
_cupsLangPrintf(stderr,
_("%s: Sorry, no encryption support compiled in!\n"),
argv[0]);
#endif
break;
case 'P' :
op = 'P';
break;
case 'R' :
ranking = 1;
break;
case 'S' :
op = 'S';
if (!argv[i][2])
i ++;
break;
case 'U' :
if (argv[i][2] != '\0')
cupsSetUser(argv[i] + 2);
else
{
i ++;
if (i >= argc)
{
_cupsLangPrintf(stderr,
_("%s: Error - expected username after "
"\'-U\' option!\n"),
argv[0]);
return (1);
}
cupsSetUser(argv[i]);
}
break;
case 'W' :
if (argv[i][2])
which = argv[i] + 2;
else
{
i ++;
if (i >= argc)
{
_cupsLangPrintf(stderr,
_("%s: Error - need \"completed\", "
"\"not-completed\", or \"all\" after "
"\'-W\' option!\n"),
argv[0]);
return (1);
}
which = argv[i];
}
if (strcmp(which, "completed") && strcmp(which, "not-completed") &&
strcmp(which, "all"))
{
_cupsLangPrintf(stderr,
_("%s: Error - need \"completed\", "
"\"not-completed\", or \"all\" after "
"\'-W\' option!\n"),
argv[0]);
return (1);
}
break;
case 'a' :
op = 'a';
http = connect_server(argv[0], http);
if (argv[i][2] != '\0')
{
check_dest(argv[0], http, argv[i] + 2, &num_dests, &dests);
status |= show_accepting(http, argv[i] + 2, num_dests, dests);
}
else if ((i + 1) < argc && argv[i + 1][0] != '-')
{
i ++;
check_dest(argv[0], http, argv[i], &num_dests, &dests);
status |= show_accepting(http, argv[i], num_dests, dests);
}
else
{
if (num_dests == 0)
num_dests = cupsGetDests2(http, &dests);
status |= show_accepting(http, NULL, num_dests, dests);
}
break;
#ifdef __sgi
case 'b' :
op = 'b';
http = connect_server(argv[0], http);
if (argv[i][2] != '\0')
{
check_dest(argv[0], http, argv[i] + 2, &num_dests, &dests);
puts("");
status |= show_jobs(http, argv[i] + 2, NULL, 3, ranking, which);
}
else
{
_cupsLangPrintf(stderr,
_("%s: Error - expected destination after "
"\'-b\' option!\n"),
argv[0]);
return (1);
}
break;
#endif
case 'c' :
op = 'c';
http = connect_server(argv[0], http);
if (argv[i][2] != '\0')
{
check_dest(argv[0], http, argv[i] + 2, &num_dests, &dests);
status |= show_classes(http, argv[i] + 2);
}
else if ((i + 1) < argc && argv[i + 1][0] != '-')
{
i ++;
check_dest(argv[0], http, argv[i], &num_dests, &dests);
status |= show_classes(http, argv[i]);
}
else
status |= show_classes(http, NULL);
break;
case 'd' :
op = 'd';
http = connect_server(argv[0], http);
if (num_dests == 0)
num_dests = cupsGetDests2(http, &dests);
show_default(num_dests, dests);
break;
case 'f' :
op = 'f';
if (!argv[i][2])
i ++;
break;
case 'h' :
if (http)
{
httpClose(http);
http = NULL;
}
if (argv[i][2] != '\0')
cupsSetServer(argv[i] + 2);
else
{
i ++;
if (i >= argc)
{
_cupsLangPrintf(stderr,
_("%s: Error - expected hostname after "
"\'-h\' option!\n"),
argv[0]);
return (1);
}
cupsSetServer(argv[i]);
}
break;
case 'l' :
#ifdef __sgi
op = 'l';
http = connect_server(argv[0], http);
if (argv[i][2] != '\0')
{
check_dest(argv[0], http, argv[i] + 2, &num_dests, &dests);
status |= show_jobs(http, argv[i] + 2, NULL, 3, ranking, which);
}
else
#endif
long_status = 2;
break;
case 'o' :
op = 'o';
http = connect_server(argv[0], http);
if (argv[i][2] != '\0')
{
check_dest(argv[0], http, argv[i] + 2, &num_dests, &dests);
status |= show_jobs(http, argv[i] + 2, NULL, long_status,
ranking, which);
}
else if ((i + 1) < argc && argv[i + 1][0] != '-')
{
i ++;
check_dest(argv[0], http, argv[i], &num_dests, &dests);
status |= show_jobs(http, argv[i], NULL, long_status,
ranking, which);
}
else
status |= show_jobs(http, NULL, NULL, long_status,
ranking, which);
break;
case 'p' :
op = 'p';
http = connect_server(argv[0], http);
if (argv[i][2] != '\0')
{
check_dest(argv[0], http, argv[i] + 2, &num_dests, &dests);
status |= show_printers(http, argv[i] + 2, num_dests, dests, long_status);
}
else if ((i + 1) < argc && argv[i + 1][0] != '-')
{
i ++;
check_dest(argv[0], http, argv[i], &num_dests, &dests);
status |= show_printers(http, argv[i], num_dests, dests, long_status);
}
else
{
if (num_dests == 0)
num_dests = cupsGetDests2(http, &dests);
status |= show_printers(http, NULL, num_dests, dests, long_status);
}
break;
case 'r' :
op = 'r';
http = connect_server(argv[0], http);
show_scheduler(http);
break;
case 's' :
op = 's';
http = connect_server(argv[0], http);
if (num_dests == 0)
num_dests = cupsGetDests2(http, &dests);
show_default(num_dests, dests);
status |= show_classes(http, NULL);
status |= show_devices(http, NULL, num_dests, dests);
break;
case 't' :
op = 't';
http = connect_server(argv[0], http);
if (num_dests == 0)
num_dests = cupsGetDests2(http, &dests);
show_scheduler(http);
show_default(num_dests, dests);
status |= show_classes(http, NULL);
status |= show_devices(http, NULL, num_dests, dests);
status |= show_accepting(http, NULL, num_dests, dests);
status |= show_printers(http, NULL, num_dests, dests, long_status);
status |= show_jobs(http, NULL, NULL, long_status, ranking, which);
break;
case 'u' :
op = 'u';
http = connect_server(argv[0], http);
if (argv[i][2] != '\0')
status |= show_jobs(http, NULL, argv[i] + 2, long_status,
ranking, which);
else if ((i + 1) < argc && argv[i + 1][0] != '-')
{
i ++;
status |= show_jobs(http, NULL, argv[i], long_status,
ranking, which);
}
else
status |= show_jobs(http, NULL, NULL, long_status,
ranking, which);
break;
case 'v' :
op = 'v';
http = connect_server(argv[0], http);
if (argv[i][2] != '\0')
{
check_dest(argv[0], http, argv[i] + 2, &num_dests, &dests);
status |= show_devices(http, argv[i] + 2, num_dests, dests);
}
else if ((i + 1) < argc && argv[i + 1][0] != '-')
{
i ++;
check_dest(argv[0], http, argv[i], &num_dests, &dests);
status |= show_devices(http, argv[i], num_dests, dests);
}
else
{
if (num_dests == 0)
num_dests = cupsGetDests2(http, &dests);
status |= show_devices(http, NULL, num_dests, dests);
}
break;
default :
_cupsLangPrintf(stderr,
_("%s: Error - unknown option \'%c\'!\n"),
argv[0], argv[i][1]);
return (1);
}
else
{
http = connect_server(argv[0], http);
status |= show_jobs(http, argv[i], NULL, long_status, ranking, which);
op = 'o';
}
if (!op)
{
http = connect_server(argv[0], http);
status |= show_jobs(http, NULL, cupsUser(), long_status, ranking, which);
}
return (status);
}
static void
check_dest(const char *command,
http_t *http,
const char *name,
int *num_dests,
cups_dest_t **dests)
{
const char *dptr;
char *pptr,
printer[128];
if (*num_dests == 0)
*num_dests = cupsGetDests2(http, dests);
for (dptr = name; *dptr != '\0';)
{
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
for (pptr = printer; !isspace(*dptr & 255) && *dptr != ',' && *dptr != '\0';)
{
if ((pptr - printer) < (sizeof(printer) - 1))
*pptr++ = *dptr++;
else
{
_cupsLangPrintf(stderr,
_("%s: Invalid destination name in list \"%s\"!\n"),
command, name);
exit(1);
}
}
*pptr = '\0';
if (cupsGetDest(printer, NULL, *num_dests, *dests) == NULL)
{
_cupsLangPrintf(stderr,
_("%s: Unknown destination \"%s\"!\n"), command, printer);
exit(1);
}
}
}
static http_t *
connect_server(const char *command,
http_t *http)
{
if (!http)
{
http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption());
if (http == NULL)
{
_cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"), command);
exit(1);
}
}
return (http);
}
static int
show_accepting(http_t *http,
const char *printers,
int num_dests,
cups_dest_t *dests)
{
int i;
ipp_t *request,
*response;
ipp_attribute_t *attr;
const char *printer,
*message;
int accepting;
time_t ptime;
struct tm *pdate;
char printer_state_time[255];
const char *dptr,
*ptr;
int match;
static const char *pattrs[] =
{
"printer-name",
"printer-state-change-time",
"printer-state-message",
"printer-is-accepting-jobs"
};
DEBUG_printf(("show_accepting(%p, %p)\n", http, printers));
if (http == NULL)
return (1);
if (printers != NULL && !strcmp(printers, "all"))
printers = NULL;
request = ippNewRequest(CUPS_GET_PRINTERS);
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)
{
DEBUG_puts("show_accepting: request succeeded...");
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
ippDelete(response);
return (1);
}
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;
message = NULL;
accepting = 1;
ptime = 0;
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
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-change-time") &&
attr->value_tag == IPP_TAG_INTEGER)
ptime = (time_t)attr->values[0].integer;
else if (!strcmp(attr->name, "printer-state-message") &&
attr->value_tag == IPP_TAG_TEXT)
message = 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;
attr = attr->next;
}
if (printer == NULL)
{
if (attr == NULL)
break;
else
continue;
}
match = printers == NULL;
if (printers != NULL)
{
for (dptr = printers; *dptr != '\0';)
{
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
for (ptr = printer;
*ptr != '\0' && *dptr != '\0' && tolower(*ptr & 255) == tolower(*dptr & 255);
ptr ++, dptr ++);
if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' || isspace(*dptr & 255)))
{
match = 1;
break;
}
while (!isspace(*dptr & 255) && *dptr != ',' && *dptr != '\0')
dptr ++;
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
}
}
if (match)
{
pdate = localtime(&ptime);
strftime(printer_state_time, sizeof(printer_state_time), "%c", pdate);
if (accepting)
_cupsLangPrintf(stdout, _("%s accepting requests since %s\n"),
printer, printer_state_time);
else
_cupsLangPrintf(stdout, _("%s not accepting requests since %s -\n"
"\t%s\n"),
printer, printer_state_time,
message == NULL ? "reason unknown" : message);
for (i = 0; i < num_dests; i ++)
if (!strcasecmp(dests[i].name, printer) && dests[i].instance)
{
if (accepting)
_cupsLangPrintf(stdout, _("%s/%s accepting requests since %s\n"),
printer, dests[i].instance, printer_state_time);
else
_cupsLangPrintf(stdout, _("%s/%s not accepting requests since "
"%s -\n\t%s\n"),
printer, dests[i].instance, printer_state_time,
message == NULL ? "reason unknown" : message);
}
}
if (attr == NULL)
break;
}
ippDelete(response);
}
else
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
return (1);
}
return (0);
}
static int
show_classes(http_t *http,
const char *dests)
{
int i;
ipp_t *request,
*response,
*response2;
http_t *http2;
ipp_attribute_t *attr;
const char *printer,
*printer_uri;
ipp_attribute_t *members;
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
server[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
const char *dptr,
*ptr;
int match;
static const char *cattrs[] =
{
"printer-name",
"printer-uri-supported",
"member-names"
};
DEBUG_printf(("show_classes(%p, %p)\n", http, dests));
if (http == NULL)
return (1);
if (dests != NULL && !strcmp(dests, "all"))
dests = NULL;
request = ippNewRequest(CUPS_GET_CLASSES);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", sizeof(cattrs) / sizeof(cattrs[0]),
NULL, cattrs);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
DEBUG_puts("show_classes: request succeeded...");
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
ippDelete(response);
return (1);
}
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;
printer_uri = NULL;
members = NULL;
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
if (!strcmp(attr->name, "printer-name") &&
attr->value_tag == IPP_TAG_NAME)
printer = attr->values[0].string.text;
if (!strcmp(attr->name, "printer-uri-supported") &&
attr->value_tag == IPP_TAG_URI)
printer_uri = attr->values[0].string.text;
if (!strcmp(attr->name, "member-names") &&
attr->value_tag == IPP_TAG_NAME)
members = attr;
attr = attr->next;
}
response2 = NULL;
if (members == NULL && printer_uri != NULL)
{
httpSeparateURI(HTTP_URI_CODING_ALL, printer_uri, method, sizeof(method),
username, sizeof(username), server, sizeof(server),
&port, resource, sizeof(resource));
if (!strcasecmp(server, http->hostname))
http2 = http;
else
http2 = httpConnectEncrypt(server, port, cupsEncryption());
if (http2 != NULL)
{
request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, printer_uri);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes",
sizeof(cattrs) / sizeof(cattrs[0]),
NULL, cattrs);
if ((response2 = cupsDoRequest(http2, request, "/")) != NULL)
members = ippFindAttribute(response2, "member-names", IPP_TAG_NAME);
if (http2 != http)
httpClose(http2);
}
}
if (printer == NULL)
{
if (response2)
ippDelete(response2);
if (attr == NULL)
break;
else
continue;
}
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' && tolower(*ptr & 255) == tolower(*dptr & 255);
ptr ++, dptr ++);
if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' || isspace(*dptr & 255)))
{
match = 1;
break;
}
while (!isspace(*dptr & 255) && *dptr != ',' && *dptr != '\0')
dptr ++;
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
}
}
if (match)
{
_cupsLangPrintf(stdout, _("members of class %s:\n"), printer);
if (members)
{
for (i = 0; i < members->num_values; i ++)
_cupsLangPrintf(stdout, "\t%s\n",
members->values[i].string.text);
}
else
_cupsLangPuts(stdout, "\tunknown\n");
}
if (response2)
ippDelete(response2);
if (attr == NULL)
break;
}
ippDelete(response);
}
else
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
return (1);
}
return (0);
}
static void
show_default(int num_dests,
cups_dest_t *dests)
{
cups_dest_t *dest;
const char *printer,
*val;
if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL)
{
if (dest->instance)
_cupsLangPrintf(stdout, _("system default destination: %s/%s\n"),
dest->name, dest->instance);
else
_cupsLangPrintf(stdout, _("system default destination: %s\n"),
dest->name);
}
else
{
val = NULL;
if ((printer = getenv("LPDEST")) == NULL)
{
if ((printer = getenv("PRINTER")) != NULL)
{
if (!strcmp(printer, "lp"))
printer = NULL;
else
val = "PRINTER";
}
}
else
val = "LPDEST";
if (printer && !cupsGetDest(printer, NULL, num_dests, dests))
_cupsLangPrintf(stdout,
_("lpstat: error - %s environment variable names "
"non-existent destination \"%s\"!\n"),
val, printer);
else
_cupsLangPuts(stdout, _("no system default destination\n"));
}
}
static int
show_devices(http_t *http,
const char *printers,
int num_dests,
cups_dest_t *dests)
{
int i;
ipp_t *request,
*response;
ipp_attribute_t *attr;
const char *printer,
*uri,
*device,
*dptr,
*ptr;
int match;
static const char *pattrs[] =
{
"printer-name",
"printer-uri-supported",
"device-uri"
};
DEBUG_printf(("show_devices(%p, %p)\n", http, dests));
if (http == NULL)
return (1);
if (printers != NULL && !strcmp(printers, "all"))
printers = NULL;
request = ippNewRequest(CUPS_GET_PRINTERS);
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)
{
DEBUG_puts("show_devices: request succeeded...");
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
ippDelete(response);
return (1);
}
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 = NULL;
uri = NULL;
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
if (!strcmp(attr->name, "printer-name") &&
attr->value_tag == IPP_TAG_NAME)
printer = attr->values[0].string.text;
if (!strcmp(attr->name, "printer-uri-supported") &&
attr->value_tag == IPP_TAG_URI)
uri = attr->values[0].string.text;
if (!strcmp(attr->name, "device-uri") &&
attr->value_tag == IPP_TAG_URI)
device = attr->values[0].string.text;
attr = attr->next;
}
if (printer == NULL)
{
if (attr == NULL)
break;
else
continue;
}
match = printers == NULL;
if (printers != NULL)
{
for (dptr = printers; *dptr != '\0';)
{
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
for (ptr = printer;
*ptr != '\0' && *dptr != '\0' && tolower(*ptr & 255) == tolower(*dptr & 255);
ptr ++, dptr ++);
if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' || isspace(*dptr & 255)))
{
match = 1;
break;
}
while (!isspace(*dptr & 255) && *dptr != ',' && *dptr != '\0')
dptr ++;
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
}
}
if (match)
{
#ifdef __osf__
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
hostname[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
if (device == NULL)
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method),
username, sizeof(username), hostname,
sizeof(hostname), &port, resource, sizeof(resource));
_cupsLangPrintf(stdout,
_("Output for printer %s is sent to remote "
"printer %s on %s\n"),
printer, strrchr(resource, '/') + 1, hostname);
}
else if (!strncmp(device, "file:", 5))
_cupsLangPrintf(stdout,
_("Output for printer %s is sent to %s\n"),
printer, device + 5);
else
_cupsLangPrintf(stdout,
_("Output for printer %s is sent to %s\n"),
printer, device);
for (i = 0; i < num_dests; i ++)
if (!strcasecmp(printer, dests[i].name) && dests[i].instance)
{
if (device == NULL)
_cupsLangPrintf(stdout,
_("Output for printer %s/%s is sent to "
"remote printer %s on %s\n"),
printer, dests[i].instance,
strrchr(resource, '/') + 1, hostname);
else if (!strncmp(device, "file:", 5))
_cupsLangPrintf(stdout,
_("Output for printer %s/%s is sent to %s\n"),
printer, dests[i].instance, device + 5);
else
_cupsLangPrintf(stdout,
_("Output for printer %s/%s is sent to %s\n"),
printer, dests[i].instance, device);
}
#else
if (device == NULL)
_cupsLangPrintf(stdout, _("device for %s: %s\n"),
printer, uri);
else if (!strncmp(device, "file:", 5))
_cupsLangPrintf(stdout, _("device for %s: %s\n"),
printer, device + 5);
else
_cupsLangPrintf(stdout, _("device for %s: %s\n"),
printer, device);
for (i = 0; i < num_dests; i ++)
if (!strcasecmp(printer, dests[i].name) && dests[i].instance)
{
if (device == NULL)
_cupsLangPrintf(stdout, _("device for %s/%s: %s\n"),
printer, dests[i].instance, uri);
else if (!strncmp(device, "file:", 5))
_cupsLangPrintf(stdout, _("device for %s/%s: %s\n"),
printer, dests[i].instance, device + 5);
else
_cupsLangPrintf(stdout, _("device for %s/%s: %s\n"),
printer, dests[i].instance, device);
}
#endif
}
if (attr == NULL)
break;
}
ippDelete(response);
}
else
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
return (1);
}
return (0);
}
static int
show_jobs(http_t *http,
const char *dests,
const char *users,
int long_status,
int ranking,
const char *which)
{
int i;
ipp_t *request,
*response;
ipp_attribute_t *attr,
*reasons;
const char *dest,
*username,
*title;
int rank,
jobid,
size;
time_t jobtime;
struct tm *jobdate;
const char *dptr,
*ptr;
int match;
char temp[255],
date[255];
static const char *jattrs[] =
{
"job-id",
"job-k-octets",
"job-name",
"time-at-creation",
"job-printer-uri",
"job-originating-user-name",
"job-state-reasons"
};
DEBUG_printf(("show_jobs(%p, %p, %p)\n", http, dests, users));
if (http == NULL)
return (1);
if (dests != NULL && !strcmp(dests, "all"))
dests = NULL;
request = ippNewRequest(IPP_GET_JOBS);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]),
NULL, jattrs);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
NULL, "ipp://localhost/jobs/");
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
NULL, which);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
ippDelete(response);
return (1);
}
rank = -1;
for (attr = response->attrs; attr != NULL; attr = attr->next)
{
while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
attr = attr->next;
if (attr == NULL)
break;
jobid = 0;
size = 0;
username = NULL;
dest = NULL;
jobtime = 0;
title = "no title";
reasons = NULL;
while (attr != NULL && attr->group_tag == IPP_TAG_JOB)
{
if (!strcmp(attr->name, "job-id") &&
attr->value_tag == IPP_TAG_INTEGER)
jobid = attr->values[0].integer;
if (!strcmp(attr->name, "job-k-octets") &&
attr->value_tag == IPP_TAG_INTEGER)
size = attr->values[0].integer;
if (!strcmp(attr->name, "time-at-creation") &&
attr->value_tag == IPP_TAG_INTEGER)
jobtime = attr->values[0].integer;
if (!strcmp(attr->name, "job-printer-uri") &&
attr->value_tag == IPP_TAG_URI)
if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
dest ++;
if (!strcmp(attr->name, "job-originating-user-name") &&
attr->value_tag == IPP_TAG_NAME)
username = attr->values[0].string.text;
if (!strcmp(attr->name, "job-name") &&
attr->value_tag == IPP_TAG_NAME)
title = attr->values[0].string.text;
if (!strcmp(attr->name, "job-state-reasons") &&
attr->value_tag == IPP_TAG_KEYWORD)
reasons = attr;
attr = attr->next;
}
if (dest == NULL || jobid == 0)
{
if (attr == NULL)
break;
else
continue;
}
match = (dests == NULL && users == NULL);
rank ++;
if (dests != NULL)
{
for (dptr = dests; *dptr != '\0';)
{
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
for (ptr = dest;
*ptr != '\0' && *dptr != '\0' && tolower(*ptr & 255) == tolower(*dptr & 255);
ptr ++, dptr ++);
if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' || isspace(*dptr & 255)))
{
match = 1;
break;
}
while (!isspace(*dptr & 255) && *dptr != ',' && *dptr != '\0')
dptr ++;
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
}
}
if (users != NULL && username != NULL)
{
for (dptr = users; *dptr != '\0';)
{
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
for (ptr = username;
*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 != ',' && *dptr != '\0')
dptr ++;
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
}
}
if (match)
{
jobdate = localtime(&jobtime);
snprintf(temp, sizeof(temp), "%s-%d", dest, jobid);
if (long_status == 3)
{
if (!strftime(date, sizeof(date), "%b %d %H:%M", jobdate))
strcpy(date, "Unknown");
_cupsLangPrintf(stdout, "%s;%s;%d;%s;%s\n",
temp, username ? username : "unknown",
size, title ? title : "unknown", date);
}
else
{
if (!strftime(date, sizeof(date), "%c", jobdate))
strcpy(date, "Unknown");
if (ranking)
_cupsLangPrintf(stdout, "%3d %-21s %-13s %8.0f %s\n",
rank, temp, username ? username : "unknown",
1024.0 * size, date);
else
_cupsLangPrintf(stdout, "%-23s %-13s %8.0f %s\n",
temp, username ? username : "unknown",
1024.0 * size, date);
if (long_status)
{
if (reasons)
{
_cupsLangPuts(stdout, _("\tAlerts:"));
for (i = 0; i < reasons->num_values; i ++)
_cupsLangPrintf(stdout, " %s",
reasons->values[i].string.text);
_cupsLangPuts(stdout, "\n");
}
_cupsLangPrintf(stdout, _("\tqueued for %s\n"), dest);
}
}
}
if (attr == NULL)
break;
}
ippDelete(response);
}
else
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
return (1);
}
return (0);
}
static int
show_printers(http_t *http,
const char *printers,
int num_dests,
cups_dest_t *dests,
int long_status)
{
int i, j;
ipp_t *request,
*response,
*jobs;
ipp_attribute_t *attr,
*jobattr,
*reasons;
const char *printer,
*message,
*description,
*location,
*make_model,
*uri;
ipp_attribute_t *allowed,
*denied;
ipp_pstate_t pstate;
cups_ptype_t ptype;
time_t ptime;
struct tm *pdate;
int jobid;
const char *dptr,
*ptr;
int match;
char printer_uri[HTTP_MAX_URI],
printer_state_time[255];
const char *root;
static const char *pattrs[] =
{
"printer-name",
"printer-state",
"printer-state-message",
"printer-state-reasons",
"printer-state-change-time",
"printer-type",
"printer-info",
"printer-location",
"printer-make-and-model",
"printer-uri-supported",
"requesting-user-name-allowed",
"requesting-user-name-denied"
};
static const char *jattrs[] =
{
"job-id"
};
DEBUG_printf(("show_printers(%p, %p)\n", http, dests));
if (http == NULL)
return (1);
if ((root = getenv("CUPS_SERVERROOT")) == NULL)
root = CUPS_SERVERROOT;
if (printers != NULL && !strcmp(printers, "all"))
printers = NULL;
request = ippNewRequest(CUPS_GET_PRINTERS);
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)
{
DEBUG_puts("show_printers: request succeeded...");
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
ippDelete(response);
return (1);
}
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;
ptime = 0;
ptype = CUPS_PRINTER_LOCAL;
pstate = IPP_PRINTER_IDLE;
message = NULL;
description = NULL;
location = NULL;
make_model = NULL;
reasons = NULL;
uri = NULL;
jobid = 0;
allowed = NULL;
denied = NULL;
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
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, "printer-type") &&
attr->value_tag == IPP_TAG_ENUM)
ptype = (cups_ptype_t)attr->values[0].integer;
else if (!strcmp(attr->name, "printer-state-message") &&
attr->value_tag == IPP_TAG_TEXT)
message = attr->values[0].string.text;
else if (!strcmp(attr->name, "printer-state-change-time") &&
attr->value_tag == IPP_TAG_INTEGER)
ptime = (time_t)attr->values[0].integer;
else if (!strcmp(attr->name, "printer-info") &&
attr->value_tag == IPP_TAG_TEXT)
description = attr->values[0].string.text;
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-uri-supported") &&
attr->value_tag == IPP_TAG_URI)
uri = attr->values[0].string.text;
else if (!strcmp(attr->name, "printer-state-reasons") &&
attr->value_tag == IPP_TAG_KEYWORD)
reasons = attr;
else if (!strcmp(attr->name, "requesting-user-name-allowed") &&
attr->value_tag == IPP_TAG_NAME)
allowed = attr;
else if (!strcmp(attr->name, "requesting-user-name-denied") &&
attr->value_tag == IPP_TAG_NAME)
denied = attr;
attr = attr->next;
}
if (printer == NULL)
{
if (attr == NULL)
break;
else
continue;
}
match = printers == NULL;
if (printers != NULL)
{
for (dptr = printers; *dptr != '\0';)
{
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
for (ptr = printer;
*ptr != '\0' && *dptr != '\0' && tolower(*ptr & 255) == tolower(*dptr & 255);
ptr ++, dptr ++);
if (*ptr == '\0' && (*dptr == '\0' || *dptr == ',' || isspace(*dptr & 255)))
{
match = 1;
break;
}
while (!isspace(*dptr & 255) && *dptr != ',' && *dptr != '\0')
dptr ++;
while (isspace(*dptr & 255) || *dptr == ',')
dptr ++;
if (*dptr == '\0')
break;
}
}
if (match)
{
if (pstate == IPP_PRINTER_PROCESSING)
{
request = ippNewRequest(IPP_GET_JOBS);
request->request.op.operation_id = IPP_GET_JOBS;
request->request.op.request_id = 1;
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes",
sizeof(jattrs) / sizeof(jattrs[0]), NULL, jattrs);
httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
"ipp", NULL, "localhost", 0, "/printers/%s", printer);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, printer_uri);
if ((jobs = cupsDoRequest(http, request, "/")) != NULL)
{
jobid = 0;
for (jobattr = jobs->attrs; jobattr; jobattr = jobattr->next)
{
if (!jobattr->name)
continue;
if (!strcmp(jobattr->name, "job-id") &&
jobattr->value_tag == IPP_TAG_INTEGER)
jobid = jobattr->values[0].integer;
else if (!strcmp(jobattr->name, "job-state") &&
jobattr->value_tag == IPP_TAG_ENUM &&
jobattr->values[0].integer == IPP_JOB_PROCESSING)
break;
}
ippDelete(jobs);
}
}
pdate = localtime(&ptime);
strftime(printer_state_time, sizeof(printer_state_time), "%c", pdate);
switch (pstate)
{
case IPP_PRINTER_IDLE :
_cupsLangPrintf(stdout,
_("printer %s is idle. enabled since %s\n"),
printer, printer_state_time);
break;
case IPP_PRINTER_PROCESSING :
_cupsLangPrintf(stdout,
_("printer %s now printing %s-%d. "
"enabled since %s\n"),
printer, printer, jobid, printer_state_time);
break;
case IPP_PRINTER_STOPPED :
_cupsLangPrintf(stdout,
_("printer %s disabled since %s -\n"),
printer, printer_state_time);
break;
}
if ((message && *message) || pstate == IPP_PRINTER_STOPPED)
{
if (!message || !*message)
_cupsLangPuts(stdout, _("\treason unknown\n"));
else
_cupsLangPrintf(stdout, "\t%s\n", message);
}
if (long_status > 1)
_cupsLangPuts(stdout,
_("\tForm mounted:\n"
"\tContent types: any\n"
"\tPrinter types: unknown\n"));
if (long_status)
{
_cupsLangPrintf(stdout, _("\tDescription: %s\n"),
description ? description : "");
if (reasons)
{
_cupsLangPuts(stdout, _("\tAlerts:"));
for (j = 0; j < reasons->num_values; j ++)
_cupsLangPrintf(stdout, " %s",
reasons->values[j].string.text);
_cupsLangPuts(stdout, "\n");
}
}
if (long_status > 1)
{
_cupsLangPrintf(stdout, _("\tLocation: %s\n"),
location ? location : "");
if (ptype & CUPS_PRINTER_REMOTE)
{
_cupsLangPuts(stdout, _("\tConnection: remote\n"));
if (make_model && !strstr(make_model, "System V Printer") &&
!strstr(make_model, "Raw Printer") && uri)
_cupsLangPrintf(stdout, _("\tInterface: %s.ppd\n"),
uri);
}
else
{
_cupsLangPuts(stdout, _("\tConnection: direct\n"));
if (make_model && strstr(make_model, "System V Printer"))
_cupsLangPrintf(stdout,
_("\tInterface: %s/interfaces/%s\n"),
root, printer);
else if (make_model && !strstr(make_model, "Raw Printer"))
_cupsLangPrintf(stdout,
_("\tInterface: %s/ppd/%s.ppd\n"), root, printer);
}
_cupsLangPuts(stdout, _("\tOn fault: no alert\n"));
_cupsLangPuts(stdout, _("\tAfter fault: continue\n"));
if (allowed)
{
_cupsLangPuts(stdout, _("\tUsers allowed:\n"));
for (j = 0; j < allowed->num_values; j ++)
_cupsLangPrintf(stdout, "\t\t%s\n",
allowed->values[j].string.text);
}
else if (denied)
{
_cupsLangPuts(stdout, _("\tUsers denied:\n"));
for (j = 0; j < denied->num_values; j ++)
_cupsLangPrintf(stdout, "\t\t%s\n",
denied->values[j].string.text);
}
else
{
_cupsLangPuts(stdout, _("\tUsers allowed:\n"));
_cupsLangPuts(stdout, _("\t\t(all)\n"));
}
_cupsLangPuts(stdout, _("\tForms allowed:\n"));
_cupsLangPuts(stdout, _("\t\t(none)\n"));
_cupsLangPuts(stdout, _("\tBanner required\n"));
_cupsLangPuts(stdout, _("\tCharset sets:\n"));
_cupsLangPuts(stdout, _("\t\t(none)\n"));
_cupsLangPuts(stdout, _("\tDefault pitch:\n"));
_cupsLangPuts(stdout, _("\tDefault page size:\n"));
_cupsLangPuts(stdout, _("\tDefault port settings:\n"));
}
for (i = 0; i < num_dests; i ++)
if (!strcasecmp(printer, dests[i].name) && dests[i].instance)
{
switch (pstate)
{
case IPP_PRINTER_IDLE :
_cupsLangPrintf(stdout,
_("printer %s/%s is idle. "
"enabled since %s\n"),
printer, dests[i].instance,
printer_state_time);
break;
case IPP_PRINTER_PROCESSING :
_cupsLangPrintf(stdout,
_("printer %s/%s now printing %s-%d. "
"enabled since %s\n"),
printer, dests[i].instance, printer, jobid,
printer_state_time);
break;
case IPP_PRINTER_STOPPED :
_cupsLangPrintf(stdout,
_("printer %s/%s disabled since %s -\n"),
printer, dests[i].instance,
printer_state_time);
break;
}
if ((message && *message) || pstate == IPP_PRINTER_STOPPED)
{
if (!message || !*message)
_cupsLangPuts(stdout, _("\treason unknown\n"));
else
_cupsLangPrintf(stdout, "\t%s\n", message);
}
if (long_status > 1)
_cupsLangPuts(stdout,
_("\tForm mounted:\n"
"\tContent types: any\n"
"\tPrinter types: unknown\n"));
if (long_status)
{
_cupsLangPrintf(stdout, _("\tDescription: %s\n"),
description ? description : "");
if (reasons)
{
_cupsLangPuts(stdout, _("\tAlerts:"));
for (j = 0; j < reasons->num_values; j ++)
_cupsLangPrintf(stdout, " %s",
reasons->values[j].string.text);
_cupsLangPuts(stdout, "\n");
}
}
if (long_status > 1)
{
_cupsLangPrintf(stdout, _("\tLocation: %s\n"),
location ? location : "");
if (ptype & CUPS_PRINTER_REMOTE)
{
_cupsLangPuts(stdout, _("\tConnection: remote\n"));
if (make_model && !strstr(make_model, "System V Printer") &&
!strstr(make_model, "Raw Printer") && uri)
_cupsLangPrintf(stdout, _("\tInterface: %s.ppd\n"),
uri);
}
else
{
_cupsLangPuts(stdout, _("\tConnection: direct\n"));
if (make_model && strstr(make_model, "System V Printer"))
_cupsLangPrintf(stdout,
_("\tInterface: %s/interfaces/%s\n"),
root, printer);
else if (make_model && !strstr(make_model, "Raw Printer"))
_cupsLangPrintf(stdout,
_("\tInterface: %s/ppd/%s.ppd\n"), root, printer);
}
_cupsLangPuts(stdout, _("\tOn fault: no alert\n"));
_cupsLangPuts(stdout, _("\tAfter fault: continue\n"));
if (allowed)
{
_cupsLangPuts(stdout, _("\tUsers allowed:\n"));
for (j = 0; j < allowed->num_values; j ++)
_cupsLangPrintf(stdout, "\t\t%s\n",
allowed->values[j].string.text);
}
else if (denied)
{
_cupsLangPuts(stdout, _("\tUsers denied:\n"));
for (j = 0; j < denied->num_values; j ++)
_cupsLangPrintf(stdout, "\t\t%s\n",
denied->values[j].string.text);
}
else
{
_cupsLangPuts(stdout, _("\tUsers allowed:\n"));
_cupsLangPuts(stdout, _("\t\t(all)\n"));
}
_cupsLangPuts(stdout, _("\tForms allowed:\n"));
_cupsLangPuts(stdout, _("\t\t(none)\n"));
_cupsLangPuts(stdout, _("\tBanner required\n"));
_cupsLangPuts(stdout, _("\tCharset sets:\n"));
_cupsLangPuts(stdout, _("\t\t(none)\n"));
_cupsLangPuts(stdout, _("\tDefault pitch:\n"));
_cupsLangPuts(stdout, _("\tDefault page size:\n"));
_cupsLangPuts(stdout, _("\tDefault port settings:\n"));
}
}
}
if (attr == NULL)
break;
}
ippDelete(response);
}
else
{
_cupsLangPrintf(stderr, "lpstat: %s\n", cupsLastErrorString());
return (1);
}
return (0);
}
static void
show_scheduler(http_t *http)
{
if (http)
_cupsLangPuts(stdout, _("scheduler is running\n"));
else
_cupsLangPuts(stdout, _("scheduler is not running\n"));
}