]> git.ipfire.org Git - thirdparty/cups.git/blame - systemv/cancel.c
The command-line applications did not convert command-line
[thirdparty/cups.git] / systemv / cancel.c
CommitLineData
fc8c8467 1/*
c9d3f842 2 * "$Id$"
fc8c8467 3 *
4 * "cancel" command for the Common UNIX Printing System (CUPS).
5 *
0e66904d 6 * Copyright 1997-2006 by Easy Software Products.
fc8c8467 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
8784b6a6 17 * 44141 Airport View Drive, Suite 204
c9d3f842 18 * Hollywood, Maryland 20636 USA
fc8c8467 19 *
9639c4de 20 * Voice: (301) 373-9600
fc8c8467 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>
def978d5 35#include <cups/string.h>
fc8c8467 36#include <cups/cups.h>
b7382443 37#include <cups/i18n.h>
fc8c8467 38
39
40/*
41 * 'main()' - Parse options and cancel jobs.
42 */
43
b7382443 44int /* O - Exit status */
45main(int argc, /* I - Number of command-line arguments */
46 char *argv[]) /* I - Command-line arguments */
fc8c8467 47{
b7382443 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 */
fc8c8467 61
fc8c8467 62
156dd8aa 63 _cupsSetLocale(argv);
ebad28ad 64
b310a06a 65 /*
66 * Setup to cancel individual print jobs...
67 */
fc8c8467 68
00f3aaf5 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;
b7382443 77
fc8c8467 78
b310a06a 79 /*
80 * Process command-line arguments...
81 */
fc8c8467 82
b310a06a 83 for (i = 1; i < argc; i ++)
50b9556e 84 if (argv[i][0] == '-' && argv[i][1])
b310a06a 85 switch (argv[i][1])
86 {
1c9e0181 87 case 'E' : /* Encrypt */
bcf61448 88#ifdef HAVE_SSL
192a8a08 89 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
1c9e0181 90
91 if (http)
192a8a08 92 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
1c9e0181 93#else
89fd567e 94 _cupsLangPrintf(stderr,
b7382443 95 _("%s: Sorry, no encryption support compiled in!\n"),
96 argv[0]);
bcf61448 97#endif /* HAVE_SSL */
1c9e0181 98 break;
99
cfea7a92 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
b310a06a 119 case 'a' : /* Cancel all jobs */
dd9e85de 120 purge = 1;
121 op = IPP_PURGE_JOBS;
fc8c8467 122 break;
123
808dd474 124 case 'h' : /* Connect to host */
8dbf3e6a 125 if (http != NULL)
6cf8f89f 126 {
8dbf3e6a 127 httpClose(http);
6cf8f89f 128 http = NULL;
129 }
808dd474 130
131 if (argv[i][2] != '\0')
5ae53c1e 132 cupsSetServer(argv[i] + 2);
808dd474 133 else
134 {
135 i ++;
8a4fe7c7 136
137 if (i >= argc)
138 {
cfea7a92 139 _cupsLangPrintf(stderr,
140 _("%s: Error - expected hostname after "
141 "\'-h\' option!\n"),
142 argv[0]);
8a4fe7c7 143 return (1);
144 }
145 else
5ae53c1e 146 cupsSetServer(argv[i]);
808dd474 147 }
148 break;
149
2558a535 150 case 'u' : /* Username */
dd9e85de 151 op = IPP_PURGE_JOBS;
152
2558a535 153 if (argv[i][2] != '\0')
dd9e85de 154 user = argv[i] + 2;
2558a535 155 else
156 {
157 i ++;
158
159 if (i >= argc)
160 {
cfea7a92 161 _cupsLangPrintf(stderr,
162 _("%s: Error - expected username after "
163 "\'-u\' option!\n"),
164 argv[0]);
2558a535 165 return (1);
166 }
167 else
dd9e85de 168 user = argv[i];
2558a535 169 }
170 break;
171
fc8c8467 172 default :
89fd567e 173 _cupsLangPrintf(stderr,
cfea7a92 174 _("%s: Error - unknown option \'%c\'!\n"),
175 argv[0], argv[i][1]);
fc8c8467 176 return (1);
177 }
b310a06a 178 else
fc8c8467 179 {
b310a06a 180 /*
181 * Cancel a job or printer...
182 */
fc8c8467 183
aa37e40e 184 if (num_dests == 0)
185 num_dests = cupsGetDests(&dests);
186
398fa469 187 if (!strcmp(argv[i], "-"))
50b9556e 188 {
5ae53c1e 189 /*
190 * Delete the current job...
191 */
192
50b9556e 193 dest = "";
194 job_id = 0;
195 }
5ae53c1e 196 else if (cupsGetDest(argv[i], NULL, num_dests, dests) != NULL)
b310a06a 197 {
5ae53c1e 198 /*
199 * Delete the current job on the named destination...
200 */
50b9556e 201
5ae53c1e 202 dest = argv[i];
203 job_id = 0;
204 }
64bbab09 205 else if ((job = strrchr(argv[i], '-')) != NULL && isdigit(job[1] & 255))
5ae53c1e 206 {
207 /*
208 * Delete the specified job ID.
209 */
50b9556e 210
5ae53c1e 211 dest = NULL;
212 op = IPP_CANCEL_JOB;
213 job_id = atoi(job + 1);
214 }
64bbab09 215 else if (isdigit(argv[i][0] & 255))
5ae53c1e 216 {
217 /*
218 * Delete the specified job ID.
219 */
808dd474 220
5ae53c1e 221 dest = NULL;
222 op = IPP_CANCEL_JOB;
223 job_id = atoi(argv[i]);
224 }
225 else
226 {
227 /*
228 * Bad printer name!
229 */
808dd474 230
89fd567e 231 _cupsLangPrintf(stderr,
cfea7a92 232 _("%s: Error - unknown destination \"%s\"!\n"),
233 argv[0], argv[i]);
727e6d14 234 return (1);
5ae53c1e 235 }
808dd474 236
5ae53c1e 237 /*
238 * For Solaris LP compatibility, ignore a destination name after
239 * cancelling a specific job ID...
240 */
808dd474 241
5ae53c1e 242 if (job_id && (i + 1) < argc &&
243 cupsGetDest(argv[i + 1], NULL, num_dests, dests) != NULL)
244 i ++;
fc8c8467 245
8dbf3e6a 246 /*
247 * Open a connection to the server...
248 */
249
250 if (http == NULL)
b5cb0608 251 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
192a8a08 252 cupsEncryption())) == NULL)
8dbf3e6a 253 {
cfea7a92 254 _cupsLangPrintf(stderr,
255 _("%s: Unable to contact server!\n"),
256 argv[0]);
8dbf3e6a 257 return (1);
258 }
259
b310a06a 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
8dbf3e6a 267 * [requesting-user-name]
b310a06a 268 */
fc8c8467 269
00f3aaf5 270 request = ippNewRequest(op);
fc8c8467 271
b310a06a 272 if (dest)
273 {
00a1fad8 274 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
275 "localhost", 0, "/printers/%s", dest);
b310a06a 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 {
1fd07458 283 sprintf(uri, "ipp://localhost/jobs/%d", job_id);
b310a06a 284 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
285 uri);
286 }
fc8c8467 287
dd9e85de 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);
8dbf3e6a 300
b310a06a 301 /*
302 * Do the request and get back a response...
303 */
fc8c8467 304
3f5dfefd 305 if (op == IPP_PURGE_JOBS && (!user || strcasecmp(user, cupsUser())))
b310a06a 306 response = cupsDoRequest(http, request, "/admin/");
307 else
308 response = cupsDoRequest(http, request, "/jobs/");
fc8c8467 309
96ecd6f0 310 if (response == NULL ||
0a3ac972 311 response->request.status.status_code > IPP_OK_CONFLICT)
8cf354e9 312 {
cfea7a92 313 _cupsLangPrintf(stderr, _("%s: %s failed: %s\n"), argv[0],
b7382443 314 op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job",
398fa469 315 cupsLastErrorString());
96ecd6f0 316
317 if (response)
318 ippDelete(response);
8cf354e9 319
b310a06a 320 return (1);
321 }
96ecd6f0 322
323 ippDelete(response);
fc8c8467 324 }
fc8c8467 325
b07e0cc2 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 {
cfea7a92 336 _cupsLangPrintf(stderr, _("%s: Unable to contact server!\n"),
337 argv[0]);
b07e0cc2 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
00f3aaf5 351 request = ippNewRequest(op);
b07e0cc2 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 {
cfea7a92 377 _cupsLangPrintf(stderr, _("%s: %s failed: %s\n"), argv[0],
b7382443 378 op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job",
00f3aaf5 379 cupsLastErrorString());
b07e0cc2 380
381 if (response)
382 ippDelete(response);
383
384 return (1);
385 }
386
387 ippDelete(response);
388 }
389
fc8c8467 390 return (0);
391}
392
393
394/*
c9d3f842 395 * End of "$Id$".
fc8c8467 396 */