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