]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cancel.c
4 * "cancel" command for CUPS.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
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/".
17 * main() - Parse options and cancel jobs.
21 * Include necessary headers...
24 #include <cups/cups-private.h>
28 * 'main()' - Parse options and cancel jobs.
31 int /* O - Exit status */
32 main(int argc
, /* I - Number of command-line arguments */
33 char *argv
[]) /* I - Command-line arguments */
35 http_t
*http
; /* HTTP connection to server */
36 int i
; /* Looping var */
37 int job_id
; /* Job ID */
38 int num_dests
; /* Number of destinations */
39 cups_dest_t
*dests
; /* Destinations */
40 char *dest
, /* Destination printer */
41 *job
, /* Job ID pointer */
42 *user
; /* Cancel jobs for a user */
43 int purge
; /* Purge or cancel jobs? */
44 char uri
[1024]; /* Printer or job URI */
45 ipp_t
*request
; /* IPP request */
46 ipp_t
*response
; /* IPP response */
47 ipp_op_t op
; /* Operation */
53 * Setup to cancel individual print jobs...
66 * Process command-line arguments...
69 for (i
= 1; i
< argc
; i
++)
70 if (argv
[i
][0] == '-' && argv
[i
][1])
74 case 'E' : /* Encrypt */
76 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
79 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
81 _cupsLangPrintf(stderr
,
82 _("%s: Sorry, no encryption support."), argv
[0]);
86 case 'U' : /* Username */
87 if (argv
[i
][2] != '\0')
88 cupsSetUser(argv
[i
] + 2);
94 _cupsLangPrintf(stderr
,
95 _("%s: Error - expected username after "
96 "\"-U\" option."), argv
[0]);
100 cupsSetUser(argv
[i
]);
104 case 'a' : /* Cancel all jobs */
105 op
= purge
? IPP_PURGE_JOBS
: IPP_CANCEL_JOBS
;
108 case 'h' : /* Connect to host */
115 if (argv
[i
][2] != '\0')
116 cupsSetServer(argv
[i
] + 2);
123 _cupsLangPrintf(stderr
,
124 _("%s: Error - expected hostname after "
125 "\"-h\" option."), argv
[0]);
129 cupsSetServer(argv
[i
]);
133 case 'u' : /* Username */
134 op
= IPP_CANCEL_MY_JOBS
;
136 if (argv
[i
][2] != '\0')
144 _cupsLangPrintf(stderr
,
145 _("%s: Error - expected username after "
146 "\"-u\" option."), argv
[0]);
154 case 'x' : /* Purge job(s) */
157 if (op
== IPP_CANCEL_JOBS
)
162 _cupsLangPrintf(stderr
,
163 _("%s: Error - unknown option \"%c\"."),
164 argv
[0], argv
[i
][1]);
171 * Cancel a job or printer...
175 num_dests
= cupsGetDests(&dests
);
177 if (!strcmp(argv
[i
], "-"))
180 * Delete the current job...
186 else if (cupsGetDest(argv
[i
], NULL
, num_dests
, dests
) != NULL
)
189 * Delete the current job on the named destination...
195 else if ((job
= strrchr(argv
[i
], '-')) != NULL
&& isdigit(job
[1] & 255))
198 * Delete the specified job ID.
203 job_id
= atoi(job
+ 1);
205 else if (isdigit(argv
[i
][0] & 255))
208 * Delete the specified job ID.
213 job_id
= atoi(argv
[i
]);
221 _cupsLangPrintf(stderr
,
222 _("%s: Error - unknown destination \"%s\"."),
228 * For Solaris LP compatibility, ignore a destination name after
229 * cancelling a specific job ID...
232 if (job_id
&& (i
+ 1) < argc
&&
233 cupsGetDest(argv
[i
+ 1], NULL
, num_dests
, dests
) != NULL
)
237 * Open a connection to the server...
241 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
242 cupsEncryption())) == NULL
)
244 _cupsLangPrintf(stderr
,
245 _("%s: Unable to connect to server."), argv
[0]);
250 * Build an IPP request, which requires the following
254 * attributes-natural-language
255 * printer-uri + job-id *or* job-uri
256 * [requesting-user-name]
259 request
= ippNewRequest(op
);
263 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
264 "localhost", 0, "/printers/%s", dest
);
265 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
266 "printer-uri", NULL
, uri
);
267 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
272 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
273 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
,
279 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
280 "requesting-user-name", NULL
, user
);
281 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
283 if (op
== IPP_CANCEL_JOBS
)
284 op
= IPP_CANCEL_MY_JOBS
;
287 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
288 "requesting-user-name", NULL
, cupsUser());
291 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", purge
);
294 * Do the request and get back a response...
297 if (op
== IPP_CANCEL_JOBS
&& (!user
|| _cups_strcasecmp(user
, cupsUser())))
298 response
= cupsDoRequest(http
, request
, "/admin/");
300 response
= cupsDoRequest(http
, request
, "/jobs/");
302 if (response
== NULL
||
303 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
305 _cupsLangPrintf(stderr
, _("%s: %s failed: %s"), argv
[0],
306 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
307 cupsLastErrorString());
318 if (num_dests
== 0 && op
== IPP_PURGE_JOBS
)
321 * Open a connection to the server...
325 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
326 cupsEncryption())) == NULL
)
328 _cupsLangPrintf(stderr
, _("%s: Unable to contact server."), argv
[0]);
333 * Build an IPP request, which requires the following
337 * attributes-natural-language
338 * printer-uri + job-id *or* job-uri
339 * [requesting-user-name]
342 request
= ippNewRequest(op
);
344 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
345 "printer-uri", NULL
, "ipp://localhost/printers/");
349 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
350 "requesting-user-name", NULL
, user
);
351 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
354 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
355 "requesting-user-name", NULL
, cupsUser());
357 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", purge
);
360 * Do the request and get back a response...
363 response
= cupsDoRequest(http
, request
, "/admin/");
365 if (response
== NULL
||
366 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
368 _cupsLangPrintf(stderr
, _("%s: %s failed: %s"), argv
[0],
369 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
370 cupsLastErrorString());