#include <stdio.h>
#include <stdlib.h>
#include <cups/cups.h>
#include <cups/i18n.h>
#include <cups/string.h>
int
main(int argc,
char *argv[])
{
http_t *http;
int i;
int job_id;
const char *dest;
char *instance;
char uri[1024];
ipp_t *request;
ipp_t *response;
ipp_op_t op;
int num_dests;
cups_dest_t *dests,
*defdest;
http_encryption_t encryption;
_cupsSetLocale(argv);
op = IPP_CANCEL_JOB;
job_id = 0;
dest = NULL;
response = NULL;
http = NULL;
encryption = cupsEncryption();
if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL)
{
_cupsLangPuts(stderr, _("lprm: Unable to contact server!\n"));
return (1);
}
num_dests = cupsGetDests2(http, &dests);
defdest = cupsGetDest(NULL, NULL, num_dests, dests);
dest = defdest ? defdest->name : NULL;
for (i = 1; i < argc; i ++)
if (argv[i][0] == '-' && argv[i][1] != '\0')
switch (argv[i][1])
{
case 'E' :
#ifdef HAVE_SSL
encryption = HTTP_ENCRYPT_REQUIRED;
httpEncryption(http, encryption);
cupsSetEncryption(encryption);
#else
_cupsLangPrintf(stderr,
_("%s: Sorry, no encryption support compiled in!\n"),
argv[0]);
#endif
break;
case 'P' :
if (argv[i][2])
dest = argv[i] + 2;
else
{
i ++;
dest = argv[i];
}
if ((instance = strchr(dest, '/')) != NULL)
*instance = '\0';
if (cupsGetDest(dest, NULL, num_dests, dests) == NULL)
{
_cupsLangPrintf(stderr,
_("%s: Error - unknown destination \"%s\"!\n"),
argv[0], dest);
cupsFreeDests(num_dests, dests);
httpClose(http);
return(1);
}
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 'h' :
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);
}
else
cupsSetServer(argv[i]);
}
httpClose(http);
cupsFreeDests(num_dests, dests);
if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
encryption)) == NULL)
{
_cupsLangPuts(stderr, _("lprm: Unable to contact server!\n"));
return (1);
}
num_dests = cupsGetDests2(http, &dests);
defdest = cupsGetDest(NULL, NULL, num_dests, dests);
dest = defdest ? defdest->name : NULL;
break;
default :
_cupsLangPrintf(stderr,
_("%s: Error - unknown option \'%c\'!\n"),
argv[0], argv[i][1]);
cupsFreeDests(num_dests, dests);
httpClose(http);
return (1);
}
else
{
if (isdigit(argv[i][0] & 255) &&
cupsGetDest(argv[i], NULL, num_dests, dests) == NULL)
{
dest = NULL;
op = IPP_CANCEL_JOB;
job_id = atoi(argv[i]);
}
else if (!strcmp(argv[i], "-"))
{
op = IPP_PURGE_JOBS;
}
else
{
dest = argv[i];
job_id = 0;
}
request = ippNewRequest(op);
if (dest)
{
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", dest);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
job_id);
}
else
{
sprintf(uri, "ipp://localhost/jobs/%d", job_id);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
uri);
}
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
if (op == IPP_PURGE_JOBS)
response = cupsDoRequest(http, request, "/admin/");
else
response = cupsDoRequest(http, request, "/jobs/");
ippDelete(response);
if (cupsLastError() > IPP_OK_CONFLICT)
{
_cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString());
cupsFreeDests(num_dests, dests);
httpClose(http);
return (1);
}
}
if (response == NULL)
if (!cupsCancelJob(dest, 0))
{
_cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString());
cupsFreeDests(num_dests, dests);
httpClose(http);
return (1);
}
cupsFreeDests(num_dests, dests);
httpClose(http);
return (0);
}