]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
a4d04587 | 2 | * "$Id: cancel.c 5023 2006-01-29 14:39:44Z 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) | |
124 | httpClose(http); | |
125 | ||
126 | if (argv[i][2] != '\0') | |
127 | cupsSetServer(argv[i] + 2); | |
128 | else | |
129 | { | |
130 | i ++; | |
131 | ||
132 | if (i >= argc) | |
133 | { | |
fa73b229 | 134 | _cupsLangPrintf(stderr, |
135 | _("%s: Error - expected hostname after " | |
136 | "\'-h\' option!\n"), | |
137 | argv[0]); | |
ef416fc2 | 138 | return (1); |
139 | } | |
140 | else | |
141 | cupsSetServer(argv[i]); | |
142 | } | |
143 | break; | |
144 | ||
145 | case 'u' : /* Username */ | |
146 | op = IPP_PURGE_JOBS; | |
147 | ||
148 | if (argv[i][2] != '\0') | |
149 | user = argv[i] + 2; | |
150 | else | |
151 | { | |
152 | i ++; | |
153 | ||
154 | if (i >= argc) | |
155 | { | |
fa73b229 | 156 | _cupsLangPrintf(stderr, |
157 | _("%s: Error - expected username after " | |
158 | "\'-u\' option!\n"), | |
159 | argv[0]); | |
ef416fc2 | 160 | return (1); |
161 | } | |
162 | else | |
163 | user = argv[i]; | |
164 | } | |
165 | break; | |
166 | ||
167 | default : | |
fa73b229 | 168 | _cupsLangPrintf(stderr, |
169 | _("%s: Error - unknown option \'%c\'!\n"), | |
170 | argv[0], argv[i][1]); | |
ef416fc2 | 171 | return (1); |
172 | } | |
173 | else | |
174 | { | |
175 | /* | |
176 | * Cancel a job or printer... | |
177 | */ | |
178 | ||
179 | if (num_dests == 0) | |
180 | num_dests = cupsGetDests(&dests); | |
181 | ||
182 | if (strcmp(argv[i], "-") == 0) | |
183 | { | |
184 | /* | |
185 | * Delete the current job... | |
186 | */ | |
187 | ||
188 | dest = ""; | |
189 | job_id = 0; | |
190 | } | |
191 | else if (cupsGetDest(argv[i], NULL, num_dests, dests) != NULL) | |
192 | { | |
193 | /* | |
194 | * Delete the current job on the named destination... | |
195 | */ | |
196 | ||
197 | dest = argv[i]; | |
198 | job_id = 0; | |
199 | } | |
200 | else if ((job = strrchr(argv[i], '-')) != NULL && isdigit(job[1] & 255)) | |
201 | { | |
202 | /* | |
203 | * Delete the specified job ID. | |
204 | */ | |
205 | ||
206 | dest = NULL; | |
207 | op = IPP_CANCEL_JOB; | |
208 | job_id = atoi(job + 1); | |
209 | } | |
210 | else if (isdigit(argv[i][0] & 255)) | |
211 | { | |
212 | /* | |
213 | * Delete the specified job ID. | |
214 | */ | |
215 | ||
216 | dest = NULL; | |
217 | op = IPP_CANCEL_JOB; | |
218 | job_id = atoi(argv[i]); | |
219 | } | |
220 | else | |
221 | { | |
222 | /* | |
223 | * Bad printer name! | |
224 | */ | |
225 | ||
fa73b229 | 226 | _cupsLangPrintf(stderr, |
227 | _("%s: Error - unknown destination \"%s\"!\n"), | |
228 | argv[0], argv[i]); | |
ef416fc2 | 229 | return (1); |
230 | } | |
231 | ||
232 | /* | |
233 | * For Solaris LP compatibility, ignore a destination name after | |
234 | * cancelling a specific job ID... | |
235 | */ | |
236 | ||
237 | if (job_id && (i + 1) < argc && | |
238 | cupsGetDest(argv[i + 1], NULL, num_dests, dests) != NULL) | |
239 | i ++; | |
240 | ||
241 | /* | |
242 | * Open a connection to the server... | |
243 | */ | |
244 | ||
245 | if (http == NULL) | |
246 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
247 | cupsEncryption())) == NULL) | |
248 | { | |
fa73b229 | 249 | _cupsLangPrintf(stderr, |
250 | _("%s: Unable to contact server!\n"), | |
251 | argv[0]); | |
ef416fc2 | 252 | return (1); |
253 | } | |
254 | ||
255 | /* | |
256 | * Build an IPP request, which requires the following | |
257 | * attributes: | |
258 | * | |
259 | * attributes-charset | |
260 | * attributes-natural-language | |
261 | * printer-uri + job-id *or* job-uri | |
262 | * [requesting-user-name] | |
263 | */ | |
264 | ||
fa73b229 | 265 | request = ippNewRequest(op); |
ef416fc2 | 266 | |
267 | if (dest) | |
268 | { | |
a4d04587 | 269 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
270 | "localhost", 0, "/printers/%s", dest); | |
ef416fc2 | 271 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
272 | "printer-uri", NULL, uri); | |
273 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
274 | job_id); | |
275 | } | |
276 | else | |
277 | { | |
278 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); | |
279 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, | |
280 | uri); | |
281 | } | |
282 | ||
283 | if (user) | |
284 | { | |
285 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
286 | "requesting-user-name", NULL, user); | |
287 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
288 | } | |
289 | else | |
290 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
291 | "requesting-user-name", NULL, cupsUser()); | |
292 | ||
293 | if (op == IPP_PURGE_JOBS) | |
294 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge); | |
295 | ||
296 | /* | |
297 | * Do the request and get back a response... | |
298 | */ | |
299 | ||
300 | if (op == IPP_PURGE_JOBS && (!user || strcasecmp(user, cupsUser()))) | |
301 | response = cupsDoRequest(http, request, "/admin/"); | |
302 | else | |
303 | response = cupsDoRequest(http, request, "/jobs/"); | |
304 | ||
305 | if (response == NULL || | |
306 | response->request.status.status_code > IPP_OK_CONFLICT) | |
307 | { | |
fa73b229 | 308 | _cupsLangPrintf(stderr, _("%s: %s failed: %s\n"), argv[0], |
ef416fc2 | 309 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", |
310 | response ? ippErrorString(response->request.status.status_code) : | |
311 | ippErrorString(cupsLastError())); | |
312 | ||
313 | if (response) | |
314 | ippDelete(response); | |
315 | ||
316 | return (1); | |
317 | } | |
318 | ||
319 | ippDelete(response); | |
320 | } | |
321 | ||
322 | if (num_dests == 0 && op == IPP_PURGE_JOBS) | |
323 | { | |
324 | /* | |
325 | * Open a connection to the server... | |
326 | */ | |
327 | ||
328 | if (http == NULL) | |
329 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
330 | cupsEncryption())) == NULL) | |
331 | { | |
fa73b229 | 332 | _cupsLangPrintf(stderr, _("%s: Unable to contact server!\n"), |
333 | argv[0]); | |
ef416fc2 | 334 | return (1); |
335 | } | |
336 | ||
337 | /* | |
338 | * Build an IPP request, which requires the following | |
339 | * attributes: | |
340 | * | |
341 | * attributes-charset | |
342 | * attributes-natural-language | |
343 | * printer-uri + job-id *or* job-uri | |
344 | * [requesting-user-name] | |
345 | */ | |
346 | ||
fa73b229 | 347 | request = ippNewRequest(op); |
ef416fc2 | 348 | |
349 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
350 | "printer-uri", NULL, "ipp://localhost/printers/"); | |
351 | ||
352 | if (user) | |
353 | { | |
354 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
355 | "requesting-user-name", NULL, user); | |
356 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
357 | } | |
358 | else | |
359 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
360 | "requesting-user-name", NULL, cupsUser()); | |
361 | ||
362 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge); | |
363 | ||
364 | /* | |
365 | * Do the request and get back a response... | |
366 | */ | |
367 | ||
368 | response = cupsDoRequest(http, request, "/admin/"); | |
369 | ||
370 | if (response == NULL || | |
371 | response->request.status.status_code > IPP_OK_CONFLICT) | |
372 | { | |
fa73b229 | 373 | _cupsLangPrintf(stderr, _("%s: %s failed: %s\n"), argv[0], |
ef416fc2 | 374 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", |
fa73b229 | 375 | cupsLastErrorString()); |
ef416fc2 | 376 | |
377 | if (response) | |
378 | ippDelete(response); | |
379 | ||
380 | return (1); | |
381 | } | |
382 | ||
383 | ippDelete(response); | |
384 | } | |
385 | ||
386 | return (0); | |
387 | } | |
388 | ||
389 | ||
390 | /* | |
a4d04587 | 391 | * End of "$Id: cancel.c 5023 2006-01-29 14:39:44Z mike $". |
ef416fc2 | 392 | */ |