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