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