]>
Commit | Line | Data |
---|---|---|
fc8c8467 | 1 | /* |
c9d3f842 | 2 | * "$Id$" |
fc8c8467 | 3 | * |
3d94661a | 4 | * "cancel" command for CUPS. |
fc8c8467 | 5 | * |
f0b48aa2 | 6 | * Copyright 2007-2011 by Apple Inc. |
0e66904d | 7 | * Copyright 1997-2006 by Easy Software Products. |
fc8c8467 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
4e8d321f | 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/". | |
fc8c8467 | 14 | * |
15 | * Contents: | |
16 | * | |
17 | * main() - Parse options and cancel jobs. | |
18 | */ | |
19 | ||
20 | /* | |
21 | * Include necessary headers... | |
22 | */ | |
23 | ||
3d94661a | 24 | #include <cups/cups-private.h> |
fc8c8467 | 25 | |
26 | ||
27 | /* | |
28 | * 'main()' - Parse options and cancel jobs. | |
29 | */ | |
30 | ||
b7382443 | 31 | int /* O - Exit status */ |
32 | main(int argc, /* I - Number of command-line arguments */ | |
33 | char *argv[]) /* I - Command-line arguments */ | |
fc8c8467 | 34 | { |
b7382443 | 35 | http_t *http; /* HTTP connection to server */ |
36 | int i; /* Looping var */ | |
37 | int job_id; /* Job ID */ | |
38 | int num_dests; /* Number of destinations */ | |
39 | cups_dest_t *dests; /* Destinations */ | |
40 | char *dest, /* Destination printer */ | |
41 | *job, /* Job ID pointer */ | |
42 | *user; /* Cancel jobs for a user */ | |
43 | int purge; /* Purge or cancel jobs? */ | |
44 | char uri[1024]; /* Printer or job URI */ | |
45 | ipp_t *request; /* IPP request */ | |
46 | ipp_t *response; /* IPP response */ | |
47 | ipp_op_t op; /* Operation */ | |
fc8c8467 | 48 | |
fc8c8467 | 49 | |
156dd8aa | 50 | _cupsSetLocale(argv); |
ebad28ad | 51 | |
b310a06a | 52 | /* |
53 | * Setup to cancel individual print jobs... | |
54 | */ | |
fc8c8467 | 55 | |
00f3aaf5 | 56 | op = IPP_CANCEL_JOB; |
57 | purge = 0; | |
00f3aaf5 | 58 | dest = NULL; |
59 | user = NULL; | |
60 | http = NULL; | |
61 | num_dests = 0; | |
62 | dests = NULL; | |
b7382443 | 63 | |
fc8c8467 | 64 | |
b310a06a | 65 | /* |
66 | * Process command-line arguments... | |
67 | */ | |
fc8c8467 | 68 | |
b310a06a | 69 | for (i = 1; i < argc; i ++) |
50b9556e | 70 | if (argv[i][0] == '-' && argv[i][1]) |
b310a06a | 71 | switch (argv[i][1]) |
72 | { | |
1c9e0181 | 73 | case 'E' : /* Encrypt */ |
bcf61448 | 74 | #ifdef HAVE_SSL |
192a8a08 | 75 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); |
1c9e0181 | 76 | |
77 | if (http) | |
192a8a08 | 78 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); |
1c9e0181 | 79 | #else |
89fd567e | 80 | _cupsLangPrintf(stderr, |
4c4eea89 | 81 | _("%s: Sorry, no encryption support."), argv[0]); |
bcf61448 | 82 | #endif /* HAVE_SSL */ |
1c9e0181 | 83 | break; |
84 | ||
cfea7a92 | 85 | case 'U' : /* Username */ |
86 | if (argv[i][2] != '\0') | |
87 | cupsSetUser(argv[i] + 2); | |
88 | else | |
89 | { | |
90 | i ++; | |
91 | if (i >= argc) | |
92 | { | |
93 | _cupsLangPrintf(stderr, | |
94 | _("%s: Error - expected username after " | |
4c4eea89 | 95 | "\"-U\" option."), argv[0]); |
cfea7a92 | 96 | return (1); |
97 | } | |
98 | ||
99 | cupsSetUser(argv[i]); | |
100 | } | |
101 | break; | |
c6fab96f | 102 | |
b310a06a | 103 | case 'a' : /* Cancel all jobs */ |
dd9e85de | 104 | purge = 1; |
105 | op = IPP_PURGE_JOBS; | |
fc8c8467 | 106 | break; |
107 | ||
808dd474 | 108 | case 'h' : /* Connect to host */ |
8dbf3e6a | 109 | if (http != NULL) |
6cf8f89f | 110 | { |
8dbf3e6a | 111 | httpClose(http); |
6cf8f89f | 112 | http = NULL; |
113 | } | |
808dd474 | 114 | |
115 | if (argv[i][2] != '\0') | |
5ae53c1e | 116 | cupsSetServer(argv[i] + 2); |
808dd474 | 117 | else |
118 | { | |
119 | i ++; | |
8a4fe7c7 | 120 | |
121 | if (i >= argc) | |
122 | { | |
cfea7a92 | 123 | _cupsLangPrintf(stderr, |
124 | _("%s: Error - expected hostname after " | |
4c4eea89 | 125 | "\"-h\" option."), argv[0]); |
8a4fe7c7 | 126 | return (1); |
127 | } | |
128 | else | |
5ae53c1e | 129 | cupsSetServer(argv[i]); |
808dd474 | 130 | } |
131 | break; | |
132 | ||
2558a535 | 133 | case 'u' : /* Username */ |
dd9e85de | 134 | op = IPP_PURGE_JOBS; |
135 | ||
2558a535 | 136 | if (argv[i][2] != '\0') |
dd9e85de | 137 | user = argv[i] + 2; |
2558a535 | 138 | else |
139 | { | |
140 | i ++; | |
141 | ||
142 | if (i >= argc) | |
143 | { | |
cfea7a92 | 144 | _cupsLangPrintf(stderr, |
145 | _("%s: Error - expected username after " | |
4c4eea89 | 146 | "\"-u\" option."), argv[0]); |
2558a535 | 147 | return (1); |
148 | } | |
149 | else | |
dd9e85de | 150 | user = argv[i]; |
2558a535 | 151 | } |
152 | break; | |
153 | ||
fc8c8467 | 154 | default : |
89fd567e | 155 | _cupsLangPrintf(stderr, |
4c4eea89 | 156 | _("%s: Error - unknown option \"%c\"."), |
cfea7a92 | 157 | argv[0], argv[i][1]); |
fc8c8467 | 158 | return (1); |
159 | } | |
b310a06a | 160 | else |
fc8c8467 | 161 | { |
b310a06a | 162 | /* |
163 | * Cancel a job or printer... | |
164 | */ | |
fc8c8467 | 165 | |
aa37e40e | 166 | if (num_dests == 0) |
167 | num_dests = cupsGetDests(&dests); | |
168 | ||
398fa469 | 169 | if (!strcmp(argv[i], "-")) |
50b9556e | 170 | { |
5ae53c1e | 171 | /* |
172 | * Delete the current job... | |
173 | */ | |
174 | ||
50b9556e | 175 | dest = ""; |
176 | job_id = 0; | |
177 | } | |
5ae53c1e | 178 | else if (cupsGetDest(argv[i], NULL, num_dests, dests) != NULL) |
b310a06a | 179 | { |
5ae53c1e | 180 | /* |
181 | * Delete the current job on the named destination... | |
182 | */ | |
50b9556e | 183 | |
5ae53c1e | 184 | dest = argv[i]; |
185 | job_id = 0; | |
186 | } | |
64bbab09 | 187 | else if ((job = strrchr(argv[i], '-')) != NULL && isdigit(job[1] & 255)) |
5ae53c1e | 188 | { |
189 | /* | |
190 | * Delete the specified job ID. | |
191 | */ | |
50b9556e | 192 | |
5ae53c1e | 193 | dest = NULL; |
194 | op = IPP_CANCEL_JOB; | |
195 | job_id = atoi(job + 1); | |
196 | } | |
64bbab09 | 197 | else if (isdigit(argv[i][0] & 255)) |
5ae53c1e | 198 | { |
199 | /* | |
200 | * Delete the specified job ID. | |
201 | */ | |
808dd474 | 202 | |
5ae53c1e | 203 | dest = NULL; |
204 | op = IPP_CANCEL_JOB; | |
205 | job_id = atoi(argv[i]); | |
206 | } | |
207 | else | |
208 | { | |
209 | /* | |
210 | * Bad printer name! | |
211 | */ | |
808dd474 | 212 | |
89fd567e | 213 | _cupsLangPrintf(stderr, |
4c4eea89 | 214 | _("%s: Error - unknown destination \"%s\"."), |
cfea7a92 | 215 | argv[0], argv[i]); |
727e6d14 | 216 | return (1); |
5ae53c1e | 217 | } |
808dd474 | 218 | |
5ae53c1e | 219 | /* |
220 | * For Solaris LP compatibility, ignore a destination name after | |
221 | * cancelling a specific job ID... | |
222 | */ | |
808dd474 | 223 | |
5ae53c1e | 224 | if (job_id && (i + 1) < argc && |
225 | cupsGetDest(argv[i + 1], NULL, num_dests, dests) != NULL) | |
226 | i ++; | |
fc8c8467 | 227 | |
8dbf3e6a | 228 | /* |
229 | * Open a connection to the server... | |
230 | */ | |
231 | ||
232 | if (http == NULL) | |
b5cb0608 | 233 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), |
192a8a08 | 234 | cupsEncryption())) == NULL) |
8dbf3e6a | 235 | { |
cfea7a92 | 236 | _cupsLangPrintf(stderr, |
f0b48aa2 | 237 | _("%s: Unable to connect to server."), argv[0]); |
8dbf3e6a | 238 | return (1); |
239 | } | |
240 | ||
b310a06a | 241 | /* |
242 | * Build an IPP request, which requires the following | |
243 | * attributes: | |
244 | * | |
245 | * attributes-charset | |
246 | * attributes-natural-language | |
247 | * printer-uri + job-id *or* job-uri | |
8dbf3e6a | 248 | * [requesting-user-name] |
b310a06a | 249 | */ |
fc8c8467 | 250 | |
00f3aaf5 | 251 | request = ippNewRequest(op); |
fc8c8467 | 252 | |
b310a06a | 253 | if (dest) |
254 | { | |
00a1fad8 | 255 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
256 | "localhost", 0, "/printers/%s", dest); | |
b310a06a | 257 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
258 | "printer-uri", NULL, uri); | |
259 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
260 | job_id); | |
261 | } | |
262 | else | |
263 | { | |
1fd07458 | 264 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); |
b310a06a | 265 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, |
266 | uri); | |
267 | } | |
fc8c8467 | 268 | |
dd9e85de | 269 | if (user) |
270 | { | |
271 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
272 | "requesting-user-name", NULL, user); | |
273 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
274 | } | |
275 | else | |
276 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
277 | "requesting-user-name", NULL, cupsUser()); | |
278 | ||
279 | if (op == IPP_PURGE_JOBS) | |
280 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge); | |
8dbf3e6a | 281 | |
b310a06a | 282 | /* |
283 | * Do the request and get back a response... | |
284 | */ | |
fc8c8467 | 285 | |
c6fab96f | 286 | if (op == IPP_PURGE_JOBS && (!user || _cups_strcasecmp(user, cupsUser()))) |
b310a06a | 287 | response = cupsDoRequest(http, request, "/admin/"); |
288 | else | |
289 | response = cupsDoRequest(http, request, "/jobs/"); | |
fc8c8467 | 290 | |
96ecd6f0 | 291 | if (response == NULL || |
0a3ac972 | 292 | response->request.status.status_code > IPP_OK_CONFLICT) |
8cf354e9 | 293 | { |
4c4eea89 | 294 | _cupsLangPrintf(stderr, _("%s: %s failed: %s"), argv[0], |
b7382443 | 295 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", |
398fa469 | 296 | cupsLastErrorString()); |
96ecd6f0 | 297 | |
298 | if (response) | |
299 | ippDelete(response); | |
8cf354e9 | 300 | |
b310a06a | 301 | return (1); |
302 | } | |
96ecd6f0 | 303 | |
304 | ippDelete(response); | |
fc8c8467 | 305 | } |
fc8c8467 | 306 | |
b07e0cc2 | 307 | if (num_dests == 0 && op == IPP_PURGE_JOBS) |
308 | { | |
309 | /* | |
310 | * Open a connection to the server... | |
311 | */ | |
312 | ||
313 | if (http == NULL) | |
314 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
315 | cupsEncryption())) == NULL) | |
316 | { | |
4c4eea89 | 317 | _cupsLangPrintf(stderr, _("%s: Unable to contact server."), argv[0]); |
b07e0cc2 | 318 | return (1); |
319 | } | |
320 | ||
321 | /* | |
322 | * Build an IPP request, which requires the following | |
323 | * attributes: | |
324 | * | |
325 | * attributes-charset | |
326 | * attributes-natural-language | |
327 | * printer-uri + job-id *or* job-uri | |
328 | * [requesting-user-name] | |
329 | */ | |
330 | ||
00f3aaf5 | 331 | request = ippNewRequest(op); |
b07e0cc2 | 332 | |
333 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
334 | "printer-uri", NULL, "ipp://localhost/printers/"); | |
335 | ||
336 | if (user) | |
337 | { | |
338 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
339 | "requesting-user-name", NULL, user); | |
340 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
341 | } | |
342 | else | |
343 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
344 | "requesting-user-name", NULL, cupsUser()); | |
345 | ||
346 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge); | |
347 | ||
348 | /* | |
349 | * Do the request and get back a response... | |
350 | */ | |
351 | ||
352 | response = cupsDoRequest(http, request, "/admin/"); | |
353 | ||
354 | if (response == NULL || | |
355 | response->request.status.status_code > IPP_OK_CONFLICT) | |
356 | { | |
4c4eea89 | 357 | _cupsLangPrintf(stderr, _("%s: %s failed: %s"), argv[0], |
b7382443 | 358 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", |
00f3aaf5 | 359 | cupsLastErrorString()); |
b07e0cc2 | 360 | |
361 | if (response) | |
362 | ippDelete(response); | |
363 | ||
364 | return (1); | |
365 | } | |
366 | ||
367 | ippDelete(response); | |
368 | } | |
369 | ||
fc8c8467 | 370 | return (0); |
371 | } | |
372 | ||
373 | ||
374 | /* | |
c9d3f842 | 375 | * End of "$Id$". |
fc8c8467 | 376 | */ |