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