]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id$" | |
3 | * | |
4 | * "cancel" command for CUPS. | |
5 | * | |
6 | * Copyright 2007-2011 by Apple Inc. | |
7 | * Copyright 1997-2006 by Easy Software Products. | |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
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/". | |
14 | * | |
15 | * Contents: | |
16 | * | |
17 | * main() - Parse options and cancel jobs. | |
18 | */ | |
19 | ||
20 | /* | |
21 | * Include necessary headers... | |
22 | */ | |
23 | ||
24 | #include <cups/cups-private.h> | |
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 */ | |
48 | ||
49 | ||
50 | _cupsSetLocale(argv); | |
51 | ||
52 | /* | |
53 | * Setup to cancel individual print jobs... | |
54 | */ | |
55 | ||
56 | op = IPP_CANCEL_JOB; | |
57 | purge = 0; | |
58 | dest = NULL; | |
59 | user = NULL; | |
60 | http = NULL; | |
61 | num_dests = 0; | |
62 | dests = NULL; | |
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 | |
80 | _cupsLangPrintf(stderr, | |
81 | _("%s: Sorry, no encryption support."), argv[0]); | |
82 | #endif /* HAVE_SSL */ | |
83 | break; | |
84 | ||
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 " | |
95 | "\"-U\" option."), argv[0]); | |
96 | return (1); | |
97 | } | |
98 | ||
99 | cupsSetUser(argv[i]); | |
100 | } | |
101 | break; | |
102 | ||
103 | case 'a' : /* Cancel all jobs */ | |
104 | purge = 1; | |
105 | op = IPP_PURGE_JOBS; | |
106 | break; | |
107 | ||
108 | case 'h' : /* Connect to host */ | |
109 | if (http != NULL) | |
110 | { | |
111 | httpClose(http); | |
112 | http = NULL; | |
113 | } | |
114 | ||
115 | if (argv[i][2] != '\0') | |
116 | cupsSetServer(argv[i] + 2); | |
117 | else | |
118 | { | |
119 | i ++; | |
120 | ||
121 | if (i >= argc) | |
122 | { | |
123 | _cupsLangPrintf(stderr, | |
124 | _("%s: Error - expected hostname after " | |
125 | "\"-h\" option."), argv[0]); | |
126 | return (1); | |
127 | } | |
128 | else | |
129 | cupsSetServer(argv[i]); | |
130 | } | |
131 | break; | |
132 | ||
133 | case 'u' : /* Username */ | |
134 | op = IPP_PURGE_JOBS; | |
135 | ||
136 | if (argv[i][2] != '\0') | |
137 | user = argv[i] + 2; | |
138 | else | |
139 | { | |
140 | i ++; | |
141 | ||
142 | if (i >= argc) | |
143 | { | |
144 | _cupsLangPrintf(stderr, | |
145 | _("%s: Error - expected username after " | |
146 | "\"-u\" option."), argv[0]); | |
147 | return (1); | |
148 | } | |
149 | else | |
150 | user = argv[i]; | |
151 | } | |
152 | break; | |
153 | ||
154 | default : | |
155 | _cupsLangPrintf(stderr, | |
156 | _("%s: Error - unknown option \"%c\"."), | |
157 | argv[0], argv[i][1]); | |
158 | return (1); | |
159 | } | |
160 | else | |
161 | { | |
162 | /* | |
163 | * Cancel a job or printer... | |
164 | */ | |
165 | ||
166 | if (num_dests == 0) | |
167 | num_dests = cupsGetDests(&dests); | |
168 | ||
169 | if (!strcmp(argv[i], "-")) | |
170 | { | |
171 | /* | |
172 | * Delete the current job... | |
173 | */ | |
174 | ||
175 | dest = ""; | |
176 | job_id = 0; | |
177 | } | |
178 | else if (cupsGetDest(argv[i], NULL, num_dests, dests) != NULL) | |
179 | { | |
180 | /* | |
181 | * Delete the current job on the named destination... | |
182 | */ | |
183 | ||
184 | dest = argv[i]; | |
185 | job_id = 0; | |
186 | } | |
187 | else if ((job = strrchr(argv[i], '-')) != NULL && isdigit(job[1] & 255)) | |
188 | { | |
189 | /* | |
190 | * Delete the specified job ID. | |
191 | */ | |
192 | ||
193 | dest = NULL; | |
194 | op = IPP_CANCEL_JOB; | |
195 | job_id = atoi(job + 1); | |
196 | } | |
197 | else if (isdigit(argv[i][0] & 255)) | |
198 | { | |
199 | /* | |
200 | * Delete the specified job ID. | |
201 | */ | |
202 | ||
203 | dest = NULL; | |
204 | op = IPP_CANCEL_JOB; | |
205 | job_id = atoi(argv[i]); | |
206 | } | |
207 | else | |
208 | { | |
209 | /* | |
210 | * Bad printer name! | |
211 | */ | |
212 | ||
213 | _cupsLangPrintf(stderr, | |
214 | _("%s: Error - unknown destination \"%s\"."), | |
215 | argv[0], argv[i]); | |
216 | return (1); | |
217 | } | |
218 | ||
219 | /* | |
220 | * For Solaris LP compatibility, ignore a destination name after | |
221 | * cancelling a specific job ID... | |
222 | */ | |
223 | ||
224 | if (job_id && (i + 1) < argc && | |
225 | cupsGetDest(argv[i + 1], NULL, num_dests, dests) != NULL) | |
226 | i ++; | |
227 | ||
228 | /* | |
229 | * Open a connection to the server... | |
230 | */ | |
231 | ||
232 | if (http == NULL) | |
233 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
234 | cupsEncryption())) == NULL) | |
235 | { | |
236 | _cupsLangPrintf(stderr, | |
237 | _("%s: Unable to connect to server."), argv[0]); | |
238 | return (1); | |
239 | } | |
240 | ||
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 | |
248 | * [requesting-user-name] | |
249 | */ | |
250 | ||
251 | request = ippNewRequest(op); | |
252 | ||
253 | if (dest) | |
254 | { | |
255 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
256 | "localhost", 0, "/printers/%s", dest); | |
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 | { | |
264 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); | |
265 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, | |
266 | uri); | |
267 | } | |
268 | ||
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); | |
281 | ||
282 | /* | |
283 | * Do the request and get back a response... | |
284 | */ | |
285 | ||
286 | if (op == IPP_PURGE_JOBS && (!user || _cups_strcasecmp(user, cupsUser()))) | |
287 | response = cupsDoRequest(http, request, "/admin/"); | |
288 | else | |
289 | response = cupsDoRequest(http, request, "/jobs/"); | |
290 | ||
291 | if (response == NULL || | |
292 | response->request.status.status_code > IPP_OK_CONFLICT) | |
293 | { | |
294 | _cupsLangPrintf(stderr, _("%s: %s failed: %s"), argv[0], | |
295 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", | |
296 | cupsLastErrorString()); | |
297 | ||
298 | if (response) | |
299 | ippDelete(response); | |
300 | ||
301 | return (1); | |
302 | } | |
303 | ||
304 | ippDelete(response); | |
305 | } | |
306 | ||
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 | { | |
317 | _cupsLangPrintf(stderr, _("%s: Unable to contact server."), argv[0]); | |
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 | ||
331 | request = ippNewRequest(op); | |
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 | { | |
357 | _cupsLangPrintf(stderr, _("%s: %s failed: %s"), argv[0], | |
358 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", | |
359 | cupsLastErrorString()); | |
360 | ||
361 | if (response) | |
362 | ippDelete(response); | |
363 | ||
364 | return (1); | |
365 | } | |
366 | ||
367 | ippDelete(response); | |
368 | } | |
369 | ||
370 | return (0); | |
371 | } | |
372 | ||
373 | ||
374 | /* | |
375 | * End of "$Id$". | |
376 | */ |