]>
Commit | Line | Data |
---|---|---|
fc8c8467 | 1 | /* |
71649dc9 | 2 | * "$Id: cancel.c,v 1.33 2004/02/25 20:14:53 mike Exp $" |
fc8c8467 | 3 | * |
4 | * "cancel" command for the Common UNIX Printing System (CUPS). | |
5 | * | |
71649dc9 | 6 | * Copyright 1997-2004 by Easy Software Products. |
fc8c8467 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
8784b6a6 | 17 | * 44141 Airport View Drive, Suite 204 |
fc8c8467 | 18 | * Hollywood, Maryland 20636-3111 USA |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * main() - Parse options and cancel jobs. | |
27 | */ | |
28 | ||
29 | /* | |
30 | * Include necessary headers... | |
31 | */ | |
32 | ||
33 | #include <stdio.h> | |
34 | #include <stdlib.h> | |
b310a06a | 35 | #include <ctype.h> |
36 | ||
1c9e0181 | 37 | #include <config.h> |
fc8c8467 | 38 | #include <cups/cups.h> |
f9a8da2d | 39 | #include <cups/string.h> |
b310a06a | 40 | #include <cups/language.h> |
fc8c8467 | 41 | |
42 | ||
43 | /* | |
44 | * 'main()' - Parse options and cancel jobs. | |
45 | */ | |
46 | ||
b310a06a | 47 | int /* O - Exit status */ |
48 | main(int argc, /* I - Number of command-line arguments */ | |
49 | char *argv[]) /* I - Command-line arguments */ | |
fc8c8467 | 50 | { |
b310a06a | 51 | http_t *http; /* HTTP connection to server */ |
fc8c8467 | 52 | int i; /* Looping var */ |
53 | int job_id; /* Job ID */ | |
4ae5a89f | 54 | int num_dests; /* Number of destinations */ |
55 | cups_dest_t *dests; /* Destinations */ | |
d7a02a53 | 56 | char *dest, /* Destination printer */ |
e3b16b79 | 57 | *job, /* Job ID pointer */ |
58 | *user; /* Cancel jobs for a user */ | |
59 | int purge; /* Purge or cancel jobs? */ | |
b310a06a | 60 | char uri[1024]; /* Printer or job URI */ |
61 | ipp_t *request; /* IPP request */ | |
62 | ipp_t *response; /* IPP response */ | |
63 | ipp_op_t op; /* Operation */ | |
64 | cups_lang_t *language; /* Language */ | |
fc8c8467 | 65 | |
fc8c8467 | 66 | |
b310a06a | 67 | /* |
68 | * Setup to cancel individual print jobs... | |
69 | */ | |
fc8c8467 | 70 | |
1c9e0181 | 71 | op = IPP_CANCEL_JOB; |
e3b16b79 | 72 | purge = 0; |
1c9e0181 | 73 | job_id = 0; |
74 | dest = NULL; | |
e3b16b79 | 75 | user = NULL; |
1c9e0181 | 76 | http = NULL; |
4ae5a89f | 77 | num_dests = 0; |
78 | dests = NULL; | |
fc8c8467 | 79 | |
b310a06a | 80 | /* |
81 | * Process command-line arguments... | |
82 | */ | |
fc8c8467 | 83 | |
b310a06a | 84 | for (i = 1; i < argc; i ++) |
50b9556e | 85 | if (argv[i][0] == '-' && argv[i][1]) |
b310a06a | 86 | switch (argv[i][1]) |
87 | { | |
1c9e0181 | 88 | case 'E' : /* Encrypt */ |
2dac9564 | 89 | #ifdef HAVE_SSL |
3c112497 | 90 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); |
1c9e0181 | 91 | |
92 | if (http) | |
3c112497 | 93 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); |
1c9e0181 | 94 | #else |
95 | fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n", | |
96 | argv[0]); | |
2dac9564 | 97 | #endif /* HAVE_SSL */ |
1c9e0181 | 98 | break; |
99 | ||
b310a06a | 100 | case 'a' : /* Cancel all jobs */ |
e3b16b79 | 101 | purge = 1; |
102 | op = IPP_PURGE_JOBS; | |
fc8c8467 | 103 | break; |
104 | ||
808dd474 | 105 | case 'h' : /* Connect to host */ |
8dbf3e6a | 106 | if (http != NULL) |
107 | httpClose(http); | |
808dd474 | 108 | |
109 | if (argv[i][2] != '\0') | |
19293ea1 | 110 | cupsSetServer(argv[i] + 2); |
808dd474 | 111 | else |
112 | { | |
113 | i ++; | |
8a4fe7c7 | 114 | |
115 | if (i >= argc) | |
116 | { | |
2558a535 | 117 | fputs("cancel: Error - expected hostname after \'-h\' option!\n", stderr); |
8a4fe7c7 | 118 | return (1); |
119 | } | |
120 | else | |
19293ea1 | 121 | cupsSetServer(argv[i]); |
808dd474 | 122 | } |
123 | break; | |
124 | ||
2558a535 | 125 | case 'u' : /* Username */ |
e3b16b79 | 126 | op = IPP_PURGE_JOBS; |
127 | ||
2558a535 | 128 | if (argv[i][2] != '\0') |
e3b16b79 | 129 | user = argv[i] + 2; |
2558a535 | 130 | else |
131 | { | |
132 | i ++; | |
133 | ||
134 | if (i >= argc) | |
135 | { | |
136 | fputs("cancel: Error - expected username after \'-u\' option!\n", stderr); | |
137 | return (1); | |
138 | } | |
139 | else | |
e3b16b79 | 140 | user = argv[i]; |
2558a535 | 141 | } |
142 | break; | |
143 | ||
fc8c8467 | 144 | default : |
b310a06a | 145 | fprintf(stderr, "cancel: Unknown option \'%c\'!\n", argv[i][1]); |
fc8c8467 | 146 | return (1); |
147 | } | |
b310a06a | 148 | else |
fc8c8467 | 149 | { |
b310a06a | 150 | /* |
151 | * Cancel a job or printer... | |
152 | */ | |
fc8c8467 | 153 | |
4ae5a89f | 154 | if (num_dests == 0) |
155 | num_dests = cupsGetDests(&dests); | |
156 | ||
19293ea1 | 157 | if (strcmp(argv[i], "-") == 0) |
50b9556e | 158 | { |
19293ea1 | 159 | /* |
160 | * Delete the current job... | |
161 | */ | |
162 | ||
50b9556e | 163 | dest = ""; |
164 | job_id = 0; | |
165 | } | |
19293ea1 | 166 | else if (cupsGetDest(argv[i], NULL, num_dests, dests) != NULL) |
b310a06a | 167 | { |
19293ea1 | 168 | /* |
169 | * Delete the current job on the named destination... | |
170 | */ | |
50b9556e | 171 | |
19293ea1 | 172 | dest = argv[i]; |
173 | job_id = 0; | |
174 | } | |
27366073 | 175 | else if ((job = strrchr(argv[i], '-')) != NULL && isdigit(job[1] & 255)) |
19293ea1 | 176 | { |
177 | /* | |
178 | * Delete the specified job ID. | |
179 | */ | |
50b9556e | 180 | |
19293ea1 | 181 | dest = NULL; |
182 | op = IPP_CANCEL_JOB; | |
183 | job_id = atoi(job + 1); | |
184 | } | |
27366073 | 185 | else if (isdigit(argv[i][0] & 255)) |
19293ea1 | 186 | { |
187 | /* | |
188 | * Delete the specified job ID. | |
189 | */ | |
808dd474 | 190 | |
19293ea1 | 191 | dest = NULL; |
192 | op = IPP_CANCEL_JOB; | |
193 | job_id = atoi(argv[i]); | |
194 | } | |
195 | else | |
196 | { | |
197 | /* | |
198 | * Bad printer name! | |
199 | */ | |
808dd474 | 200 | |
19293ea1 | 201 | fprintf(stderr, "cancel: Unknown destination \"%s\"!\n", argv[i]); |
504a6b20 | 202 | return (1); |
19293ea1 | 203 | } |
808dd474 | 204 | |
19293ea1 | 205 | /* |
206 | * For Solaris LP compatibility, ignore a destination name after | |
207 | * cancelling a specific job ID... | |
208 | */ | |
808dd474 | 209 | |
19293ea1 | 210 | if (job_id && (i + 1) < argc && |
211 | cupsGetDest(argv[i + 1], NULL, num_dests, dests) != NULL) | |
212 | i ++; | |
fc8c8467 | 213 | |
8dbf3e6a | 214 | /* |
215 | * Open a connection to the server... | |
216 | */ | |
217 | ||
218 | if (http == NULL) | |
a1793153 | 219 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), |
3c112497 | 220 | cupsEncryption())) == NULL) |
8dbf3e6a | 221 | { |
222 | fputs("cancel: Unable to contact server!\n", stderr); | |
223 | return (1); | |
224 | } | |
225 | ||
b310a06a | 226 | /* |
227 | * Build an IPP request, which requires the following | |
228 | * attributes: | |
229 | * | |
230 | * attributes-charset | |
231 | * attributes-natural-language | |
232 | * printer-uri + job-id *or* job-uri | |
8dbf3e6a | 233 | * [requesting-user-name] |
b310a06a | 234 | */ |
fc8c8467 | 235 | |
b310a06a | 236 | request = ippNew(); |
fc8c8467 | 237 | |
b310a06a | 238 | request->request.op.operation_id = op; |
239 | request->request.op.request_id = 1; | |
fc8c8467 | 240 | |
b310a06a | 241 | language = cupsLangDefault(); |
fc8c8467 | 242 | |
b310a06a | 243 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
244 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
fc8c8467 | 245 | |
b310a06a | 246 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, |
247 | "attributes-natural-language", NULL, language->language); | |
fc8c8467 | 248 | |
b310a06a | 249 | if (dest) |
250 | { | |
c5808f70 | 251 | snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", dest); |
b310a06a | 252 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
253 | "printer-uri", NULL, uri); | |
254 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
255 | job_id); | |
256 | } | |
257 | else | |
258 | { | |
1fd07458 | 259 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); |
b310a06a | 260 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, |
261 | uri); | |
262 | } | |
fc8c8467 | 263 | |
e3b16b79 | 264 | if (user) |
265 | { | |
266 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
267 | "requesting-user-name", NULL, user); | |
268 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
269 | } | |
270 | else | |
271 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
272 | "requesting-user-name", NULL, cupsUser()); | |
273 | ||
274 | if (op == IPP_PURGE_JOBS) | |
275 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge); | |
8dbf3e6a | 276 | |
b310a06a | 277 | /* |
278 | * Do the request and get back a response... | |
279 | */ | |
fc8c8467 | 280 | |
b310a06a | 281 | if (op == IPP_PURGE_JOBS) |
282 | response = cupsDoRequest(http, request, "/admin/"); | |
283 | else | |
284 | response = cupsDoRequest(http, request, "/jobs/"); | |
fc8c8467 | 285 | |
96ecd6f0 | 286 | if (response == NULL || |
287 | response->request.status.status_code > IPP_OK_CONFLICT) | |
8cf354e9 | 288 | { |
96ecd6f0 | 289 | fprintf(stderr, "cancel: %s failed: %s\n", |
290 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", | |
291 | response ? ippErrorString(response->request.status.status_code) : | |
292 | ippErrorString(cupsLastError())); | |
293 | ||
294 | if (response) | |
295 | ippDelete(response); | |
8cf354e9 | 296 | |
b310a06a | 297 | return (1); |
298 | } | |
96ecd6f0 | 299 | |
300 | ippDelete(response); | |
fc8c8467 | 301 | } |
fc8c8467 | 302 | |
ddd39388 | 303 | if (num_dests == 0 && op == IPP_PURGE_JOBS) |
304 | { | |
305 | /* | |
306 | * Open a connection to the server... | |
307 | */ | |
308 | ||
309 | if (http == NULL) | |
310 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
311 | cupsEncryption())) == NULL) | |
312 | { | |
313 | fputs("cancel: Unable to contact server!\n", stderr); | |
314 | return (1); | |
315 | } | |
316 | ||
317 | /* | |
318 | * Build an IPP request, which requires the following | |
319 | * attributes: | |
320 | * | |
321 | * attributes-charset | |
322 | * attributes-natural-language | |
323 | * printer-uri + job-id *or* job-uri | |
324 | * [requesting-user-name] | |
325 | */ | |
326 | ||
327 | request = ippNew(); | |
328 | ||
329 | request->request.op.operation_id = op; | |
330 | request->request.op.request_id = 1; | |
331 | ||
332 | language = cupsLangDefault(); | |
333 | ||
334 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
335 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
336 | ||
337 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
338 | "attributes-natural-language", NULL, language->language); | |
339 | ||
340 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
341 | "printer-uri", NULL, "ipp://localhost/printers/"); | |
342 | ||
343 | if (user) | |
344 | { | |
345 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
346 | "requesting-user-name", NULL, user); | |
347 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
348 | } | |
349 | else | |
350 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
351 | "requesting-user-name", NULL, cupsUser()); | |
352 | ||
353 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge); | |
354 | ||
355 | /* | |
356 | * Do the request and get back a response... | |
357 | */ | |
358 | ||
359 | response = cupsDoRequest(http, request, "/admin/"); | |
360 | ||
361 | if (response == NULL || | |
362 | response->request.status.status_code > IPP_OK_CONFLICT) | |
363 | { | |
364 | fprintf(stderr, "cancel: %s failed: %s\n", | |
365 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", | |
366 | response ? ippErrorString(response->request.status.status_code) : | |
367 | ippErrorString(cupsLastError())); | |
368 | ||
369 | if (response) | |
370 | ippDelete(response); | |
371 | ||
372 | return (1); | |
373 | } | |
374 | ||
375 | ippDelete(response); | |
376 | } | |
377 | ||
fc8c8467 | 378 | return (0); |
379 | } | |
380 | ||
381 | ||
382 | /* | |
71649dc9 | 383 | * End of "$Id: cancel.c,v 1.33 2004/02/25 20:14:53 mike Exp $". |
fc8c8467 | 384 | */ |