#include "cups-private.h"
#include "debug-internal.h"
#include <fcntl.h>
#include <sys/stat.h>
#if defined(_WIN32) || defined(__EMX__)
# include <io.h>
#else
# include <unistd.h>
#endif
int
cupsCancelJob(const char *name,
int job_id)
{
return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0)
< IPP_STATUS_REDIRECTION_OTHER_SITE);
}
ipp_status_t
cupsCancelJob2(http_t *http,
const char *name,
int job_id,
int purge)
{
char uri[HTTP_MAX_URI];
ipp_t *request;
if (job_id < -1 || (!name && job_id == 0))
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
return (0);
}
if (!http)
if ((http = _cupsConnect()) == NULL)
return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE);
request = ippNewRequest(job_id < 0 ? IPP_OP_PURGE_JOBS : IPP_OP_CANCEL_JOB);
if (name)
{
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", ippPort(), "/printers/%s", name);
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 if (job_id > 0)
{
snprintf(uri, sizeof(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 (purge && job_id >= 0)
ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1);
else if (!purge && job_id < 0)
ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0);
ippDelete(cupsDoRequest(http, request, "/jobs/"));
return (cupsLastError());
}
int
cupsCreateJob(
http_t *http,
const char *name,
const char *title,
int num_options,
cups_option_t *options)
{
int job_id = 0;
ipp_status_t status;
cups_dest_t *dest;
cups_dinfo_t *info;
DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", num_options=%d, options=%p)", (void *)http, name, title, num_options, (void *)options));
if (!name)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
return (0);
}
if ((dest = cupsGetNamedDest(http, name, NULL)) == NULL)
{
DEBUG_puts("1cupsCreateJob: Destination not found.");
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0);
return (0);
}
DEBUG_puts("1cupsCreateJob: Querying destination info.");
if ((info = cupsCopyDestInfo(http, dest)) == NULL)
{
DEBUG_puts("1cupsCreateJob: Query failed.");
cupsFreeDests(1, dest);
return (0);
}
status = cupsCreateDestJob(http, dest, info, &job_id, title, num_options, options);
DEBUG_printf(("1cupsCreateJob: cupsCreateDestJob returned %04x (%s)", status, ippErrorString(status)));
cupsFreeDestInfo(info);
cupsFreeDests(1, dest);
if (status >= IPP_STATUS_REDIRECTION_OTHER_SITE)
return (0);
else
return (job_id);
}
ipp_status_t
cupsFinishDocument(http_t *http,
const char *name)
{
char resource[1024];
snprintf(resource, sizeof(resource), "/printers/%s", name);
ippDelete(cupsGetResponse(http, resource));
return (cupsLastError());
}
void
cupsFreeJobs(int num_jobs,
cups_job_t *jobs)
{
int i;
cups_job_t *job;
if (num_jobs <= 0 || !jobs)
return;
for (i = num_jobs, job = jobs; i > 0; i --, job ++)
{
_cupsStrFree(job->dest);
_cupsStrFree(job->user);
_cupsStrFree(job->format);
_cupsStrFree(job->title);
}
free(jobs);
}
int
cupsGetClasses(char ***classes)
{
if (classes)
*classes = NULL;
return (0);
}
const char *
cupsGetDefault(void)
{
return (cupsGetDefault2(CUPS_HTTP_DEFAULT));
}
const char *
cupsGetDefault2(http_t *http)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
_cups_globals_t *cg = _cupsGlobals();
if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer)))
return (cg->def_printer);
if (!http)
if ((http = _cupsConnect()) == NULL)
return (NULL);
request = ippNewRequest(IPP_OP_CUPS_GET_DEFAULT);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if ((attr = ippFindAttribute(response, "printer-name",
IPP_TAG_NAME)) != NULL)
{
strlcpy(cg->def_printer, attr->values[0].string.text,
sizeof(cg->def_printer));
ippDelete(response);
return (cg->def_printer);
}
ippDelete(response);
}
return (NULL);
}
int
cupsGetJobs(cups_job_t **jobs,
const char *name,
int myjobs,
int whichjobs)
{
return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs));
}
int
cupsGetJobs2(http_t *http,
cups_job_t **jobs,
const char *name,
int myjobs,
int whichjobs)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
cups_job_t *temp;
int id,
priority,
size;
ipp_jstate_t state;
time_t completed_time,
creation_time,
processing_time;
const char *dest,
*format,
*title,
*user;
char uri[HTTP_MAX_URI];
_cups_globals_t *cg = _cupsGlobals();
static const char * const attrs[] =
{
"document-format",
"job-id",
"job-k-octets",
"job-name",
"job-originating-user-name",
"job-printer-uri",
"job-priority",
"job-state",
"time-at-completed",
"time-at-creation",
"time-at-processing"
};
if (!jobs)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
return (-1);
}
if (name)
{
if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s",
name) < HTTP_URI_STATUS_OK)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL,
_("Unable to create printer-uri"), 1);
return (-1);
}
}
else
strlcpy(uri, "ipp://localhost/", sizeof(uri));
if (!http)
if ((http = _cupsConnect()) == NULL)
return (-1);
request = ippNewRequest(IPP_OP_GET_JOBS);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
if (myjobs)
ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
if (whichjobs == CUPS_WHICHJOBS_COMPLETED)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"which-jobs", NULL, "completed");
else if (whichjobs == CUPS_WHICHJOBS_ALL)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"which-jobs", NULL, "all");
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", sizeof(attrs) / sizeof(attrs[0]),
NULL, attrs);
n = 0;
*jobs = NULL;
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
for (attr = response->attrs; attr; attr = attr->next)
{
while (attr && attr->group_tag != IPP_TAG_JOB)
attr = attr->next;
if (!attr)
break;
id = 0;
size = 0;
priority = 50;
state = IPP_JSTATE_PENDING;
user = "unknown";
dest = NULL;
format = "application/octet-stream";
title = "untitled";
creation_time = 0;
completed_time = 0;
processing_time = 0;
while (attr && attr->group_tag == IPP_TAG_JOB)
{
if (!strcmp(attr->name, "job-id") &&
attr->value_tag == IPP_TAG_INTEGER)
id = attr->values[0].integer;
else if (!strcmp(attr->name, "job-state") &&
attr->value_tag == IPP_TAG_ENUM)
state = (ipp_jstate_t)attr->values[0].integer;
else if (!strcmp(attr->name, "job-priority") &&
attr->value_tag == IPP_TAG_INTEGER)
priority = attr->values[0].integer;
else if (!strcmp(attr->name, "job-k-octets") &&
attr->value_tag == IPP_TAG_INTEGER)
size = attr->values[0].integer;
else if (!strcmp(attr->name, "time-at-completed") &&
attr->value_tag == IPP_TAG_INTEGER)
completed_time = attr->values[0].integer;
else if (!strcmp(attr->name, "time-at-creation") &&
attr->value_tag == IPP_TAG_INTEGER)
creation_time = attr->values[0].integer;
else if (!strcmp(attr->name, "time-at-processing") &&
attr->value_tag == IPP_TAG_INTEGER)
processing_time = attr->values[0].integer;
else if (!strcmp(attr->name, "job-printer-uri") &&
attr->value_tag == IPP_TAG_URI)
{
if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
dest ++;
}
else if (!strcmp(attr->name, "job-originating-user-name") &&
attr->value_tag == IPP_TAG_NAME)
user = attr->values[0].string.text;
else if (!strcmp(attr->name, "document-format") &&
attr->value_tag == IPP_TAG_MIMETYPE)
format = attr->values[0].string.text;
else if (!strcmp(attr->name, "job-name") &&
(attr->value_tag == IPP_TAG_TEXT ||
attr->value_tag == IPP_TAG_NAME))
title = attr->values[0].string.text;
attr = attr->next;
}
if (!dest || !id)
{
if (!attr)
break;
else
continue;
}
if (n == 0)
temp = malloc(sizeof(cups_job_t));
else
temp = realloc(*jobs, sizeof(cups_job_t) * (size_t)(n + 1));
if (!temp)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
cupsFreeJobs(n, *jobs);
*jobs = NULL;
ippDelete(response);
return (-1);
}
*jobs = temp;
temp += n;
n ++;
temp->dest = _cupsStrAlloc(dest);
temp->user = _cupsStrAlloc(user);
temp->format = _cupsStrAlloc(format);
temp->title = _cupsStrAlloc(title);
temp->id = id;
temp->priority = priority;
temp->state = state;
temp->size = size;
temp->completed_time = completed_time;
temp->creation_time = creation_time;
temp->processing_time = processing_time;
if (!attr)
break;
}
ippDelete(response);
}
if (n == 0 && cg->last_error >= IPP_STATUS_ERROR_BAD_REQUEST)
return (-1);
else
return (n);
}
int
cupsGetPrinters(char ***printers)
{
if (printers)
*printers = NULL;
return (0);
}
int
cupsPrintFile(const char *name,
const char *filename,
const char *title,
int num_options,
cups_option_t *options)
{
DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", title=\"%s\", num_options=%d, options=%p)", name, filename, title, num_options, (void *)options));
return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title,
num_options, options));
}
int
cupsPrintFile2(
http_t *http,
const char *name,
const char *filename,
const char *title,
int num_options,
cups_option_t *options)
{
DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", title=\"%s\", num_options=%d, options=%p)", (void *)http, name, filename, title, num_options, (void *)options));
return (cupsPrintFiles2(http, name, 1, &filename, title, num_options,
options));
}
int
cupsPrintFiles(
const char *name,
int num_files,
const char **files,
const char *title,
int num_options,
cups_option_t *options)
{
DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, files=%p, title=\"%s\", num_options=%d, options=%p)", name, num_files, (void *)files, title, num_options, (void *)options));
return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title,
num_options, options));
}
int
cupsPrintFiles2(
http_t *http,
const char *name,
int num_files,
const char **files,
const char *title,
int num_options,
cups_option_t *options)
{
int i;
int job_id;
const char *docname;
const char *format;
cups_file_t *fp;
char buffer[8192];
ssize_t bytes;
http_status_t status;
_cups_globals_t *cg = _cupsGlobals();
ipp_status_t cancel_status;
char *cancel_message;
DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, files=%p, title=\"%s\", num_options=%d, options=%p)", (void *)http, name, num_files, (void *)files, title, num_options, (void *)options));
if (!name || num_files < 1 || !files)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
return (0);
}
if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0)
return (0);
if (cupsGetOption("raw", num_options, options))
format = CUPS_FORMAT_RAW;
else if ((format = cupsGetOption("document-format", num_options,
options)) == NULL)
format = CUPS_FORMAT_AUTO;
for (i = 0; i < num_files; i ++)
{
if ((docname = strrchr(files[i], '/')) != NULL)
docname ++;
else
docname = files[i];
if ((fp = cupsFileOpen(files[i], "rb")) == NULL)
{
_cupsSetError(IPP_STATUS_ERROR_DOCUMENT_ACCESS, NULL, 0);
goto cancel_job;
}
status = cupsStartDocument(http, name, job_id, docname, format,
i == (num_files - 1));
while (status == HTTP_STATUS_CONTINUE &&
(bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
status = cupsWriteRequestData(http, buffer, (size_t)bytes);
cupsFileClose(fp);
if (status != HTTP_STATUS_CONTINUE || cupsFinishDocument(http, name) != IPP_STATUS_OK)
{
goto cancel_job;
}
}
return (job_id);
cancel_job:
cancel_status = cg->last_error;
cancel_message = cg->last_status_message ?
_cupsStrRetain(cg->last_status_message) : NULL;
cupsCancelJob2(http, name, job_id, 0);
cg->last_error = cancel_status;
cg->last_status_message = cancel_message;
return (0);
}
http_status_t
cupsStartDocument(
http_t *http,
const char *name,
int job_id,
const char *docname,
const char *format,
int last_document)
{
char resource[1024],
printer_uri[1024];
ipp_t *request;
http_status_t status;
if ((request = ippNewRequest(IPP_OP_SEND_DOCUMENT)) == NULL)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0);
return (HTTP_STATUS_ERROR);
}
httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
NULL, "localhost", ippPort(), "/printers/%s", name);
snprintf(resource, sizeof(resource), "/printers/%s", name);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, printer_uri);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
if (docname)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
NULL, docname);
if (format)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
"document-format", NULL, format);
ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", (char)last_document);
status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE);
ippDelete(request);
return (status);
}