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