#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <cups/string.h>
#include <cups/cups.h>
#include <cups/i18n.h>
#include <cups/debug.h>
static int show_devices(http_t *, int);
static int show_models(http_t *, int);
int
main(int argc,
char *argv[])
{
int i;
http_t *http;
int long_status;
_cupsSetLocale(argv);
http = NULL;
long_status = 0;
for (i = 1; i < argc; i ++)
if (argv[i][0] == '-')
switch (argv[i][1])
{
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 'l' :
long_status = 1;
break;
case 'm' :
if (!http)
{
http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption());
if (http == NULL)
{
_cupsLangPrintf(stderr,
_("lpinfo: Unable to connect to server: %s\n"),
strerror(errno));
return (1);
}
}
if (show_models(http, long_status))
return (1);
break;
case 'v' :
if (!http)
{
http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption());
if (http == NULL)
{
_cupsLangPrintf(stderr,
_("lpinfo: Unable to connect to server: %s\n"),
strerror(errno));
return (1);
}
}
if (show_devices(http, long_status))
return (1);
break;
case 'h' :
if (http)
{
httpClose(http);
http = NULL;
}
if (argv[i][2] != '\0')
cupsSetServer(argv[i] + 2);
else
{
i ++;
if (i >= argc)
{
_cupsLangPuts(stderr,
_("Error: need hostname after \'-h\' option!\n"));
return (1);
}
cupsSetServer(argv[i]);
}
break;
default :
_cupsLangPrintf(stderr, _("lpinfo: Unknown option \'%c\'!\n"),
argv[i][1]);
return (1);
}
else
{
_cupsLangPrintf(stderr, _("lpinfo: Unknown argument \'%s\'!\n"),
argv[i]);
return (1);
}
return (0);
}
static int
show_devices(http_t *http,
int long_status)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
const char *device_class,
*device_id,
*device_info,
*device_make,
*device_uri;
if (http == NULL)
return (1);
request = ippNewRequest(CUPS_GET_DEVICES);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
_cupsLangPrintf(stderr, "lpinfo: %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;
device_class = NULL;
device_info = NULL;
device_make = NULL;
device_uri = NULL;
device_id = "NONE";
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
if (!strcmp(attr->name, "device-class") &&
attr->value_tag == IPP_TAG_KEYWORD)
device_class = attr->values[0].string.text;
else if (!strcmp(attr->name, "device-info") &&
attr->value_tag == IPP_TAG_TEXT)
device_info = attr->values[0].string.text;
else if (!strcmp(attr->name, "device-make-and-model") &&
attr->value_tag == IPP_TAG_TEXT)
device_make = attr->values[0].string.text;
else if (!strcmp(attr->name, "device-uri") &&
attr->value_tag == IPP_TAG_URI)
device_uri = attr->values[0].string.text;
else if (!strcmp(attr->name, "device-id") &&
attr->value_tag == IPP_TAG_TEXT)
device_id = attr->values[0].string.text;
attr = attr->next;
}
if (device_class == NULL || device_info == NULL ||
device_make == NULL || device_uri == NULL)
{
if (attr == NULL)
break;
else
continue;
}
if (long_status)
{
_cupsLangPrintf(stdout,
_("Device: uri = %s\n"
" class = %s\n"
" info = %s\n"
" make-and-model = %s\n"
" device-id = %s\n"),
device_uri, device_class, device_info, device_make,
device_id);
}
else
_cupsLangPrintf(stdout, "%s %s\n", device_class, device_uri);
if (attr == NULL)
break;
}
ippDelete(response);
}
else
{
_cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
return (1);
}
return (0);
}
static int
show_models(http_t *http,
int long_status)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
const char *ppd_device_id,
*ppd_language,
*ppd_make,
*ppd_name;
if (http == NULL)
return (1);
request = ippNewRequest(CUPS_GET_PPDS);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
_cupsLangPrintf(stderr, "lpinfo: %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;
ppd_device_id = "NONE";
ppd_language = NULL;
ppd_make = NULL;
ppd_name = NULL;
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
if (!strcmp(attr->name, "ppd-device-id") &&
attr->value_tag == IPP_TAG_TEXT)
ppd_device_id = attr->values[0].string.text;
else if (!strcmp(attr->name, "ppd-natural-language") &&
attr->value_tag == IPP_TAG_LANGUAGE)
ppd_language = attr->values[0].string.text;
else if (!strcmp(attr->name, "ppd-make-and-model") &&
attr->value_tag == IPP_TAG_TEXT)
ppd_make = attr->values[0].string.text;
else if (!strcmp(attr->name, "ppd-name") &&
attr->value_tag == IPP_TAG_NAME)
ppd_name = attr->values[0].string.text;
attr = attr->next;
}
if (ppd_language == NULL || ppd_make == NULL || ppd_name == NULL)
{
if (attr == NULL)
break;
else
continue;
}
if (long_status)
{
_cupsLangPrintf(stdout,
_("Model: name = %s\n"
" natural_language = %s\n"
" make-and-model = %s\n"
" device-id = %s\n"),
ppd_name, ppd_language, ppd_make, ppd_device_id);
}
else
_cupsLangPrintf(stdout, "%s %s\n", ppd_name, ppd_make);
if (attr == NULL)
break;
}
ippDelete(response);
}
else
{
_cupsLangPrintf(stderr, "lpinfo: %s\n", cupsLastErrorString());
return (1);
}
return (0);
}