]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
7e86f2f6 | 2 | * Printing utilities for CUPS. |
ef416fc2 | 3 | * |
7536de1a | 4 | * Copyright 2007-2017 by Apple Inc. |
7e86f2f6 | 5 | * Copyright 1997-2006 by Easy Software Products. |
ef416fc2 | 6 | * |
e3101897 | 7 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
ef416fc2 | 8 | */ |
9 | ||
10 | /* | |
11 | * Include necessary headers... | |
12 | */ | |
13 | ||
71e16022 | 14 | #include "cups-private.h" |
ef416fc2 | 15 | #include <fcntl.h> |
16 | #include <sys/stat.h> | |
17 | #if defined(WIN32) || defined(__EMX__) | |
18 | # include <io.h> | |
19 | #else | |
20 | # include <unistd.h> | |
21 | #endif /* WIN32 || __EMX__ */ | |
22 | ||
23 | ||
7536de1a MS |
24 | /* |
25 | * Enumeration data and callback... | |
26 | */ | |
27 | ||
28 | typedef struct _cups_createdata_s | |
29 | { | |
30 | const char *name; /* Destination name */ | |
31 | cups_dest_t *dest; /* Matching destination */ | |
32 | } _cups_createdata_t; | |
33 | ||
34 | static int cups_create_cb(_cups_createdata_t *data, unsigned flags, cups_dest_t *dest); | |
35 | ||
36 | ||
ef416fc2 | 37 | /* |
38 | * 'cupsCancelJob()' - Cancel a print job on the default server. | |
39 | * | |
5a738aea MS |
40 | * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ |
41 | * to cancel the current job on the named destination. | |
42 | * | |
43 | * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get | |
ef416fc2 | 44 | * the cause of any failure. |
53af7f21 MS |
45 | * |
46 | * @exclude all@ | |
ef416fc2 | 47 | */ |
48 | ||
49 | int /* O - 1 on success, 0 on failure */ | |
50 | cupsCancelJob(const char *name, /* I - Name of printer or class */ | |
568fa3fa | 51 | int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ |
ef416fc2 | 52 | { |
5a738aea | 53 | return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) |
cb7f98ee | 54 | < IPP_STATUS_REDIRECTION_OTHER_SITE); |
3d052e43 MS |
55 | } |
56 | ||
57 | ||
58 | /* | |
59 | * 'cupsCancelJob2()' - Cancel or purge a print job. | |
60 | * | |
61 | * Canceled jobs remain in the job history while purged jobs are removed | |
62 | * from the job history. | |
63 | * | |
5a738aea MS |
64 | * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ |
65 | * to cancel the current job on the named destination. | |
66 | * | |
67 | * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get | |
3d052e43 MS |
68 | * the cause of any failure. |
69 | * | |
53af7f21 | 70 | * @since CUPS 1.4/macOS 10.6@ @exclude all@ |
3d052e43 MS |
71 | */ |
72 | ||
73 | ipp_status_t /* O - IPP status */ | |
568fa3fa | 74 | cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
5a738aea | 75 | const char *name, /* I - Name of printer or class */ |
568fa3fa | 76 | int job_id, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ |
5a738aea | 77 | int purge) /* I - 1 to purge, 0 to cancel */ |
3d052e43 | 78 | { |
5a738aea | 79 | char uri[HTTP_MAX_URI]; /* Job/printer URI */ |
3d052e43 | 80 | ipp_t *request; /* IPP request */ |
ef416fc2 | 81 | |
82 | ||
83 | /* | |
3d052e43 | 84 | * Range check input... |
ef416fc2 | 85 | */ |
86 | ||
5a738aea | 87 | if (job_id < -1 || (!name && job_id == 0)) |
ef416fc2 | 88 | { |
cb7f98ee | 89 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
ef416fc2 | 90 | return (0); |
91 | } | |
92 | ||
93 | /* | |
3d052e43 | 94 | * Connect to the default server as needed... |
ef416fc2 | 95 | */ |
96 | ||
3d052e43 MS |
97 | if (!http) |
98 | if ((http = _cupsConnect()) == NULL) | |
cb7f98ee | 99 | return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE); |
ef416fc2 | 100 | |
101 | /* | |
5a738aea | 102 | * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following |
ef416fc2 | 103 | * attributes: |
104 | * | |
105 | * attributes-charset | |
106 | * attributes-natural-language | |
5a738aea | 107 | * job-uri or printer-uri + job-id |
3d052e43 | 108 | * requesting-user-name |
5a738aea | 109 | * [purge-job] or [purge-jobs] |
ef416fc2 | 110 | */ |
111 | ||
cb7f98ee | 112 | request = ippNewRequest(job_id < 0 ? IPP_OP_PURGE_JOBS : IPP_OP_CANCEL_JOB); |
ef416fc2 | 113 | |
5a738aea MS |
114 | if (name) |
115 | { | |
116 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
117 | "localhost", ippPort(), "/printers/%s", name); | |
118 | ||
119 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
120 | uri); | |
121 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
122 | job_id); | |
123 | } | |
124 | else if (job_id > 0) | |
125 | { | |
126 | snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id); | |
127 | ||
128 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); | |
129 | } | |
ef416fc2 | 130 | |
131 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
132 | NULL, cupsUser()); | |
5a738aea MS |
133 | |
134 | if (purge && job_id >= 0) | |
3d052e43 | 135 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1); |
5a738aea MS |
136 | else if (!purge && job_id < 0) |
137 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0); | |
ef416fc2 | 138 | |
139 | /* | |
140 | * Do the request... | |
141 | */ | |
142 | ||
3d052e43 MS |
143 | ippDelete(cupsDoRequest(http, request, "/jobs/")); |
144 | ||
145 | return (cupsLastError()); | |
146 | } | |
147 | ||
148 | ||
149 | /* | |
568fa3fa | 150 | * 'cupsCreateJob()' - Create an empty job for streaming. |
3d052e43 | 151 | * |
568fa3fa MS |
152 | * Use this function when you want to stream print data using the |
153 | * @link cupsStartDocument@, @link cupsWriteRequestData@, and | |
154 | * @link cupsFinishDocument@ functions. If you have one or more files to | |
155 | * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function | |
156 | * instead. | |
3d052e43 | 157 | * |
53af7f21 | 158 | * @since CUPS 1.4/macOS 10.6@ @exclude all@ |
3d052e43 MS |
159 | */ |
160 | ||
161 | int /* O - Job ID or 0 on error */ | |
162 | cupsCreateJob( | |
568fa3fa MS |
163 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
164 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
165 | const char *title, /* I - Title of job */ |
166 | int num_options, /* I - Number of options */ | |
167 | cups_option_t *options) /* I - Options */ | |
168 | { | |
3d052e43 | 169 | int job_id = 0; /* job-id value */ |
7536de1a MS |
170 | ipp_status_t status; /* Create-Job status */ |
171 | _cups_createdata_t data; /* Enumeration data */ | |
172 | cups_dinfo_t *info; /* Destination information */ | |
3d052e43 MS |
173 | |
174 | ||
807315e6 | 175 | DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", num_options=%d, options=%p)", (void *)http, name, title, num_options, (void *)options)); |
3d052e43 MS |
176 | |
177 | /* | |
178 | * Range check input... | |
179 | */ | |
180 | ||
181 | if (!name) | |
182 | { | |
cb7f98ee | 183 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
3d052e43 MS |
184 | return (0); |
185 | } | |
186 | ||
187 | /* | |
7536de1a | 188 | * Lookup the destination... |
3d052e43 MS |
189 | */ |
190 | ||
7536de1a MS |
191 | data.name = name; |
192 | data.dest = NULL; | |
193 | ||
f50db552 | 194 | cupsEnumDests(0, 1000, NULL, 0, 0, (cups_dest_cb_t)cups_create_cb, &data); |
7536de1a MS |
195 | |
196 | if (!data.dest) | |
3d052e43 | 197 | { |
7536de1a MS |
198 | DEBUG_puts("1cupsCreateJob: Destination not found."); |
199 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0); | |
3d052e43 MS |
200 | return (0); |
201 | } | |
202 | ||
3d052e43 | 203 | /* |
7536de1a | 204 | * Query dest information and create the job... |
3d052e43 MS |
205 | */ |
206 | ||
7536de1a MS |
207 | DEBUG_puts("1cupsCreateJob: Querying destination info."); |
208 | if ((info = cupsCopyDestInfo(http, data.dest)) == NULL) | |
209 | { | |
210 | DEBUG_puts("1cupsCreateJob: Query failed."); | |
211 | cupsFreeDests(1, data.dest); | |
212 | return (0); | |
213 | } | |
3d052e43 | 214 | |
7536de1a MS |
215 | status = cupsCreateDestJob(http, data.dest, info, &job_id, title, num_options, options); |
216 | DEBUG_printf(("1cupsCreateJob: cupsCreateDestJob returned %04x (%s)", status, ippErrorString(status))); | |
3d052e43 | 217 | |
7536de1a MS |
218 | cupsFreeDestInfo(info); |
219 | cupsFreeDests(1, data.dest); | |
3d052e43 MS |
220 | |
221 | /* | |
7536de1a | 222 | * Return the job... |
3d052e43 MS |
223 | */ |
224 | ||
7536de1a MS |
225 | if (status >= IPP_STATUS_REDIRECTION_OTHER_SITE) |
226 | return (0); | |
227 | else | |
228 | return (job_id); | |
3d052e43 MS |
229 | } |
230 | ||
231 | ||
232 | /* | |
233 | * 'cupsFinishDocument()' - Finish sending a document. | |
234 | * | |
5a738aea MS |
235 | * The document must have been started using @link cupsStartDocument@. |
236 | * | |
53af7f21 | 237 | * @since CUPS 1.4/macOS 10.6@ @exclude all@ |
3d052e43 MS |
238 | */ |
239 | ||
240 | ipp_status_t /* O - Status of document submission */ | |
568fa3fa MS |
241 | cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
242 | const char *name) /* I - Destination name */ | |
3d052e43 MS |
243 | { |
244 | char resource[1024]; /* Printer resource */ | |
245 | ||
246 | ||
247 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
ef416fc2 | 248 | |
3d052e43 MS |
249 | ippDelete(cupsGetResponse(http, resource)); |
250 | ||
251 | return (cupsLastError()); | |
ef416fc2 | 252 | } |
253 | ||
254 | ||
ef416fc2 | 255 | /* |
256 | * 'cupsFreeJobs()' - Free memory used by job data. | |
257 | */ | |
258 | ||
259 | void | |
260 | cupsFreeJobs(int num_jobs, /* I - Number of jobs */ | |
261 | cups_job_t *jobs) /* I - Jobs */ | |
262 | { | |
3d052e43 MS |
263 | int i; /* Looping var */ |
264 | cups_job_t *job; /* Current job */ | |
ef416fc2 | 265 | |
266 | ||
3d052e43 | 267 | if (num_jobs <= 0 || !jobs) |
ef416fc2 | 268 | return; |
269 | ||
3d052e43 | 270 | for (i = num_jobs, job = jobs; i > 0; i --, job ++) |
ef416fc2 | 271 | { |
3d052e43 MS |
272 | _cupsStrFree(job->dest); |
273 | _cupsStrFree(job->user); | |
274 | _cupsStrFree(job->format); | |
275 | _cupsStrFree(job->title); | |
ef416fc2 | 276 | } |
277 | ||
278 | free(jobs); | |
279 | } | |
280 | ||
281 | ||
282 | /* | |
283 | * 'cupsGetClasses()' - Get a list of printer classes from the default server. | |
284 | * | |
240214ef MS |
285 | * This function is deprecated and no longer returns a list of printer |
286 | * classes - use @link cupsGetDests@ instead. | |
ef416fc2 | 287 | * |
53af7f21 | 288 | * @deprecated@ @exclude all@ |
ef416fc2 | 289 | */ |
290 | ||
291 | int /* O - Number of classes */ | |
292 | cupsGetClasses(char ***classes) /* O - Classes */ | |
293 | { | |
240214ef MS |
294 | if (classes) |
295 | *classes = NULL; | |
ef416fc2 | 296 | |
240214ef | 297 | return (0); |
ef416fc2 | 298 | } |
299 | ||
300 | ||
301 | /* | |
302 | * 'cupsGetDefault()' - Get the default printer or class for the default server. | |
303 | * | |
304 | * This function returns the default printer or class as defined by | |
305 | * the LPDEST or PRINTER environment variables. If these environment | |
306 | * variables are not set, the server default destination is returned. | |
5a738aea MS |
307 | * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ |
308 | * functions to get the user-defined default printer, as this function does | |
309 | * not support the lpoptions-defined default printer. | |
53af7f21 MS |
310 | * |
311 | * @exclude all@ | |
ef416fc2 | 312 | */ |
313 | ||
5a738aea | 314 | const char * /* O - Default printer or @code NULL@ */ |
ef416fc2 | 315 | cupsGetDefault(void) |
316 | { | |
ef416fc2 | 317 | /* |
318 | * Return the default printer... | |
319 | */ | |
320 | ||
3d052e43 | 321 | return (cupsGetDefault2(CUPS_HTTP_DEFAULT)); |
ef416fc2 | 322 | } |
323 | ||
324 | ||
325 | /* | |
326 | * 'cupsGetDefault2()' - Get the default printer or class for the specified server. | |
327 | * | |
328 | * This function returns the default printer or class as defined by | |
329 | * the LPDEST or PRINTER environment variables. If these environment | |
330 | * variables are not set, the server default destination is returned. | |
5a738aea MS |
331 | * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ |
332 | * functions to get the user-defined default printer, as this function does | |
333 | * not support the lpoptions-defined default printer. | |
ef416fc2 | 334 | * |
53af7f21 | 335 | * @since CUPS 1.1.21/macOS 10.4@ @exclude all@ |
ef416fc2 | 336 | */ |
337 | ||
5a738aea | 338 | const char * /* O - Default printer or @code NULL@ */ |
568fa3fa | 339 | cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 340 | { |
341 | ipp_t *request, /* IPP Request */ | |
342 | *response; /* IPP Response */ | |
343 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 344 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
345 | ||
346 | ||
347 | /* | |
38e73f87 | 348 | * See if we have a user default printer set... |
ef416fc2 | 349 | */ |
350 | ||
38e73f87 MS |
351 | if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer))) |
352 | return (cg->def_printer); | |
ef416fc2 | 353 | |
354 | /* | |
3d052e43 | 355 | * Connect to the server as needed... |
ef416fc2 | 356 | */ |
357 | ||
358 | if (!http) | |
3d052e43 MS |
359 | if ((http = _cupsConnect()) == NULL) |
360 | return (NULL); | |
ef416fc2 | 361 | |
362 | /* | |
363 | * Build a CUPS_GET_DEFAULT request, which requires the following | |
364 | * attributes: | |
365 | * | |
366 | * attributes-charset | |
367 | * attributes-natural-language | |
368 | */ | |
369 | ||
cb7f98ee | 370 | request = ippNewRequest(IPP_OP_CUPS_GET_DEFAULT); |
ef416fc2 | 371 | |
372 | /* | |
373 | * Do the request and get back a response... | |
374 | */ | |
375 | ||
376 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
377 | { | |
3d052e43 MS |
378 | if ((attr = ippFindAttribute(response, "printer-name", |
379 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 380 | { |
3d052e43 MS |
381 | strlcpy(cg->def_printer, attr->values[0].string.text, |
382 | sizeof(cg->def_printer)); | |
ef416fc2 | 383 | ippDelete(response); |
384 | return (cg->def_printer); | |
385 | } | |
386 | ||
387 | ippDelete(response); | |
388 | } | |
389 | ||
390 | return (NULL); | |
391 | } | |
392 | ||
393 | ||
394 | /* | |
395 | * 'cupsGetJobs()' - Get the jobs from the default server. | |
5a738aea MS |
396 | * |
397 | * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless | |
398 | * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are | |
399 | * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns | |
400 | * jobs that are stopped, canceled, aborted, or completed. | |
53af7f21 MS |
401 | * |
402 | * @exclude all@ | |
ef416fc2 | 403 | */ |
404 | ||
405 | int /* O - Number of jobs */ | |
406 | cupsGetJobs(cups_job_t **jobs, /* O - Job data */ | |
568fa3fa | 407 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ |
ecdc0628 | 408 | int myjobs, /* I - 0 = all users, 1 = mine */ |
5a738aea | 409 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ |
ef416fc2 | 410 | { |
ef416fc2 | 411 | /* |
412 | * Return the jobs... | |
413 | */ | |
414 | ||
5a738aea | 415 | return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs)); |
ef416fc2 | 416 | } |
417 | ||
418 | ||
419 | ||
420 | /* | |
421 | * 'cupsGetJobs2()' - Get the jobs from the specified server. | |
422 | * | |
5a738aea MS |
423 | * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless |
424 | * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are | |
425 | * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns | |
426 | * jobs that are stopped, canceled, aborted, or completed. | |
427 | * | |
8072030b | 428 | * @since CUPS 1.1.21/macOS 10.4@ |
ef416fc2 | 429 | */ |
430 | ||
431 | int /* O - Number of jobs */ | |
568fa3fa | 432 | cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 433 | cups_job_t **jobs, /* O - Job data */ |
568fa3fa | 434 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ |
ecdc0628 | 435 | int myjobs, /* I - 0 = all users, 1 = mine */ |
5a738aea | 436 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ |
ef416fc2 | 437 | { |
438 | int n; /* Number of jobs */ | |
439 | ipp_t *request, /* IPP Request */ | |
440 | *response; /* IPP Response */ | |
441 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 442 | cups_job_t *temp; /* Temporary pointer */ |
443 | int id, /* job-id */ | |
444 | priority, /* job-priority */ | |
445 | size; /* job-k-octets */ | |
446 | ipp_jstate_t state; /* job-state */ | |
447 | time_t completed_time, /* time-at-completed */ | |
448 | creation_time, /* time-at-creation */ | |
449 | processing_time; /* time-at-processing */ | |
450 | const char *dest, /* job-printer-uri */ | |
451 | *format, /* document-format */ | |
452 | *title, /* job-name */ | |
453 | *user; /* job-originating-user-name */ | |
454 | char uri[HTTP_MAX_URI]; /* URI for jobs */ | |
455 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
456 | static const char * const attrs[] = /* Requested attributes */ | |
457 | { | |
5a662dc0 | 458 | "document-format", |
ef416fc2 | 459 | "job-id", |
ef416fc2 | 460 | "job-k-octets", |
5a662dc0 MS |
461 | "job-name", |
462 | "job-originating-user-name", | |
463 | "job-printer-uri", | |
464 | "job-priority", | |
ef416fc2 | 465 | "job-state", |
466 | "time-at-completed", | |
467 | "time-at-creation", | |
5a662dc0 | 468 | "time-at-processing" |
ef416fc2 | 469 | }; |
470 | ||
471 | ||
472 | /* | |
473 | * Range check input... | |
474 | */ | |
475 | ||
3d052e43 | 476 | if (!jobs) |
ef416fc2 | 477 | { |
cb7f98ee | 478 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
ef416fc2 | 479 | |
480 | return (-1); | |
481 | } | |
482 | ||
483 | /* | |
484 | * Get the right URI... | |
485 | */ | |
486 | ||
5a738aea | 487 | if (name) |
ef416fc2 | 488 | { |
a4d04587 | 489 | if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
cb7f98ee MS |
490 | "localhost", 0, "/printers/%s", |
491 | name) < HTTP_URI_STATUS_OK) | |
ef416fc2 | 492 | { |
cb7f98ee MS |
493 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, |
494 | _("Unable to create printer-uri"), 1); | |
ef416fc2 | 495 | |
496 | return (-1); | |
497 | } | |
498 | } | |
499 | else | |
5a9febac | 500 | strlcpy(uri, "ipp://localhost/", sizeof(uri)); |
ef416fc2 | 501 | |
3d052e43 MS |
502 | if (!http) |
503 | if ((http = _cupsConnect()) == NULL) | |
504 | return (-1); | |
ef416fc2 | 505 | |
506 | /* | |
507 | * Build an IPP_GET_JOBS request, which requires the following | |
508 | * attributes: | |
509 | * | |
510 | * attributes-charset | |
511 | * attributes-natural-language | |
512 | * printer-uri | |
513 | * requesting-user-name | |
514 | * which-jobs | |
515 | * my-jobs | |
516 | * requested-attributes | |
517 | */ | |
518 | ||
cb7f98ee | 519 | request = ippNewRequest(IPP_OP_GET_JOBS); |
ef416fc2 | 520 | |
521 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
522 | "printer-uri", NULL, uri); | |
523 | ||
524 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
525 | "requesting-user-name", NULL, cupsUser()); | |
526 | ||
527 | if (myjobs) | |
528 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
529 | ||
5a738aea | 530 | if (whichjobs == CUPS_WHICHJOBS_COMPLETED) |
ef416fc2 | 531 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
532 | "which-jobs", NULL, "completed"); | |
5a738aea | 533 | else if (whichjobs == CUPS_WHICHJOBS_ALL) |
ecdc0628 | 534 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
535 | "which-jobs", NULL, "all"); | |
ef416fc2 | 536 | |
537 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
538 | "requested-attributes", sizeof(attrs) / sizeof(attrs[0]), | |
539 | NULL, attrs); | |
540 | ||
541 | /* | |
542 | * Do the request and get back a response... | |
543 | */ | |
544 | ||
545 | n = 0; | |
546 | *jobs = NULL; | |
547 | ||
548 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
549 | { | |
3d052e43 | 550 | for (attr = response->attrs; attr; attr = attr->next) |
ef416fc2 | 551 | { |
552 | /* | |
553 | * Skip leading attributes until we hit a job... | |
554 | */ | |
555 | ||
3d052e43 | 556 | while (attr && attr->group_tag != IPP_TAG_JOB) |
ef416fc2 | 557 | attr = attr->next; |
558 | ||
3d052e43 | 559 | if (!attr) |
ef416fc2 | 560 | break; |
561 | ||
562 | /* | |
563 | * Pull the needed attributes from this job... | |
564 | */ | |
565 | ||
566 | id = 0; | |
567 | size = 0; | |
568 | priority = 50; | |
cb7f98ee | 569 | state = IPP_JSTATE_PENDING; |
ef416fc2 | 570 | user = "unknown"; |
571 | dest = NULL; | |
572 | format = "application/octet-stream"; | |
573 | title = "untitled"; | |
574 | creation_time = 0; | |
575 | completed_time = 0; | |
576 | processing_time = 0; | |
577 | ||
3d052e43 | 578 | while (attr && attr->group_tag == IPP_TAG_JOB) |
ef416fc2 | 579 | { |
3d052e43 | 580 | if (!strcmp(attr->name, "job-id") && |
ef416fc2 | 581 | attr->value_tag == IPP_TAG_INTEGER) |
582 | id = attr->values[0].integer; | |
3d052e43 | 583 | else if (!strcmp(attr->name, "job-state") && |
ef416fc2 | 584 | attr->value_tag == IPP_TAG_ENUM) |
585 | state = (ipp_jstate_t)attr->values[0].integer; | |
3d052e43 | 586 | else if (!strcmp(attr->name, "job-priority") && |
ef416fc2 | 587 | attr->value_tag == IPP_TAG_INTEGER) |
588 | priority = attr->values[0].integer; | |
3d052e43 | 589 | else if (!strcmp(attr->name, "job-k-octets") && |
ef416fc2 | 590 | attr->value_tag == IPP_TAG_INTEGER) |
591 | size = attr->values[0].integer; | |
3d052e43 | 592 | else if (!strcmp(attr->name, "time-at-completed") && |
ef416fc2 | 593 | attr->value_tag == IPP_TAG_INTEGER) |
594 | completed_time = attr->values[0].integer; | |
3d052e43 | 595 | else if (!strcmp(attr->name, "time-at-creation") && |
ef416fc2 | 596 | attr->value_tag == IPP_TAG_INTEGER) |
597 | creation_time = attr->values[0].integer; | |
3d052e43 | 598 | else if (!strcmp(attr->name, "time-at-processing") && |
ef416fc2 | 599 | attr->value_tag == IPP_TAG_INTEGER) |
600 | processing_time = attr->values[0].integer; | |
3d052e43 | 601 | else if (!strcmp(attr->name, "job-printer-uri") && |
ef416fc2 | 602 | attr->value_tag == IPP_TAG_URI) |
603 | { | |
604 | if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL) | |
605 | dest ++; | |
606 | } | |
3d052e43 | 607 | else if (!strcmp(attr->name, "job-originating-user-name") && |
ef416fc2 | 608 | attr->value_tag == IPP_TAG_NAME) |
609 | user = attr->values[0].string.text; | |
3d052e43 | 610 | else if (!strcmp(attr->name, "document-format") && |
ef416fc2 | 611 | attr->value_tag == IPP_TAG_MIMETYPE) |
612 | format = attr->values[0].string.text; | |
3d052e43 | 613 | else if (!strcmp(attr->name, "job-name") && |
ef416fc2 | 614 | (attr->value_tag == IPP_TAG_TEXT || |
615 | attr->value_tag == IPP_TAG_NAME)) | |
616 | title = attr->values[0].string.text; | |
617 | ||
618 | attr = attr->next; | |
619 | } | |
620 | ||
621 | /* | |
622 | * See if we have everything needed... | |
623 | */ | |
624 | ||
3d052e43 | 625 | if (!dest || !id) |
ef416fc2 | 626 | { |
3d052e43 | 627 | if (!attr) |
ef416fc2 | 628 | break; |
629 | else | |
630 | continue; | |
631 | } | |
632 | ||
633 | /* | |
634 | * Allocate memory for the job... | |
635 | */ | |
636 | ||
637 | if (n == 0) | |
638 | temp = malloc(sizeof(cups_job_t)); | |
639 | else | |
7e86f2f6 | 640 | temp = realloc(*jobs, sizeof(cups_job_t) * (size_t)(n + 1)); |
ef416fc2 | 641 | |
3d052e43 | 642 | if (!temp) |
ef416fc2 | 643 | { |
644 | /* | |
645 | * Ran out of memory! | |
646 | */ | |
647 | ||
cb7f98ee | 648 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0); |
3d052e43 | 649 | |
ef416fc2 | 650 | cupsFreeJobs(n, *jobs); |
651 | *jobs = NULL; | |
652 | ||
653 | ippDelete(response); | |
3d052e43 MS |
654 | |
655 | return (-1); | |
ef416fc2 | 656 | } |
657 | ||
658 | *jobs = temp; | |
659 | temp += n; | |
660 | n ++; | |
661 | ||
662 | /* | |
663 | * Copy the data over... | |
664 | */ | |
665 | ||
3d052e43 MS |
666 | temp->dest = _cupsStrAlloc(dest); |
667 | temp->user = _cupsStrAlloc(user); | |
668 | temp->format = _cupsStrAlloc(format); | |
669 | temp->title = _cupsStrAlloc(title); | |
ef416fc2 | 670 | temp->id = id; |
671 | temp->priority = priority; | |
672 | temp->state = state; | |
673 | temp->size = size; | |
674 | temp->completed_time = completed_time; | |
675 | temp->creation_time = creation_time; | |
676 | temp->processing_time = processing_time; | |
677 | ||
3d052e43 | 678 | if (!attr) |
ef416fc2 | 679 | break; |
680 | } | |
681 | ||
682 | ippDelete(response); | |
683 | } | |
684 | ||
cb7f98ee | 685 | if (n == 0 && cg->last_error >= IPP_STATUS_ERROR_BAD_REQUEST) |
ef416fc2 | 686 | return (-1); |
687 | else | |
688 | return (n); | |
689 | } | |
690 | ||
691 | ||
ef416fc2 | 692 | /* |
693 | * 'cupsGetPrinters()' - Get a list of printers from the default server. | |
694 | * | |
240214ef MS |
695 | * This function is deprecated and no longer returns a list of printers - use |
696 | * @link cupsGetDests@ instead. | |
ef416fc2 | 697 | * |
53af7f21 | 698 | * @deprecated@ @exclude all@ |
ef416fc2 | 699 | */ |
700 | ||
701 | int /* O - Number of printers */ | |
702 | cupsGetPrinters(char ***printers) /* O - Printers */ | |
703 | { | |
240214ef MS |
704 | if (printers) |
705 | *printers = NULL; | |
ef416fc2 | 706 | |
240214ef | 707 | return (0); |
ef416fc2 | 708 | } |
709 | ||
710 | ||
ef416fc2 | 711 | /* |
712 | * 'cupsPrintFile()' - Print a file to a printer or class on the default server. | |
53af7f21 MS |
713 | * |
714 | * @exclude all@ | |
ef416fc2 | 715 | */ |
716 | ||
5a738aea | 717 | int /* O - Job ID or 0 on error */ |
568fa3fa | 718 | cupsPrintFile(const char *name, /* I - Destination name */ |
ef416fc2 | 719 | const char *filename, /* I - File to print */ |
720 | const char *title, /* I - Title of job */ | |
721 | int num_options,/* I - Number of options */ | |
722 | cups_option_t *options) /* I - Options */ | |
723 | { | |
807315e6 | 724 | DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", title=\"%s\", num_options=%d, options=%p)", name, filename, title, num_options, (void *)options)); |
ef416fc2 | 725 | |
3d052e43 MS |
726 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title, |
727 | num_options, options)); | |
ef416fc2 | 728 | } |
729 | ||
730 | ||
731 | /* | |
5a738aea MS |
732 | * 'cupsPrintFile2()' - Print a file to a printer or class on the specified |
733 | * server. | |
ef416fc2 | 734 | * |
53af7f21 | 735 | * @since CUPS 1.1.21/macOS 10.4@ @exclude all@ |
ef416fc2 | 736 | */ |
737 | ||
5a738aea | 738 | int /* O - Job ID or 0 on error */ |
3d052e43 | 739 | cupsPrintFile2( |
568fa3fa MS |
740 | http_t *http, /* I - Connection to server */ |
741 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
742 | const char *filename, /* I - File to print */ |
743 | const char *title, /* I - Title of job */ | |
744 | int num_options, /* I - Number of options */ | |
745 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 746 | { |
807315e6 | 747 | 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)); |
ef416fc2 | 748 | |
3d052e43 MS |
749 | return (cupsPrintFiles2(http, name, 1, &filename, title, num_options, |
750 | options)); | |
ef416fc2 | 751 | } |
752 | ||
753 | ||
754 | /* | |
fa73b229 | 755 | * 'cupsPrintFiles()' - Print one or more files to a printer or class on the |
756 | * default server. | |
53af7f21 MS |
757 | * |
758 | * @exclude all@ | |
ef416fc2 | 759 | */ |
760 | ||
5a738aea | 761 | int /* O - Job ID or 0 on error */ |
3d052e43 | 762 | cupsPrintFiles( |
568fa3fa | 763 | const char *name, /* I - Destination name */ |
3d052e43 MS |
764 | int num_files, /* I - Number of files */ |
765 | const char **files, /* I - File(s) to print */ | |
766 | const char *title, /* I - Title of job */ | |
767 | int num_options, /* I - Number of options */ | |
768 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 769 | { |
807315e6 | 770 | 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)); |
ef416fc2 | 771 | |
ef416fc2 | 772 | /* |
773 | * Print the file(s)... | |
774 | */ | |
775 | ||
3d052e43 | 776 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title, |
ef416fc2 | 777 | num_options, options)); |
778 | } | |
779 | ||
780 | ||
ef416fc2 | 781 | /* |
fa73b229 | 782 | * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the |
783 | * specified server. | |
ef416fc2 | 784 | * |
53af7f21 | 785 | * @since CUPS 1.1.21/macOS 10.4@ @exclude all@ |
ef416fc2 | 786 | */ |
787 | ||
5a738aea | 788 | int /* O - Job ID or 0 on error */ |
3d052e43 | 789 | cupsPrintFiles2( |
568fa3fa MS |
790 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
791 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
792 | int num_files, /* I - Number of files */ |
793 | const char **files, /* I - File(s) to print */ | |
794 | const char *title, /* I - Title of job */ | |
795 | int num_options, /* I - Number of options */ | |
796 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 797 | { |
798 | int i; /* Looping var */ | |
3d052e43 MS |
799 | int job_id; /* New job ID */ |
800 | const char *docname; /* Basename of current filename */ | |
801 | const char *format; /* Document format */ | |
802 | cups_file_t *fp; /* Current file */ | |
803 | char buffer[8192]; /* Copy buffer */ | |
804 | ssize_t bytes; /* Bytes in buffer */ | |
805 | http_status_t status; /* Status of write */ | |
5a662dc0 MS |
806 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ |
807 | ipp_status_t cancel_status; /* Status code to preserve */ | |
808 | char *cancel_message; /* Error message to preserve */ | |
ef416fc2 | 809 | |
810 | ||
807315e6 | 811 | 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)); |
ef416fc2 | 812 | |
813 | /* | |
814 | * Range check input... | |
815 | */ | |
816 | ||
3d052e43 | 817 | if (!name || num_files < 1 || !files) |
ef416fc2 | 818 | { |
cb7f98ee | 819 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
ef416fc2 | 820 | |
821 | return (0); | |
822 | } | |
823 | ||
824 | /* | |
3d052e43 | 825 | * Create the print job... |
ef416fc2 | 826 | */ |
827 | ||
3d052e43 | 828 | if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0) |
ef416fc2 | 829 | return (0); |
ef416fc2 | 830 | |
831 | /* | |
3d052e43 | 832 | * Send each of the files... |
ef416fc2 | 833 | */ |
834 | ||
3d052e43 MS |
835 | if (cupsGetOption("raw", num_options, options)) |
836 | format = CUPS_FORMAT_RAW; | |
837 | else if ((format = cupsGetOption("document-format", num_options, | |
838 | options)) == NULL) | |
839 | format = CUPS_FORMAT_AUTO; | |
ef416fc2 | 840 | |
3d052e43 MS |
841 | for (i = 0; i < num_files; i ++) |
842 | { | |
843 | /* | |
844 | * Start the next file... | |
845 | */ | |
ef416fc2 | 846 | |
3d052e43 MS |
847 | if ((docname = strrchr(files[i], '/')) != NULL) |
848 | docname ++; | |
849 | else | |
850 | docname = files[i]; | |
ef416fc2 | 851 | |
3d052e43 MS |
852 | if ((fp = cupsFileOpen(files[i], "rb")) == NULL) |
853 | { | |
854 | /* | |
855 | * Unable to open print file, cancel the job and return... | |
856 | */ | |
ef416fc2 | 857 | |
cb7f98ee | 858 | _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_ACCESS, NULL, 0); |
5a662dc0 | 859 | goto cancel_job; |
3d052e43 | 860 | } |
ef416fc2 | 861 | |
ba55dc12 MS |
862 | status = cupsStartDocument(http, name, job_id, docname, format, |
863 | i == (num_files - 1)); | |
ef416fc2 | 864 | |
cb7f98ee | 865 | while (status == HTTP_STATUS_CONTINUE && |
ba55dc12 | 866 | (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) |
7e86f2f6 | 867 | status = cupsWriteRequestData(http, buffer, (size_t)bytes); |
ef416fc2 | 868 | |
3d052e43 | 869 | cupsFileClose(fp); |
ef416fc2 | 870 | |
cb7f98ee | 871 | if (status != HTTP_STATUS_CONTINUE || cupsFinishDocument(http, name) != IPP_STATUS_OK) |
ef416fc2 | 872 | { |
873 | /* | |
3d052e43 | 874 | * Unable to queue, cancel the job and return... |
ef416fc2 | 875 | */ |
876 | ||
5a662dc0 | 877 | goto cancel_job; |
3d052e43 MS |
878 | } |
879 | } | |
ef416fc2 | 880 | |
3d052e43 | 881 | return (job_id); |
5a662dc0 MS |
882 | |
883 | /* | |
884 | * If we get here, something happened while sending the print job so we need | |
885 | * to cancel the job without setting the last error (since we need to preserve | |
886 | * the current error... | |
887 | */ | |
888 | ||
889 | cancel_job: | |
890 | ||
891 | cancel_status = cg->last_error; | |
892 | cancel_message = cg->last_status_message ? | |
893 | _cupsStrRetain(cg->last_status_message) : NULL; | |
894 | ||
895 | cupsCancelJob2(http, name, job_id, 0); | |
896 | ||
897 | cg->last_error = cancel_status; | |
898 | cg->last_status_message = cancel_message; | |
899 | ||
900 | return (0); | |
3d052e43 | 901 | } |
ef416fc2 | 902 | |
ef416fc2 | 903 | |
3d052e43 MS |
904 | /* |
905 | * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob(). | |
906 | * | |
5a738aea MS |
907 | * Use @link cupsWriteRequestData@ to write data for the document and |
908 | * @link cupsFinishDocument@ to finish the document and get the submission status. | |
3d052e43 | 909 | * |
5a738aea MS |
910 | * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@, |
911 | * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and | |
912 | * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although | |
913 | * any supported MIME type string can be supplied. | |
3d052e43 | 914 | * |
53af7f21 | 915 | * @since CUPS 1.4/macOS 10.6@ @exclude all@ |
3d052e43 | 916 | */ |
ef416fc2 | 917 | |
3d052e43 MS |
918 | http_status_t /* O - HTTP status of request */ |
919 | cupsStartDocument( | |
568fa3fa MS |
920 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
921 | const char *name, /* I - Destination name */ | |
5a738aea | 922 | int job_id, /* I - Job ID from @link cupsCreateJob@ */ |
3d052e43 | 923 | const char *docname, /* I - Name of document */ |
5a738aea | 924 | const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */ |
3d052e43 MS |
925 | int last_document) /* I - 1 for last document in job, 0 otherwise */ |
926 | { | |
927 | char resource[1024], /* Resource for destinatio */ | |
928 | printer_uri[1024]; /* Printer URI */ | |
929 | ipp_t *request; /* Send-Document request */ | |
930 | http_status_t status; /* HTTP status */ | |
ef416fc2 | 931 | |
bd7854cb | 932 | |
3d052e43 MS |
933 | /* |
934 | * Create a Send-Document request... | |
935 | */ | |
bd7854cb | 936 | |
cb7f98ee | 937 | if ((request = ippNewRequest(IPP_OP_SEND_DOCUMENT)) == NULL) |
3d052e43 | 938 | { |
cb7f98ee MS |
939 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0); |
940 | return (HTTP_STATUS_ERROR); | |
3d052e43 | 941 | } |
bd7854cb | 942 | |
3d052e43 MS |
943 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", |
944 | NULL, "localhost", ippPort(), "/printers/%s", name); | |
945 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
ef416fc2 | 946 | |
3d052e43 MS |
947 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
948 | NULL, printer_uri); | |
949 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id); | |
950 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
951 | NULL, cupsUser()); | |
952 | if (docname) | |
953 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name", | |
954 | NULL, docname); | |
955 | if (format) | |
956 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, | |
957 | "document-format", NULL, format); | |
7e86f2f6 | 958 | ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", (char)last_document); |
ef416fc2 | 959 | |
3d052e43 MS |
960 | /* |
961 | * Send and delete the request, then return the status... | |
962 | */ | |
ef416fc2 | 963 | |
3d052e43 | 964 | status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE); |
ef416fc2 | 965 | |
3d052e43 | 966 | ippDelete(request); |
ef416fc2 | 967 | |
3d052e43 | 968 | return (status); |
ef416fc2 | 969 | } |
7536de1a MS |
970 | |
971 | ||
972 | /* | |
973 | * 'cups_create_cb()' - Find the destination for printing. | |
974 | */ | |
975 | ||
976 | static int /* O - 0 on match */ | |
977 | cups_create_cb( | |
978 | _cups_createdata_t *data, /* I - Data from cupsCreateJob call */ | |
979 | unsigned flags, /* I - Enumeration flags */ | |
980 | cups_dest_t *dest) /* I - Destination */ | |
981 | { | |
2a75f21b | 982 | DEBUG_printf(("2cups_create_cb(data=%p(%s), flags=%08x, dest=%p(%s))", (void *)data, data->name, flags, (void *)dest, dest->name)); |
7536de1a MS |
983 | |
984 | (void)flags; | |
985 | ||
986 | if (dest->instance || strcasecmp(data->name, dest->name)) | |
987 | return (1); | |
988 | ||
989 | cupsCopyDest(dest, 0, &data->dest); | |
990 | ||
991 | return (0); | |
992 | } |