]>
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 | * |
7e86f2f6 MS |
7 | * These coded instructions, statements, and computer programs are the |
8 | * property of Apple Inc. and are protected by Federal copyright | |
9 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
10 | * which should have been included with this file. If this file is | |
57b7b66b | 11 | * missing or damaged, see the license at "http://www.cups.org/". |
ef416fc2 | 12 | * |
7e86f2f6 | 13 | * This file is subject to the Apple OS-Developed Software exception. |
ef416fc2 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
71e16022 | 20 | #include "cups-private.h" |
ef416fc2 | 21 | #include <fcntl.h> |
22 | #include <sys/stat.h> | |
23 | #if defined(WIN32) || defined(__EMX__) | |
24 | # include <io.h> | |
25 | #else | |
26 | # include <unistd.h> | |
27 | #endif /* WIN32 || __EMX__ */ | |
28 | ||
29 | ||
7536de1a MS |
30 | /* |
31 | * Enumeration data and callback... | |
32 | */ | |
33 | ||
34 | typedef struct _cups_createdata_s | |
35 | { | |
36 | const char *name; /* Destination name */ | |
37 | cups_dest_t *dest; /* Matching destination */ | |
38 | } _cups_createdata_t; | |
39 | ||
40 | static int cups_create_cb(_cups_createdata_t *data, unsigned flags, cups_dest_t *dest); | |
41 | ||
42 | ||
ef416fc2 | 43 | /* |
44 | * 'cupsCancelJob()' - Cancel a print job on the default server. | |
45 | * | |
5a738aea MS |
46 | * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ |
47 | * to cancel the current job on the named destination. | |
48 | * | |
49 | * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get | |
ef416fc2 | 50 | * the cause of any failure. |
51 | */ | |
52 | ||
53 | int /* O - 1 on success, 0 on failure */ | |
54 | cupsCancelJob(const char *name, /* I - Name of printer or class */ | |
568fa3fa | 55 | int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ |
ef416fc2 | 56 | { |
5a738aea | 57 | return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) |
cb7f98ee | 58 | < IPP_STATUS_REDIRECTION_OTHER_SITE); |
3d052e43 MS |
59 | } |
60 | ||
61 | ||
62 | /* | |
63 | * 'cupsCancelJob2()' - Cancel or purge a print job. | |
64 | * | |
65 | * Canceled jobs remain in the job history while purged jobs are removed | |
66 | * from the job history. | |
67 | * | |
5a738aea MS |
68 | * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ |
69 | * to cancel the current job on the named destination. | |
70 | * | |
71 | * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get | |
3d052e43 MS |
72 | * the cause of any failure. |
73 | * | |
8072030b | 74 | * @since CUPS 1.4/macOS 10.6@ |
3d052e43 MS |
75 | */ |
76 | ||
77 | ipp_status_t /* O - IPP status */ | |
568fa3fa | 78 | cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
5a738aea | 79 | const char *name, /* I - Name of printer or class */ |
568fa3fa | 80 | int job_id, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ |
5a738aea | 81 | int purge) /* I - 1 to purge, 0 to cancel */ |
3d052e43 | 82 | { |
5a738aea | 83 | char uri[HTTP_MAX_URI]; /* Job/printer URI */ |
3d052e43 | 84 | ipp_t *request; /* IPP request */ |
ef416fc2 | 85 | |
86 | ||
87 | /* | |
3d052e43 | 88 | * Range check input... |
ef416fc2 | 89 | */ |
90 | ||
5a738aea | 91 | if (job_id < -1 || (!name && job_id == 0)) |
ef416fc2 | 92 | { |
cb7f98ee | 93 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
ef416fc2 | 94 | return (0); |
95 | } | |
96 | ||
97 | /* | |
3d052e43 | 98 | * Connect to the default server as needed... |
ef416fc2 | 99 | */ |
100 | ||
3d052e43 MS |
101 | if (!http) |
102 | if ((http = _cupsConnect()) == NULL) | |
cb7f98ee | 103 | return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE); |
ef416fc2 | 104 | |
105 | /* | |
5a738aea | 106 | * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following |
ef416fc2 | 107 | * attributes: |
108 | * | |
109 | * attributes-charset | |
110 | * attributes-natural-language | |
5a738aea | 111 | * job-uri or printer-uri + job-id |
3d052e43 | 112 | * requesting-user-name |
5a738aea | 113 | * [purge-job] or [purge-jobs] |
ef416fc2 | 114 | */ |
115 | ||
cb7f98ee | 116 | request = ippNewRequest(job_id < 0 ? IPP_OP_PURGE_JOBS : IPP_OP_CANCEL_JOB); |
ef416fc2 | 117 | |
5a738aea MS |
118 | if (name) |
119 | { | |
120 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
121 | "localhost", ippPort(), "/printers/%s", name); | |
122 | ||
123 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
124 | uri); | |
125 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
126 | job_id); | |
127 | } | |
128 | else if (job_id > 0) | |
129 | { | |
130 | snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id); | |
131 | ||
132 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); | |
133 | } | |
ef416fc2 | 134 | |
135 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
136 | NULL, cupsUser()); | |
5a738aea MS |
137 | |
138 | if (purge && job_id >= 0) | |
3d052e43 | 139 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1); |
5a738aea MS |
140 | else if (!purge && job_id < 0) |
141 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0); | |
ef416fc2 | 142 | |
143 | /* | |
144 | * Do the request... | |
145 | */ | |
146 | ||
3d052e43 MS |
147 | ippDelete(cupsDoRequest(http, request, "/jobs/")); |
148 | ||
149 | return (cupsLastError()); | |
150 | } | |
151 | ||
152 | ||
153 | /* | |
568fa3fa | 154 | * 'cupsCreateJob()' - Create an empty job for streaming. |
3d052e43 | 155 | * |
568fa3fa MS |
156 | * Use this function when you want to stream print data using the |
157 | * @link cupsStartDocument@, @link cupsWriteRequestData@, and | |
158 | * @link cupsFinishDocument@ functions. If you have one or more files to | |
159 | * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function | |
160 | * instead. | |
3d052e43 | 161 | * |
8072030b | 162 | * @since CUPS 1.4/macOS 10.6@ |
3d052e43 MS |
163 | */ |
164 | ||
165 | int /* O - Job ID or 0 on error */ | |
166 | cupsCreateJob( | |
568fa3fa MS |
167 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
168 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
169 | const char *title, /* I - Title of job */ |
170 | int num_options, /* I - Number of options */ | |
171 | cups_option_t *options) /* I - Options */ | |
172 | { | |
3d052e43 | 173 | int job_id = 0; /* job-id value */ |
7536de1a MS |
174 | ipp_status_t status; /* Create-Job status */ |
175 | _cups_createdata_t data; /* Enumeration data */ | |
176 | cups_dinfo_t *info; /* Destination information */ | |
3d052e43 MS |
177 | |
178 | ||
807315e6 | 179 | DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", num_options=%d, options=%p)", (void *)http, name, title, num_options, (void *)options)); |
3d052e43 MS |
180 | |
181 | /* | |
182 | * Range check input... | |
183 | */ | |
184 | ||
185 | if (!name) | |
186 | { | |
cb7f98ee | 187 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
3d052e43 MS |
188 | return (0); |
189 | } | |
190 | ||
191 | /* | |
7536de1a | 192 | * Lookup the destination... |
3d052e43 MS |
193 | */ |
194 | ||
7536de1a MS |
195 | data.name = name; |
196 | data.dest = NULL; | |
197 | ||
f50db552 | 198 | cupsEnumDests(0, 1000, NULL, 0, 0, (cups_dest_cb_t)cups_create_cb, &data); |
7536de1a MS |
199 | |
200 | if (!data.dest) | |
3d052e43 | 201 | { |
7536de1a MS |
202 | DEBUG_puts("1cupsCreateJob: Destination not found."); |
203 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0); | |
3d052e43 MS |
204 | return (0); |
205 | } | |
206 | ||
3d052e43 | 207 | /* |
7536de1a | 208 | * Query dest information and create the job... |
3d052e43 MS |
209 | */ |
210 | ||
7536de1a MS |
211 | DEBUG_puts("1cupsCreateJob: Querying destination info."); |
212 | if ((info = cupsCopyDestInfo(http, data.dest)) == NULL) | |
213 | { | |
214 | DEBUG_puts("1cupsCreateJob: Query failed."); | |
215 | cupsFreeDests(1, data.dest); | |
216 | return (0); | |
217 | } | |
3d052e43 | 218 | |
7536de1a MS |
219 | status = cupsCreateDestJob(http, data.dest, info, &job_id, title, num_options, options); |
220 | DEBUG_printf(("1cupsCreateJob: cupsCreateDestJob returned %04x (%s)", status, ippErrorString(status))); | |
3d052e43 | 221 | |
7536de1a MS |
222 | cupsFreeDestInfo(info); |
223 | cupsFreeDests(1, data.dest); | |
3d052e43 MS |
224 | |
225 | /* | |
7536de1a | 226 | * Return the job... |
3d052e43 MS |
227 | */ |
228 | ||
7536de1a MS |
229 | if (status >= IPP_STATUS_REDIRECTION_OTHER_SITE) |
230 | return (0); | |
231 | else | |
232 | return (job_id); | |
3d052e43 MS |
233 | } |
234 | ||
235 | ||
236 | /* | |
237 | * 'cupsFinishDocument()' - Finish sending a document. | |
238 | * | |
5a738aea MS |
239 | * The document must have been started using @link cupsStartDocument@. |
240 | * | |
8072030b | 241 | * @since CUPS 1.4/macOS 10.6@ |
3d052e43 MS |
242 | */ |
243 | ||
244 | ipp_status_t /* O - Status of document submission */ | |
568fa3fa MS |
245 | cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
246 | const char *name) /* I - Destination name */ | |
3d052e43 MS |
247 | { |
248 | char resource[1024]; /* Printer resource */ | |
249 | ||
250 | ||
251 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
ef416fc2 | 252 | |
3d052e43 MS |
253 | ippDelete(cupsGetResponse(http, resource)); |
254 | ||
255 | return (cupsLastError()); | |
ef416fc2 | 256 | } |
257 | ||
258 | ||
ef416fc2 | 259 | /* |
260 | * 'cupsFreeJobs()' - Free memory used by job data. | |
261 | */ | |
262 | ||
263 | void | |
264 | cupsFreeJobs(int num_jobs, /* I - Number of jobs */ | |
265 | cups_job_t *jobs) /* I - Jobs */ | |
266 | { | |
3d052e43 MS |
267 | int i; /* Looping var */ |
268 | cups_job_t *job; /* Current job */ | |
ef416fc2 | 269 | |
270 | ||
3d052e43 | 271 | if (num_jobs <= 0 || !jobs) |
ef416fc2 | 272 | return; |
273 | ||
3d052e43 | 274 | for (i = num_jobs, job = jobs; i > 0; i --, job ++) |
ef416fc2 | 275 | { |
3d052e43 MS |
276 | _cupsStrFree(job->dest); |
277 | _cupsStrFree(job->user); | |
278 | _cupsStrFree(job->format); | |
279 | _cupsStrFree(job->title); | |
ef416fc2 | 280 | } |
281 | ||
282 | free(jobs); | |
283 | } | |
284 | ||
285 | ||
286 | /* | |
287 | * 'cupsGetClasses()' - Get a list of printer classes from the default server. | |
288 | * | |
240214ef MS |
289 | * This function is deprecated and no longer returns a list of printer |
290 | * classes - use @link cupsGetDests@ instead. | |
ef416fc2 | 291 | * |
292 | * @deprecated@ | |
293 | */ | |
294 | ||
295 | int /* O - Number of classes */ | |
296 | cupsGetClasses(char ***classes) /* O - Classes */ | |
297 | { | |
240214ef MS |
298 | if (classes) |
299 | *classes = NULL; | |
ef416fc2 | 300 | |
240214ef | 301 | return (0); |
ef416fc2 | 302 | } |
303 | ||
304 | ||
305 | /* | |
306 | * 'cupsGetDefault()' - Get the default printer or class for the default server. | |
307 | * | |
308 | * This function returns the default printer or class as defined by | |
309 | * the LPDEST or PRINTER environment variables. If these environment | |
310 | * variables are not set, the server default destination is returned. | |
5a738aea MS |
311 | * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ |
312 | * functions to get the user-defined default printer, as this function does | |
313 | * not support the lpoptions-defined default printer. | |
ef416fc2 | 314 | */ |
315 | ||
5a738aea | 316 | const char * /* O - Default printer or @code NULL@ */ |
ef416fc2 | 317 | cupsGetDefault(void) |
318 | { | |
ef416fc2 | 319 | /* |
320 | * Return the default printer... | |
321 | */ | |
322 | ||
3d052e43 | 323 | return (cupsGetDefault2(CUPS_HTTP_DEFAULT)); |
ef416fc2 | 324 | } |
325 | ||
326 | ||
327 | /* | |
328 | * 'cupsGetDefault2()' - Get the default printer or class for the specified server. | |
329 | * | |
330 | * This function returns the default printer or class as defined by | |
331 | * the LPDEST or PRINTER environment variables. If these environment | |
332 | * variables are not set, the server default destination is returned. | |
5a738aea MS |
333 | * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ |
334 | * functions to get the user-defined default printer, as this function does | |
335 | * not support the lpoptions-defined default printer. | |
ef416fc2 | 336 | * |
8072030b | 337 | * @since CUPS 1.1.21/macOS 10.4@ |
ef416fc2 | 338 | */ |
339 | ||
5a738aea | 340 | const char * /* O - Default printer or @code NULL@ */ |
568fa3fa | 341 | cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 342 | { |
343 | ipp_t *request, /* IPP Request */ | |
344 | *response; /* IPP Response */ | |
345 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 346 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
347 | ||
348 | ||
349 | /* | |
38e73f87 | 350 | * See if we have a user default printer set... |
ef416fc2 | 351 | */ |
352 | ||
38e73f87 MS |
353 | if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer))) |
354 | return (cg->def_printer); | |
ef416fc2 | 355 | |
356 | /* | |
3d052e43 | 357 | * Connect to the server as needed... |
ef416fc2 | 358 | */ |
359 | ||
360 | if (!http) | |
3d052e43 MS |
361 | if ((http = _cupsConnect()) == NULL) |
362 | return (NULL); | |
ef416fc2 | 363 | |
364 | /* | |
365 | * Build a CUPS_GET_DEFAULT request, which requires the following | |
366 | * attributes: | |
367 | * | |
368 | * attributes-charset | |
369 | * attributes-natural-language | |
370 | */ | |
371 | ||
cb7f98ee | 372 | request = ippNewRequest(IPP_OP_CUPS_GET_DEFAULT); |
ef416fc2 | 373 | |
374 | /* | |
375 | * Do the request and get back a response... | |
376 | */ | |
377 | ||
378 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
379 | { | |
3d052e43 MS |
380 | if ((attr = ippFindAttribute(response, "printer-name", |
381 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 382 | { |
3d052e43 MS |
383 | strlcpy(cg->def_printer, attr->values[0].string.text, |
384 | sizeof(cg->def_printer)); | |
ef416fc2 | 385 | ippDelete(response); |
386 | return (cg->def_printer); | |
387 | } | |
388 | ||
389 | ippDelete(response); | |
390 | } | |
391 | ||
392 | return (NULL); | |
393 | } | |
394 | ||
395 | ||
396 | /* | |
397 | * 'cupsGetJobs()' - Get the jobs from the default server. | |
5a738aea MS |
398 | * |
399 | * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless | |
400 | * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are | |
401 | * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns | |
402 | * jobs that are stopped, canceled, aborted, or completed. | |
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 | * |
698 | * @deprecated@ | |
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. | |
713 | */ | |
714 | ||
5a738aea | 715 | int /* O - Job ID or 0 on error */ |
568fa3fa | 716 | cupsPrintFile(const char *name, /* I - Destination name */ |
ef416fc2 | 717 | const char *filename, /* I - File to print */ |
718 | const char *title, /* I - Title of job */ | |
719 | int num_options,/* I - Number of options */ | |
720 | cups_option_t *options) /* I - Options */ | |
721 | { | |
807315e6 | 722 | DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", title=\"%s\", num_options=%d, options=%p)", name, filename, title, num_options, (void *)options)); |
ef416fc2 | 723 | |
3d052e43 MS |
724 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title, |
725 | num_options, options)); | |
ef416fc2 | 726 | } |
727 | ||
728 | ||
729 | /* | |
5a738aea MS |
730 | * 'cupsPrintFile2()' - Print a file to a printer or class on the specified |
731 | * server. | |
ef416fc2 | 732 | * |
8072030b | 733 | * @since CUPS 1.1.21/macOS 10.4@ |
ef416fc2 | 734 | */ |
735 | ||
5a738aea | 736 | int /* O - Job ID or 0 on error */ |
3d052e43 | 737 | cupsPrintFile2( |
568fa3fa MS |
738 | http_t *http, /* I - Connection to server */ |
739 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
740 | const char *filename, /* I - File to print */ |
741 | const char *title, /* I - Title of job */ | |
742 | int num_options, /* I - Number of options */ | |
743 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 744 | { |
807315e6 | 745 | 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 | 746 | |
3d052e43 MS |
747 | return (cupsPrintFiles2(http, name, 1, &filename, title, num_options, |
748 | options)); | |
ef416fc2 | 749 | } |
750 | ||
751 | ||
752 | /* | |
fa73b229 | 753 | * 'cupsPrintFiles()' - Print one or more files to a printer or class on the |
754 | * default server. | |
ef416fc2 | 755 | */ |
756 | ||
5a738aea | 757 | int /* O - Job ID or 0 on error */ |
3d052e43 | 758 | cupsPrintFiles( |
568fa3fa | 759 | const char *name, /* I - Destination name */ |
3d052e43 MS |
760 | int num_files, /* I - Number of files */ |
761 | const char **files, /* I - File(s) to print */ | |
762 | const char *title, /* I - Title of job */ | |
763 | int num_options, /* I - Number of options */ | |
764 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 765 | { |
807315e6 | 766 | 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 | 767 | |
ef416fc2 | 768 | /* |
769 | * Print the file(s)... | |
770 | */ | |
771 | ||
3d052e43 | 772 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title, |
ef416fc2 | 773 | num_options, options)); |
774 | } | |
775 | ||
776 | ||
ef416fc2 | 777 | /* |
fa73b229 | 778 | * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the |
779 | * specified server. | |
ef416fc2 | 780 | * |
8072030b | 781 | * @since CUPS 1.1.21/macOS 10.4@ |
ef416fc2 | 782 | */ |
783 | ||
5a738aea | 784 | int /* O - Job ID or 0 on error */ |
3d052e43 | 785 | cupsPrintFiles2( |
568fa3fa MS |
786 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
787 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
788 | int num_files, /* I - Number of files */ |
789 | const char **files, /* I - File(s) to print */ | |
790 | const char *title, /* I - Title of job */ | |
791 | int num_options, /* I - Number of options */ | |
792 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 793 | { |
794 | int i; /* Looping var */ | |
3d052e43 MS |
795 | int job_id; /* New job ID */ |
796 | const char *docname; /* Basename of current filename */ | |
797 | const char *format; /* Document format */ | |
798 | cups_file_t *fp; /* Current file */ | |
799 | char buffer[8192]; /* Copy buffer */ | |
800 | ssize_t bytes; /* Bytes in buffer */ | |
801 | http_status_t status; /* Status of write */ | |
5a662dc0 MS |
802 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ |
803 | ipp_status_t cancel_status; /* Status code to preserve */ | |
804 | char *cancel_message; /* Error message to preserve */ | |
ef416fc2 | 805 | |
806 | ||
807315e6 | 807 | 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 | 808 | |
809 | /* | |
810 | * Range check input... | |
811 | */ | |
812 | ||
3d052e43 | 813 | if (!name || num_files < 1 || !files) |
ef416fc2 | 814 | { |
cb7f98ee | 815 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
ef416fc2 | 816 | |
817 | return (0); | |
818 | } | |
819 | ||
820 | /* | |
3d052e43 | 821 | * Create the print job... |
ef416fc2 | 822 | */ |
823 | ||
3d052e43 | 824 | if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0) |
ef416fc2 | 825 | return (0); |
ef416fc2 | 826 | |
827 | /* | |
3d052e43 | 828 | * Send each of the files... |
ef416fc2 | 829 | */ |
830 | ||
3d052e43 MS |
831 | if (cupsGetOption("raw", num_options, options)) |
832 | format = CUPS_FORMAT_RAW; | |
833 | else if ((format = cupsGetOption("document-format", num_options, | |
834 | options)) == NULL) | |
835 | format = CUPS_FORMAT_AUTO; | |
ef416fc2 | 836 | |
3d052e43 MS |
837 | for (i = 0; i < num_files; i ++) |
838 | { | |
839 | /* | |
840 | * Start the next file... | |
841 | */ | |
ef416fc2 | 842 | |
3d052e43 MS |
843 | if ((docname = strrchr(files[i], '/')) != NULL) |
844 | docname ++; | |
845 | else | |
846 | docname = files[i]; | |
ef416fc2 | 847 | |
3d052e43 MS |
848 | if ((fp = cupsFileOpen(files[i], "rb")) == NULL) |
849 | { | |
850 | /* | |
851 | * Unable to open print file, cancel the job and return... | |
852 | */ | |
ef416fc2 | 853 | |
cb7f98ee | 854 | _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_ACCESS, NULL, 0); |
5a662dc0 | 855 | goto cancel_job; |
3d052e43 | 856 | } |
ef416fc2 | 857 | |
ba55dc12 MS |
858 | status = cupsStartDocument(http, name, job_id, docname, format, |
859 | i == (num_files - 1)); | |
ef416fc2 | 860 | |
cb7f98ee | 861 | while (status == HTTP_STATUS_CONTINUE && |
ba55dc12 | 862 | (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) |
7e86f2f6 | 863 | status = cupsWriteRequestData(http, buffer, (size_t)bytes); |
ef416fc2 | 864 | |
3d052e43 | 865 | cupsFileClose(fp); |
ef416fc2 | 866 | |
cb7f98ee | 867 | if (status != HTTP_STATUS_CONTINUE || cupsFinishDocument(http, name) != IPP_STATUS_OK) |
ef416fc2 | 868 | { |
869 | /* | |
3d052e43 | 870 | * Unable to queue, cancel the job and return... |
ef416fc2 | 871 | */ |
872 | ||
5a662dc0 | 873 | goto cancel_job; |
3d052e43 MS |
874 | } |
875 | } | |
ef416fc2 | 876 | |
3d052e43 | 877 | return (job_id); |
5a662dc0 MS |
878 | |
879 | /* | |
880 | * If we get here, something happened while sending the print job so we need | |
881 | * to cancel the job without setting the last error (since we need to preserve | |
882 | * the current error... | |
883 | */ | |
884 | ||
885 | cancel_job: | |
886 | ||
887 | cancel_status = cg->last_error; | |
888 | cancel_message = cg->last_status_message ? | |
889 | _cupsStrRetain(cg->last_status_message) : NULL; | |
890 | ||
891 | cupsCancelJob2(http, name, job_id, 0); | |
892 | ||
893 | cg->last_error = cancel_status; | |
894 | cg->last_status_message = cancel_message; | |
895 | ||
896 | return (0); | |
3d052e43 | 897 | } |
ef416fc2 | 898 | |
ef416fc2 | 899 | |
3d052e43 MS |
900 | /* |
901 | * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob(). | |
902 | * | |
5a738aea MS |
903 | * Use @link cupsWriteRequestData@ to write data for the document and |
904 | * @link cupsFinishDocument@ to finish the document and get the submission status. | |
3d052e43 | 905 | * |
5a738aea MS |
906 | * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@, |
907 | * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and | |
908 | * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although | |
909 | * any supported MIME type string can be supplied. | |
3d052e43 | 910 | * |
8072030b | 911 | * @since CUPS 1.4/macOS 10.6@ |
3d052e43 | 912 | */ |
ef416fc2 | 913 | |
3d052e43 MS |
914 | http_status_t /* O - HTTP status of request */ |
915 | cupsStartDocument( | |
568fa3fa MS |
916 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
917 | const char *name, /* I - Destination name */ | |
5a738aea | 918 | int job_id, /* I - Job ID from @link cupsCreateJob@ */ |
3d052e43 | 919 | const char *docname, /* I - Name of document */ |
5a738aea | 920 | const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */ |
3d052e43 MS |
921 | int last_document) /* I - 1 for last document in job, 0 otherwise */ |
922 | { | |
923 | char resource[1024], /* Resource for destinatio */ | |
924 | printer_uri[1024]; /* Printer URI */ | |
925 | ipp_t *request; /* Send-Document request */ | |
926 | http_status_t status; /* HTTP status */ | |
ef416fc2 | 927 | |
bd7854cb | 928 | |
3d052e43 MS |
929 | /* |
930 | * Create a Send-Document request... | |
931 | */ | |
bd7854cb | 932 | |
cb7f98ee | 933 | if ((request = ippNewRequest(IPP_OP_SEND_DOCUMENT)) == NULL) |
3d052e43 | 934 | { |
cb7f98ee MS |
935 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0); |
936 | return (HTTP_STATUS_ERROR); | |
3d052e43 | 937 | } |
bd7854cb | 938 | |
3d052e43 MS |
939 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", |
940 | NULL, "localhost", ippPort(), "/printers/%s", name); | |
941 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
ef416fc2 | 942 | |
3d052e43 MS |
943 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
944 | NULL, printer_uri); | |
945 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id); | |
946 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
947 | NULL, cupsUser()); | |
948 | if (docname) | |
949 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name", | |
950 | NULL, docname); | |
951 | if (format) | |
952 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, | |
953 | "document-format", NULL, format); | |
7e86f2f6 | 954 | ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", (char)last_document); |
ef416fc2 | 955 | |
3d052e43 MS |
956 | /* |
957 | * Send and delete the request, then return the status... | |
958 | */ | |
ef416fc2 | 959 | |
3d052e43 | 960 | status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE); |
ef416fc2 | 961 | |
3d052e43 | 962 | ippDelete(request); |
ef416fc2 | 963 | |
3d052e43 | 964 | return (status); |
ef416fc2 | 965 | } |
7536de1a MS |
966 | |
967 | ||
968 | /* | |
969 | * 'cups_create_cb()' - Find the destination for printing. | |
970 | */ | |
971 | ||
972 | static int /* O - 0 on match */ | |
973 | cups_create_cb( | |
974 | _cups_createdata_t *data, /* I - Data from cupsCreateJob call */ | |
975 | unsigned flags, /* I - Enumeration flags */ | |
976 | cups_dest_t *dest) /* I - Destination */ | |
977 | { | |
978 | DEBUG_printf(("2cups_create_cb(data=%p(%s), flags=%08x, dest=%p(%s))", data, data->name, flags, dest, dest->name)); | |
979 | ||
980 | (void)flags; | |
981 | ||
982 | if (dest->instance || strcasecmp(data->name, dest->name)) | |
983 | return (1); | |
984 | ||
985 | cupsCopyDest(dest, 0, &data->dest); | |
986 | ||
987 | return (0); | |
988 | } |