]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
354aadbe | 2 | * "$Id: util.c 13138 2016-03-15 14:59:54Z msweet $" |
ef416fc2 | 3 | * |
7e86f2f6 | 4 | * Printing utilities for CUPS. |
ef416fc2 | 5 | * |
4eff9409 | 6 | * Copyright 2007-2015 by Apple Inc. |
7e86f2f6 | 7 | * Copyright 1997-2006 by Easy Software Products. |
ef416fc2 | 8 | * |
7e86f2f6 MS |
9 | * These coded instructions, statements, and computer programs are the |
10 | * property of Apple Inc. and are protected by Federal copyright | |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
7e86f2f6 | 15 | * This file is subject to the Apple OS-Developed Software exception. |
ef416fc2 | 16 | */ |
17 | ||
18 | /* | |
19 | * Include necessary headers... | |
20 | */ | |
21 | ||
71e16022 | 22 | #include "cups-private.h" |
ef416fc2 | 23 | #include <fcntl.h> |
24 | #include <sys/stat.h> | |
25 | #if defined(WIN32) || defined(__EMX__) | |
26 | # include <io.h> | |
27 | #else | |
28 | # include <unistd.h> | |
29 | #endif /* WIN32 || __EMX__ */ | |
30 | ||
31 | ||
ef416fc2 | 32 | /* |
33 | * 'cupsCancelJob()' - Cancel a print job on the default server. | |
34 | * | |
5a738aea MS |
35 | * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ |
36 | * to cancel the current job on the named destination. | |
37 | * | |
38 | * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get | |
ef416fc2 | 39 | * the cause of any failure. |
40 | */ | |
41 | ||
42 | int /* O - 1 on success, 0 on failure */ | |
43 | cupsCancelJob(const char *name, /* I - Name of printer or class */ | |
568fa3fa | 44 | int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ |
ef416fc2 | 45 | { |
5a738aea | 46 | return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) |
cb7f98ee | 47 | < IPP_STATUS_REDIRECTION_OTHER_SITE); |
3d052e43 MS |
48 | } |
49 | ||
50 | ||
51 | /* | |
52 | * 'cupsCancelJob2()' - Cancel or purge a print job. | |
53 | * | |
54 | * Canceled jobs remain in the job history while purged jobs are removed | |
55 | * from the job history. | |
56 | * | |
5a738aea MS |
57 | * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ |
58 | * to cancel the current job on the named destination. | |
59 | * | |
60 | * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get | |
3d052e43 MS |
61 | * the cause of any failure. |
62 | * | |
f3c17241 | 63 | * @since CUPS 1.4/OS X 10.6@ |
3d052e43 MS |
64 | */ |
65 | ||
66 | ipp_status_t /* O - IPP status */ | |
568fa3fa | 67 | cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
5a738aea | 68 | const char *name, /* I - Name of printer or class */ |
568fa3fa | 69 | int job_id, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ |
5a738aea | 70 | int purge) /* I - 1 to purge, 0 to cancel */ |
3d052e43 | 71 | { |
5a738aea | 72 | char uri[HTTP_MAX_URI]; /* Job/printer URI */ |
3d052e43 | 73 | ipp_t *request; /* IPP request */ |
ef416fc2 | 74 | |
75 | ||
76 | /* | |
3d052e43 | 77 | * Range check input... |
ef416fc2 | 78 | */ |
79 | ||
5a738aea | 80 | if (job_id < -1 || (!name && job_id == 0)) |
ef416fc2 | 81 | { |
cb7f98ee | 82 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
ef416fc2 | 83 | return (0); |
84 | } | |
85 | ||
86 | /* | |
3d052e43 | 87 | * Connect to the default server as needed... |
ef416fc2 | 88 | */ |
89 | ||
3d052e43 MS |
90 | if (!http) |
91 | if ((http = _cupsConnect()) == NULL) | |
cb7f98ee | 92 | return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE); |
ef416fc2 | 93 | |
94 | /* | |
5a738aea | 95 | * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following |
ef416fc2 | 96 | * attributes: |
97 | * | |
98 | * attributes-charset | |
99 | * attributes-natural-language | |
5a738aea | 100 | * job-uri or printer-uri + job-id |
3d052e43 | 101 | * requesting-user-name |
5a738aea | 102 | * [purge-job] or [purge-jobs] |
ef416fc2 | 103 | */ |
104 | ||
cb7f98ee | 105 | request = ippNewRequest(job_id < 0 ? IPP_OP_PURGE_JOBS : IPP_OP_CANCEL_JOB); |
ef416fc2 | 106 | |
5a738aea MS |
107 | if (name) |
108 | { | |
109 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
110 | "localhost", ippPort(), "/printers/%s", name); | |
111 | ||
112 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
113 | uri); | |
114 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
115 | job_id); | |
116 | } | |
117 | else if (job_id > 0) | |
118 | { | |
119 | snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id); | |
120 | ||
121 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); | |
122 | } | |
ef416fc2 | 123 | |
124 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
125 | NULL, cupsUser()); | |
5a738aea MS |
126 | |
127 | if (purge && job_id >= 0) | |
3d052e43 | 128 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1); |
5a738aea MS |
129 | else if (!purge && job_id < 0) |
130 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0); | |
ef416fc2 | 131 | |
132 | /* | |
133 | * Do the request... | |
134 | */ | |
135 | ||
3d052e43 MS |
136 | ippDelete(cupsDoRequest(http, request, "/jobs/")); |
137 | ||
138 | return (cupsLastError()); | |
139 | } | |
140 | ||
141 | ||
142 | /* | |
568fa3fa | 143 | * 'cupsCreateJob()' - Create an empty job for streaming. |
3d052e43 | 144 | * |
568fa3fa MS |
145 | * Use this function when you want to stream print data using the |
146 | * @link cupsStartDocument@, @link cupsWriteRequestData@, and | |
147 | * @link cupsFinishDocument@ functions. If you have one or more files to | |
148 | * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function | |
149 | * instead. | |
3d052e43 | 150 | * |
f3c17241 | 151 | * @since CUPS 1.4/OS X 10.6@ |
3d052e43 MS |
152 | */ |
153 | ||
154 | int /* O - Job ID or 0 on error */ | |
155 | cupsCreateJob( | |
568fa3fa MS |
156 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
157 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
158 | const char *title, /* I - Title of job */ |
159 | int num_options, /* I - Number of options */ | |
160 | cups_option_t *options) /* I - Options */ | |
161 | { | |
162 | char printer_uri[1024], /* Printer URI */ | |
163 | resource[1024]; /* Printer resource */ | |
164 | ipp_t *request, /* Create-Job request */ | |
165 | *response; /* Create-Job response */ | |
166 | ipp_attribute_t *attr; /* job-id attribute */ | |
167 | int job_id = 0; /* job-id value */ | |
168 | ||
169 | ||
170 | DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", " | |
e07d4801 | 171 | "num_options=%d, options=%p)", |
3d052e43 MS |
172 | http, name, title, num_options, options)); |
173 | ||
174 | /* | |
175 | * Range check input... | |
176 | */ | |
177 | ||
178 | if (!name) | |
179 | { | |
cb7f98ee | 180 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
3d052e43 MS |
181 | return (0); |
182 | } | |
183 | ||
184 | /* | |
185 | * Build a Create-Job request... | |
186 | */ | |
187 | ||
cb7f98ee | 188 | if ((request = ippNewRequest(IPP_OP_CREATE_JOB)) == NULL) |
3d052e43 | 189 | { |
cb7f98ee | 190 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0); |
3d052e43 MS |
191 | return (0); |
192 | } | |
193 | ||
194 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", | |
195 | NULL, "localhost", ippPort(), "/printers/%s", name); | |
196 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
197 | ||
198 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
199 | NULL, printer_uri); | |
200 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
201 | NULL, cupsUser()); | |
202 | if (title) | |
203 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, | |
204 | title); | |
a469f8a5 | 205 | cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION); |
a29fd7dd MS |
206 | cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB); |
207 | cupsEncodeOptions2(request, num_options, options, IPP_TAG_SUBSCRIPTION); | |
3d052e43 MS |
208 | |
209 | /* | |
210 | * Send the request and get the job-id... | |
211 | */ | |
212 | ||
213 | response = cupsDoRequest(http, request, resource); | |
214 | ||
215 | if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL) | |
216 | job_id = attr->values[0].integer; | |
217 | ||
218 | ippDelete(response); | |
219 | ||
220 | /* | |
221 | * Return it... | |
222 | */ | |
223 | ||
224 | return (job_id); | |
225 | } | |
226 | ||
227 | ||
228 | /* | |
229 | * 'cupsFinishDocument()' - Finish sending a document. | |
230 | * | |
5a738aea MS |
231 | * The document must have been started using @link cupsStartDocument@. |
232 | * | |
f3c17241 | 233 | * @since CUPS 1.4/OS X 10.6@ |
3d052e43 MS |
234 | */ |
235 | ||
236 | ipp_status_t /* O - Status of document submission */ | |
568fa3fa MS |
237 | cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
238 | const char *name) /* I - Destination name */ | |
3d052e43 MS |
239 | { |
240 | char resource[1024]; /* Printer resource */ | |
241 | ||
242 | ||
243 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
ef416fc2 | 244 | |
3d052e43 MS |
245 | ippDelete(cupsGetResponse(http, resource)); |
246 | ||
247 | return (cupsLastError()); | |
ef416fc2 | 248 | } |
249 | ||
250 | ||
ef416fc2 | 251 | /* |
252 | * 'cupsFreeJobs()' - Free memory used by job data. | |
253 | */ | |
254 | ||
255 | void | |
256 | cupsFreeJobs(int num_jobs, /* I - Number of jobs */ | |
257 | cups_job_t *jobs) /* I - Jobs */ | |
258 | { | |
3d052e43 MS |
259 | int i; /* Looping var */ |
260 | cups_job_t *job; /* Current job */ | |
ef416fc2 | 261 | |
262 | ||
3d052e43 | 263 | if (num_jobs <= 0 || !jobs) |
ef416fc2 | 264 | return; |
265 | ||
3d052e43 | 266 | for (i = num_jobs, job = jobs; i > 0; i --, job ++) |
ef416fc2 | 267 | { |
3d052e43 MS |
268 | _cupsStrFree(job->dest); |
269 | _cupsStrFree(job->user); | |
270 | _cupsStrFree(job->format); | |
271 | _cupsStrFree(job->title); | |
ef416fc2 | 272 | } |
273 | ||
274 | free(jobs); | |
275 | } | |
276 | ||
277 | ||
278 | /* | |
279 | * 'cupsGetClasses()' - Get a list of printer classes from the default server. | |
280 | * | |
240214ef MS |
281 | * This function is deprecated and no longer returns a list of printer |
282 | * classes - use @link cupsGetDests@ instead. | |
ef416fc2 | 283 | * |
284 | * @deprecated@ | |
285 | */ | |
286 | ||
287 | int /* O - Number of classes */ | |
288 | cupsGetClasses(char ***classes) /* O - Classes */ | |
289 | { | |
240214ef MS |
290 | if (classes) |
291 | *classes = NULL; | |
ef416fc2 | 292 | |
240214ef | 293 | return (0); |
ef416fc2 | 294 | } |
295 | ||
296 | ||
297 | /* | |
298 | * 'cupsGetDefault()' - Get the default printer or class for the default server. | |
299 | * | |
300 | * This function returns the default printer or class as defined by | |
301 | * the LPDEST or PRINTER environment variables. If these environment | |
302 | * variables are not set, the server default destination is returned. | |
5a738aea MS |
303 | * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ |
304 | * functions to get the user-defined default printer, as this function does | |
305 | * not support the lpoptions-defined default printer. | |
ef416fc2 | 306 | */ |
307 | ||
5a738aea | 308 | const char * /* O - Default printer or @code NULL@ */ |
ef416fc2 | 309 | cupsGetDefault(void) |
310 | { | |
ef416fc2 | 311 | /* |
312 | * Return the default printer... | |
313 | */ | |
314 | ||
3d052e43 | 315 | return (cupsGetDefault2(CUPS_HTTP_DEFAULT)); |
ef416fc2 | 316 | } |
317 | ||
318 | ||
319 | /* | |
320 | * 'cupsGetDefault2()' - Get the default printer or class for the specified server. | |
321 | * | |
322 | * This function returns the default printer or class as defined by | |
323 | * the LPDEST or PRINTER environment variables. If these environment | |
324 | * variables are not set, the server default destination is returned. | |
5a738aea MS |
325 | * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ |
326 | * functions to get the user-defined default printer, as this function does | |
327 | * not support the lpoptions-defined default printer. | |
ef416fc2 | 328 | * |
f3c17241 | 329 | * @since CUPS 1.1.21/OS X 10.4@ |
ef416fc2 | 330 | */ |
331 | ||
5a738aea | 332 | const char * /* O - Default printer or @code NULL@ */ |
568fa3fa | 333 | cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 334 | { |
335 | ipp_t *request, /* IPP Request */ | |
336 | *response; /* IPP Response */ | |
337 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 338 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
339 | ||
340 | ||
341 | /* | |
38e73f87 | 342 | * See if we have a user default printer set... |
ef416fc2 | 343 | */ |
344 | ||
38e73f87 MS |
345 | if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer))) |
346 | return (cg->def_printer); | |
ef416fc2 | 347 | |
348 | /* | |
3d052e43 | 349 | * Connect to the server as needed... |
ef416fc2 | 350 | */ |
351 | ||
352 | if (!http) | |
3d052e43 MS |
353 | if ((http = _cupsConnect()) == NULL) |
354 | return (NULL); | |
ef416fc2 | 355 | |
356 | /* | |
357 | * Build a CUPS_GET_DEFAULT request, which requires the following | |
358 | * attributes: | |
359 | * | |
360 | * attributes-charset | |
361 | * attributes-natural-language | |
362 | */ | |
363 | ||
cb7f98ee | 364 | request = ippNewRequest(IPP_OP_CUPS_GET_DEFAULT); |
ef416fc2 | 365 | |
366 | /* | |
367 | * Do the request and get back a response... | |
368 | */ | |
369 | ||
370 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
371 | { | |
3d052e43 MS |
372 | if ((attr = ippFindAttribute(response, "printer-name", |
373 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 374 | { |
3d052e43 MS |
375 | strlcpy(cg->def_printer, attr->values[0].string.text, |
376 | sizeof(cg->def_printer)); | |
ef416fc2 | 377 | ippDelete(response); |
378 | return (cg->def_printer); | |
379 | } | |
380 | ||
381 | ippDelete(response); | |
382 | } | |
383 | ||
384 | return (NULL); | |
385 | } | |
386 | ||
387 | ||
388 | /* | |
389 | * 'cupsGetJobs()' - Get the jobs from the default server. | |
5a738aea MS |
390 | * |
391 | * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless | |
392 | * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are | |
393 | * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns | |
394 | * jobs that are stopped, canceled, aborted, or completed. | |
ef416fc2 | 395 | */ |
396 | ||
397 | int /* O - Number of jobs */ | |
398 | cupsGetJobs(cups_job_t **jobs, /* O - Job data */ | |
568fa3fa | 399 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ |
ecdc0628 | 400 | int myjobs, /* I - 0 = all users, 1 = mine */ |
5a738aea | 401 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ |
ef416fc2 | 402 | { |
ef416fc2 | 403 | /* |
404 | * Return the jobs... | |
405 | */ | |
406 | ||
5a738aea | 407 | return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs)); |
ef416fc2 | 408 | } |
409 | ||
410 | ||
411 | ||
412 | /* | |
413 | * 'cupsGetJobs2()' - Get the jobs from the specified server. | |
414 | * | |
5a738aea MS |
415 | * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless |
416 | * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are | |
417 | * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns | |
418 | * jobs that are stopped, canceled, aborted, or completed. | |
419 | * | |
f3c17241 | 420 | * @since CUPS 1.1.21/OS X 10.4@ |
ef416fc2 | 421 | */ |
422 | ||
423 | int /* O - Number of jobs */ | |
568fa3fa | 424 | cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 425 | cups_job_t **jobs, /* O - Job data */ |
568fa3fa | 426 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ |
ecdc0628 | 427 | int myjobs, /* I - 0 = all users, 1 = mine */ |
5a738aea | 428 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ |
ef416fc2 | 429 | { |
430 | int n; /* Number of jobs */ | |
431 | ipp_t *request, /* IPP Request */ | |
432 | *response; /* IPP Response */ | |
433 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 434 | cups_job_t *temp; /* Temporary pointer */ |
435 | int id, /* job-id */ | |
436 | priority, /* job-priority */ | |
437 | size; /* job-k-octets */ | |
438 | ipp_jstate_t state; /* job-state */ | |
439 | time_t completed_time, /* time-at-completed */ | |
440 | creation_time, /* time-at-creation */ | |
441 | processing_time; /* time-at-processing */ | |
442 | const char *dest, /* job-printer-uri */ | |
443 | *format, /* document-format */ | |
444 | *title, /* job-name */ | |
445 | *user; /* job-originating-user-name */ | |
446 | char uri[HTTP_MAX_URI]; /* URI for jobs */ | |
447 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
448 | static const char * const attrs[] = /* Requested attributes */ | |
449 | { | |
5a662dc0 | 450 | "document-format", |
ef416fc2 | 451 | "job-id", |
ef416fc2 | 452 | "job-k-octets", |
5a662dc0 MS |
453 | "job-name", |
454 | "job-originating-user-name", | |
455 | "job-printer-uri", | |
456 | "job-priority", | |
ef416fc2 | 457 | "job-state", |
458 | "time-at-completed", | |
459 | "time-at-creation", | |
5a662dc0 | 460 | "time-at-processing" |
ef416fc2 | 461 | }; |
462 | ||
463 | ||
464 | /* | |
465 | * Range check input... | |
466 | */ | |
467 | ||
3d052e43 | 468 | if (!jobs) |
ef416fc2 | 469 | { |
cb7f98ee | 470 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
ef416fc2 | 471 | |
472 | return (-1); | |
473 | } | |
474 | ||
475 | /* | |
476 | * Get the right URI... | |
477 | */ | |
478 | ||
5a738aea | 479 | if (name) |
ef416fc2 | 480 | { |
a4d04587 | 481 | if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
cb7f98ee MS |
482 | "localhost", 0, "/printers/%s", |
483 | name) < HTTP_URI_STATUS_OK) | |
ef416fc2 | 484 | { |
cb7f98ee MS |
485 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, |
486 | _("Unable to create printer-uri"), 1); | |
ef416fc2 | 487 | |
488 | return (-1); | |
489 | } | |
490 | } | |
491 | else | |
5a9febac | 492 | strlcpy(uri, "ipp://localhost/", sizeof(uri)); |
ef416fc2 | 493 | |
3d052e43 MS |
494 | if (!http) |
495 | if ((http = _cupsConnect()) == NULL) | |
496 | return (-1); | |
ef416fc2 | 497 | |
498 | /* | |
499 | * Build an IPP_GET_JOBS request, which requires the following | |
500 | * attributes: | |
501 | * | |
502 | * attributes-charset | |
503 | * attributes-natural-language | |
504 | * printer-uri | |
505 | * requesting-user-name | |
506 | * which-jobs | |
507 | * my-jobs | |
508 | * requested-attributes | |
509 | */ | |
510 | ||
cb7f98ee | 511 | request = ippNewRequest(IPP_OP_GET_JOBS); |
ef416fc2 | 512 | |
513 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
514 | "printer-uri", NULL, uri); | |
515 | ||
516 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
517 | "requesting-user-name", NULL, cupsUser()); | |
518 | ||
519 | if (myjobs) | |
520 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
521 | ||
5a738aea | 522 | if (whichjobs == CUPS_WHICHJOBS_COMPLETED) |
ef416fc2 | 523 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
524 | "which-jobs", NULL, "completed"); | |
5a738aea | 525 | else if (whichjobs == CUPS_WHICHJOBS_ALL) |
ecdc0628 | 526 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
527 | "which-jobs", NULL, "all"); | |
ef416fc2 | 528 | |
529 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
530 | "requested-attributes", sizeof(attrs) / sizeof(attrs[0]), | |
531 | NULL, attrs); | |
532 | ||
533 | /* | |
534 | * Do the request and get back a response... | |
535 | */ | |
536 | ||
537 | n = 0; | |
538 | *jobs = NULL; | |
539 | ||
540 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
541 | { | |
3d052e43 | 542 | for (attr = response->attrs; attr; attr = attr->next) |
ef416fc2 | 543 | { |
544 | /* | |
545 | * Skip leading attributes until we hit a job... | |
546 | */ | |
547 | ||
3d052e43 | 548 | while (attr && attr->group_tag != IPP_TAG_JOB) |
ef416fc2 | 549 | attr = attr->next; |
550 | ||
3d052e43 | 551 | if (!attr) |
ef416fc2 | 552 | break; |
553 | ||
554 | /* | |
555 | * Pull the needed attributes from this job... | |
556 | */ | |
557 | ||
558 | id = 0; | |
559 | size = 0; | |
560 | priority = 50; | |
cb7f98ee | 561 | state = IPP_JSTATE_PENDING; |
ef416fc2 | 562 | user = "unknown"; |
563 | dest = NULL; | |
564 | format = "application/octet-stream"; | |
565 | title = "untitled"; | |
566 | creation_time = 0; | |
567 | completed_time = 0; | |
568 | processing_time = 0; | |
569 | ||
3d052e43 | 570 | while (attr && attr->group_tag == IPP_TAG_JOB) |
ef416fc2 | 571 | { |
3d052e43 | 572 | if (!strcmp(attr->name, "job-id") && |
ef416fc2 | 573 | attr->value_tag == IPP_TAG_INTEGER) |
574 | id = attr->values[0].integer; | |
3d052e43 | 575 | else if (!strcmp(attr->name, "job-state") && |
ef416fc2 | 576 | attr->value_tag == IPP_TAG_ENUM) |
577 | state = (ipp_jstate_t)attr->values[0].integer; | |
3d052e43 | 578 | else if (!strcmp(attr->name, "job-priority") && |
ef416fc2 | 579 | attr->value_tag == IPP_TAG_INTEGER) |
580 | priority = attr->values[0].integer; | |
3d052e43 | 581 | else if (!strcmp(attr->name, "job-k-octets") && |
ef416fc2 | 582 | attr->value_tag == IPP_TAG_INTEGER) |
583 | size = attr->values[0].integer; | |
3d052e43 | 584 | else if (!strcmp(attr->name, "time-at-completed") && |
ef416fc2 | 585 | attr->value_tag == IPP_TAG_INTEGER) |
586 | completed_time = attr->values[0].integer; | |
3d052e43 | 587 | else if (!strcmp(attr->name, "time-at-creation") && |
ef416fc2 | 588 | attr->value_tag == IPP_TAG_INTEGER) |
589 | creation_time = attr->values[0].integer; | |
3d052e43 | 590 | else if (!strcmp(attr->name, "time-at-processing") && |
ef416fc2 | 591 | attr->value_tag == IPP_TAG_INTEGER) |
592 | processing_time = attr->values[0].integer; | |
3d052e43 | 593 | else if (!strcmp(attr->name, "job-printer-uri") && |
ef416fc2 | 594 | attr->value_tag == IPP_TAG_URI) |
595 | { | |
596 | if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL) | |
597 | dest ++; | |
598 | } | |
3d052e43 | 599 | else if (!strcmp(attr->name, "job-originating-user-name") && |
ef416fc2 | 600 | attr->value_tag == IPP_TAG_NAME) |
601 | user = attr->values[0].string.text; | |
3d052e43 | 602 | else if (!strcmp(attr->name, "document-format") && |
ef416fc2 | 603 | attr->value_tag == IPP_TAG_MIMETYPE) |
604 | format = attr->values[0].string.text; | |
3d052e43 | 605 | else if (!strcmp(attr->name, "job-name") && |
ef416fc2 | 606 | (attr->value_tag == IPP_TAG_TEXT || |
607 | attr->value_tag == IPP_TAG_NAME)) | |
608 | title = attr->values[0].string.text; | |
609 | ||
610 | attr = attr->next; | |
611 | } | |
612 | ||
613 | /* | |
614 | * See if we have everything needed... | |
615 | */ | |
616 | ||
3d052e43 | 617 | if (!dest || !id) |
ef416fc2 | 618 | { |
3d052e43 | 619 | if (!attr) |
ef416fc2 | 620 | break; |
621 | else | |
622 | continue; | |
623 | } | |
624 | ||
625 | /* | |
626 | * Allocate memory for the job... | |
627 | */ | |
628 | ||
629 | if (n == 0) | |
630 | temp = malloc(sizeof(cups_job_t)); | |
631 | else | |
7e86f2f6 | 632 | temp = realloc(*jobs, sizeof(cups_job_t) * (size_t)(n + 1)); |
ef416fc2 | 633 | |
3d052e43 | 634 | if (!temp) |
ef416fc2 | 635 | { |
636 | /* | |
637 | * Ran out of memory! | |
638 | */ | |
639 | ||
cb7f98ee | 640 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0); |
3d052e43 | 641 | |
ef416fc2 | 642 | cupsFreeJobs(n, *jobs); |
643 | *jobs = NULL; | |
644 | ||
645 | ippDelete(response); | |
3d052e43 MS |
646 | |
647 | return (-1); | |
ef416fc2 | 648 | } |
649 | ||
650 | *jobs = temp; | |
651 | temp += n; | |
652 | n ++; | |
653 | ||
654 | /* | |
655 | * Copy the data over... | |
656 | */ | |
657 | ||
3d052e43 MS |
658 | temp->dest = _cupsStrAlloc(dest); |
659 | temp->user = _cupsStrAlloc(user); | |
660 | temp->format = _cupsStrAlloc(format); | |
661 | temp->title = _cupsStrAlloc(title); | |
ef416fc2 | 662 | temp->id = id; |
663 | temp->priority = priority; | |
664 | temp->state = state; | |
665 | temp->size = size; | |
666 | temp->completed_time = completed_time; | |
667 | temp->creation_time = creation_time; | |
668 | temp->processing_time = processing_time; | |
669 | ||
3d052e43 | 670 | if (!attr) |
ef416fc2 | 671 | break; |
672 | } | |
673 | ||
674 | ippDelete(response); | |
675 | } | |
676 | ||
cb7f98ee | 677 | if (n == 0 && cg->last_error >= IPP_STATUS_ERROR_BAD_REQUEST) |
ef416fc2 | 678 | return (-1); |
679 | else | |
680 | return (n); | |
681 | } | |
682 | ||
683 | ||
ef416fc2 | 684 | /* |
685 | * 'cupsGetPrinters()' - Get a list of printers from the default server. | |
686 | * | |
240214ef MS |
687 | * This function is deprecated and no longer returns a list of printers - use |
688 | * @link cupsGetDests@ instead. | |
ef416fc2 | 689 | * |
690 | * @deprecated@ | |
691 | */ | |
692 | ||
693 | int /* O - Number of printers */ | |
694 | cupsGetPrinters(char ***printers) /* O - Printers */ | |
695 | { | |
240214ef MS |
696 | if (printers) |
697 | *printers = NULL; | |
ef416fc2 | 698 | |
240214ef | 699 | return (0); |
ef416fc2 | 700 | } |
701 | ||
702 | ||
ef416fc2 | 703 | /* |
704 | * 'cupsPrintFile()' - Print a file to a printer or class on the default server. | |
705 | */ | |
706 | ||
5a738aea | 707 | int /* O - Job ID or 0 on error */ |
568fa3fa | 708 | cupsPrintFile(const char *name, /* I - Destination name */ |
ef416fc2 | 709 | const char *filename, /* I - File to print */ |
710 | const char *title, /* I - Title of job */ | |
711 | int num_options,/* I - Number of options */ | |
712 | cups_option_t *options) /* I - Options */ | |
713 | { | |
714 | DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", " | |
e07d4801 | 715 | "title=\"%s\", num_options=%d, options=%p)", |
ef416fc2 | 716 | name, filename, title, num_options, options)); |
717 | ||
3d052e43 MS |
718 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title, |
719 | num_options, options)); | |
ef416fc2 | 720 | } |
721 | ||
722 | ||
723 | /* | |
5a738aea MS |
724 | * 'cupsPrintFile2()' - Print a file to a printer or class on the specified |
725 | * server. | |
ef416fc2 | 726 | * |
f3c17241 | 727 | * @since CUPS 1.1.21/OS X 10.4@ |
ef416fc2 | 728 | */ |
729 | ||
5a738aea | 730 | int /* O - Job ID or 0 on error */ |
3d052e43 | 731 | cupsPrintFile2( |
568fa3fa MS |
732 | http_t *http, /* I - Connection to server */ |
733 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
734 | const char *filename, /* I - File to print */ |
735 | const char *title, /* I - Title of job */ | |
736 | int num_options, /* I - Number of options */ | |
737 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 738 | { |
739 | DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", " | |
e07d4801 | 740 | "title=\"%s\", num_options=%d, options=%p)", |
ef416fc2 | 741 | http, name, filename, title, num_options, options)); |
742 | ||
3d052e43 MS |
743 | return (cupsPrintFiles2(http, name, 1, &filename, title, num_options, |
744 | options)); | |
ef416fc2 | 745 | } |
746 | ||
747 | ||
748 | /* | |
fa73b229 | 749 | * 'cupsPrintFiles()' - Print one or more files to a printer or class on the |
750 | * default server. | |
ef416fc2 | 751 | */ |
752 | ||
5a738aea | 753 | int /* O - Job ID or 0 on error */ |
3d052e43 | 754 | cupsPrintFiles( |
568fa3fa | 755 | const char *name, /* I - Destination name */ |
3d052e43 MS |
756 | int num_files, /* I - Number of files */ |
757 | const char **files, /* I - File(s) to print */ | |
758 | const char *title, /* I - Title of job */ | |
759 | int num_options, /* I - Number of options */ | |
760 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 761 | { |
ef416fc2 | 762 | DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, " |
e07d4801 | 763 | "files=%p, title=\"%s\", num_options=%d, options=%p)", |
ef416fc2 | 764 | name, num_files, files, title, num_options, options)); |
765 | ||
ef416fc2 | 766 | /* |
767 | * Print the file(s)... | |
768 | */ | |
769 | ||
3d052e43 | 770 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title, |
ef416fc2 | 771 | num_options, options)); |
772 | } | |
773 | ||
774 | ||
ef416fc2 | 775 | /* |
fa73b229 | 776 | * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the |
777 | * specified server. | |
ef416fc2 | 778 | * |
f3c17241 | 779 | * @since CUPS 1.1.21/OS X 10.4@ |
ef416fc2 | 780 | */ |
781 | ||
5a738aea | 782 | int /* O - Job ID or 0 on error */ |
3d052e43 | 783 | cupsPrintFiles2( |
568fa3fa MS |
784 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
785 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
786 | int num_files, /* I - Number of files */ |
787 | const char **files, /* I - File(s) to print */ | |
788 | const char *title, /* I - Title of job */ | |
789 | int num_options, /* I - Number of options */ | |
790 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 791 | { |
792 | int i; /* Looping var */ | |
3d052e43 MS |
793 | int job_id; /* New job ID */ |
794 | const char *docname; /* Basename of current filename */ | |
795 | const char *format; /* Document format */ | |
796 | cups_file_t *fp; /* Current file */ | |
797 | char buffer[8192]; /* Copy buffer */ | |
798 | ssize_t bytes; /* Bytes in buffer */ | |
799 | http_status_t status; /* Status of write */ | |
5a662dc0 MS |
800 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ |
801 | ipp_status_t cancel_status; /* Status code to preserve */ | |
802 | char *cancel_message; /* Error message to preserve */ | |
ef416fc2 | 803 | |
804 | ||
3d052e43 | 805 | DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, " |
e07d4801 | 806 | "files=%p, title=\"%s\", num_options=%d, options=%p)", |
ef416fc2 | 807 | http, name, num_files, files, title, num_options, options)); |
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 | * |
f3c17241 | 911 | * @since CUPS 1.4/OS X 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 | } |
966 | ||
967 | ||
ef416fc2 | 968 | /* |
354aadbe | 969 | * End of "$Id: util.c 13138 2016-03-15 14:59:54Z msweet $". |
ef416fc2 | 970 | */ |