#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <cups/cups.h>
#include <cups/language.h>
#include <cups/debug.h>
#include <cups/string.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;
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
fprintf(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)
{
perror("lpinfo: Unable to connect to server");
return (1);
}
}
if (show_models(http, long_status))
return (1);
break;
case 'v' :
if (!http)
{
http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption());
if (http == NULL)
{
perror("lpinfo: Unable to connect to server");
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)
{
fputs("Error: need hostname after \'-h\' option!\n", stderr);
return (1);
}
cupsSetServer(argv[i]);
}
break;
default :
fprintf(stderr, "lpinfo: Unknown option \'%c\'!\n", argv[i][1]);
return (1);
}
else
{
fprintf(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;
cups_lang_t *language;
const char *device_class,
*device_info,
*device_make,
*device_uri;
if (http == NULL)
return (1);
request = ippNew();
request->request.op.operation_id = CUPS_GET_DEVICES;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, "ipp://localhost/printers/");
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
fprintf(stderr, "lpinfo: cups-get-devices failed: %s\n",
ippErrorString(response->request.status.status_code));
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;
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
if (strcmp(attr->name, "device-class") == 0 &&
attr->value_tag == IPP_TAG_KEYWORD)
device_class = attr->values[0].string.text;
if (strcmp(attr->name, "device-info") == 0 &&
attr->value_tag == IPP_TAG_TEXT)
device_info = attr->values[0].string.text;
if (strcmp(attr->name, "device-make-and-model") == 0 &&
attr->value_tag == IPP_TAG_TEXT)
device_make = attr->values[0].string.text;
if (strcmp(attr->name, "device-uri") == 0 &&
attr->value_tag == IPP_TAG_URI)
device_uri = 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)
{
printf("Device: uri = %s\n", device_uri);
printf(" class = %s\n", device_class);
printf(" info = %s\n", device_info);
printf(" make-and-model = %s\n", device_make);
}
else
printf("%s %s\n", device_class, device_uri);
if (attr == NULL)
break;
}
ippDelete(response);
}
else
{
fprintf(stderr, "lpinfo: cups-get-devices failed: %s\n",
ippErrorString(cupsLastError()));
return (1);
}
return (0);
}
static int
show_models(http_t *http,
int long_status)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
cups_lang_t *language;
const char *ppd_language,
*ppd_make,
*ppd_name;
if (http == NULL)
return (1);
request = ippNew();
request->request.op.operation_id = CUPS_GET_PPDS;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, "ipp://localhost/printers/");
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
fprintf(stderr, "lpinfo: cups-get-ppds failed: %s\n",
ippErrorString(response->request.status.status_code));
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_language = NULL;
ppd_make = NULL;
ppd_name = NULL;
while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
{
if (strcmp(attr->name, "ppd-natural-language") == 0 &&
attr->value_tag == IPP_TAG_LANGUAGE)
ppd_language = attr->values[0].string.text;
if (strcmp(attr->name, "ppd-make-and-model") == 0 &&
attr->value_tag == IPP_TAG_TEXT)
ppd_make = attr->values[0].string.text;
if (strcmp(attr->name, "ppd-name") == 0 &&
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)
{
printf("Model: name = %s\n", ppd_name);
printf(" natural_language = %s\n", ppd_language);
printf(" make-and-model = %s\n", ppd_make);
}
else
printf("%s %s\n", ppd_name, ppd_make);
if (attr == NULL)
break;
}
ippDelete(response);
}
else
{
fprintf(stderr, "lpinfo: cups-get-ppds failed: %s\n",
ippErrorString(cupsLastError()));
return (1);
}
return (0);
}