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