]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cancel.c
2 * "cancel" command for CUPS.
4 * Copyright 2007-2013 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include <cups/cups-private.h>
22 * 'main()' - Parse options and cancel jobs.
25 int /* O - Exit status */
26 main(int argc
, /* I - Number of command-line arguments */
27 char *argv
[]) /* I - Command-line arguments */
29 http_t
*http
; /* HTTP connection to server */
30 int i
; /* Looping var */
31 int job_id
; /* Job ID */
32 int num_dests
; /* Number of destinations */
33 cups_dest_t
*dests
; /* Destinations */
34 char *dest
, /* Destination printer */
35 *job
, /* Job ID pointer */
36 *user
; /* Cancel jobs for a user */
37 int purge
; /* Purge or cancel jobs? */
38 char uri
[1024]; /* Printer or job URI */
39 ipp_t
*request
; /* IPP request */
40 ipp_t
*response
; /* IPP response */
41 ipp_op_t op
; /* Operation */
47 * Setup to cancel individual print jobs...
60 * Process command-line arguments...
63 for (i
= 1; i
< argc
; i
++)
64 if (argv
[i
][0] == '-' && argv
[i
][1])
68 case 'E' : /* Encrypt */
70 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
73 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
75 _cupsLangPrintf(stderr
,
76 _("%s: Sorry, no encryption support."), argv
[0]);
80 case 'U' : /* Username */
81 if (argv
[i
][2] != '\0')
82 cupsSetUser(argv
[i
] + 2);
88 _cupsLangPrintf(stderr
,
89 _("%s: Error - expected username after "
90 "\"-U\" option."), argv
[0]);
98 case 'a' : /* Cancel all jobs */
99 op
= purge
? IPP_PURGE_JOBS
: IPP_CANCEL_JOBS
;
102 case 'h' : /* Connect to host */
109 if (argv
[i
][2] != '\0')
110 cupsSetServer(argv
[i
] + 2);
117 _cupsLangPrintf(stderr
,
118 _("%s: Error - expected hostname after "
119 "\"-h\" option."), argv
[0]);
123 cupsSetServer(argv
[i
]);
127 case 'u' : /* Username */
128 op
= IPP_CANCEL_MY_JOBS
;
130 if (argv
[i
][2] != '\0')
138 _cupsLangPrintf(stderr
,
139 _("%s: Error - expected username after "
140 "\"-u\" option."), argv
[0]);
148 case 'x' : /* Purge job(s) */
151 if (op
== IPP_CANCEL_JOBS
)
156 _cupsLangPrintf(stderr
,
157 _("%s: Error - unknown option \"%c\"."),
158 argv
[0], argv
[i
][1]);
165 * Cancel a job or printer...
169 num_dests
= cupsGetDests(&dests
);
171 if (!strcmp(argv
[i
], "-"))
174 * Delete the current job...
180 else if (cupsGetDest(argv
[i
], NULL
, num_dests
, dests
) != NULL
)
183 * Delete the current job on the named destination...
189 else if ((job
= strrchr(argv
[i
], '-')) != NULL
&& isdigit(job
[1] & 255))
192 * Delete the specified job ID.
197 job_id
= atoi(job
+ 1);
199 else if (isdigit(argv
[i
][0] & 255))
202 * Delete the specified job ID.
207 job_id
= atoi(argv
[i
]);
215 _cupsLangPrintf(stderr
,
216 _("%s: Error - unknown destination \"%s\"."),
222 * For Solaris LP compatibility, ignore a destination name after
223 * cancelling a specific job ID...
226 if (job_id
&& (i
+ 1) < argc
&&
227 cupsGetDest(argv
[i
+ 1], NULL
, num_dests
, dests
) != NULL
)
231 * Open a connection to the server...
235 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
236 cupsEncryption())) == NULL
)
238 _cupsLangPrintf(stderr
,
239 _("%s: Unable to connect to server."), argv
[0]);
244 * Build an IPP request, which requires the following
248 * attributes-natural-language
249 * printer-uri + job-id *or* job-uri
250 * [requesting-user-name]
253 request
= ippNewRequest(op
);
257 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
258 "localhost", 0, "/printers/%s", dest
);
259 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
260 "printer-uri", NULL
, uri
);
261 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
266 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
267 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
,
273 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
274 "requesting-user-name", NULL
, user
);
275 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
277 if (op
== IPP_CANCEL_JOBS
)
278 op
= IPP_CANCEL_MY_JOBS
;
281 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
282 "requesting-user-name", NULL
, cupsUser());
285 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", (char)purge
);
288 * Do the request and get back a response...
291 if (op
== IPP_CANCEL_JOBS
&& (!user
|| _cups_strcasecmp(user
, cupsUser())))
292 response
= cupsDoRequest(http
, request
, "/admin/");
294 response
= cupsDoRequest(http
, request
, "/jobs/");
296 if (response
== NULL
||
297 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
299 _cupsLangPrintf(stderr
, _("%s: %s failed: %s"), argv
[0],
300 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
301 cupsLastErrorString());
312 if (num_dests
== 0 && op
!= IPP_CANCEL_JOB
)
315 * Open a connection to the server...
319 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
320 cupsEncryption())) == NULL
)
322 _cupsLangPrintf(stderr
, _("%s: Unable to contact server."), argv
[0]);
327 * Build an IPP request, which requires the following
331 * attributes-natural-language
332 * printer-uri + job-id *or* job-uri
333 * [requesting-user-name]
336 request
= ippNewRequest(op
);
338 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
339 "printer-uri", NULL
, "ipp://localhost/printers/");
343 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
344 "requesting-user-name", NULL
, user
);
345 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
348 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
349 "requesting-user-name", NULL
, cupsUser());
351 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", (char)purge
);
354 * Do the request and get back a response...
357 response
= cupsDoRequest(http
, request
, "/admin/");
359 if (response
== NULL
||
360 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
362 _cupsLangPrintf(stderr
, _("%s: %s failed: %s"), argv
[0],
363 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
364 cupsLastErrorString());