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