]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cancel.c
2 * "cancel" command for CUPS.
4 * Copyright 2007-2016 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 *opt
, /* Option pointer */
35 *dest
, /* Destination printer */
36 *job
, /* Job ID pointer */
37 *user
; /* Cancel jobs for a user */
38 int purge
; /* Purge or cancel jobs? */
39 char uri
[1024]; /* Printer or job URI */
40 ipp_t
*request
; /* IPP request */
41 ipp_t
*response
; /* IPP response */
42 ipp_op_t op
; /* Operation */
48 * Setup to cancel individual print jobs...
61 * Process command-line arguments...
64 for (i
= 1; i
< argc
; i
++)
66 if (argv
[i
][0] == '-' && argv
[i
][1])
68 for (opt
= argv
[i
] + 1; *opt
; opt
++)
72 case 'E' : /* Encrypt */
74 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
77 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
79 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
83 case 'U' : /* Username */
87 opt
+= strlen(opt
) - 1;
94 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
102 case 'a' : /* Cancel all jobs */
103 op
= purge
? IPP_PURGE_JOBS
: IPP_CANCEL_JOBS
;
106 case 'h' : /* Connect to host */
115 cupsSetServer(opt
+ 1);
116 opt
+= strlen(opt
) - 1;
124 _cupsLangPrintf(stderr
, _("%s: Error - expected hostname after \"-h\" option."), argv
[0]);
128 cupsSetServer(argv
[i
]);
132 case 'u' : /* Username */
133 op
= IPP_CANCEL_MY_JOBS
;
138 opt
+= strlen(opt
) - 1;
146 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-u\" option."), argv
[0]);
154 case 'x' : /* Purge job(s) */
157 if (op
== IPP_CANCEL_JOBS
)
162 _cupsLangPrintf(stderr
, _("%s: Error - unknown option \"%c\"."), argv
[0], *opt
);
170 * Cancel a job or printer...
174 num_dests
= cupsGetDests(&dests
);
176 if (!strcmp(argv
[i
], "-"))
179 * Delete the current job...
185 else if (cupsGetDest(argv
[i
], NULL
, num_dests
, dests
) != NULL
)
188 * Delete the current job on the named destination...
194 else if ((job
= strrchr(argv
[i
], '-')) != NULL
&& isdigit(job
[1] & 255))
197 * Delete the specified job ID.
202 job_id
= atoi(job
+ 1);
204 else if (isdigit(argv
[i
][0] & 255))
207 * Delete the specified job ID.
212 job_id
= atoi(argv
[i
]);
220 _cupsLangPrintf(stderr
,
221 _("%s: Error - unknown destination \"%s\"."),
227 * For Solaris LP compatibility, ignore a destination name after
228 * cancelling a specific job ID...
231 if (job_id
&& (i
+ 1) < argc
&&
232 cupsGetDest(argv
[i
+ 1], NULL
, num_dests
, dests
) != NULL
)
236 * Open a connection to the server...
240 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
241 cupsEncryption())) == NULL
)
243 _cupsLangPrintf(stderr
,
244 _("%s: Unable to connect to server."), argv
[0]);
249 * Build an IPP request, which requires the following
253 * attributes-natural-language
254 * printer-uri + job-id *or* job-uri
255 * [requesting-user-name]
258 request
= ippNewRequest(op
);
262 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
263 "localhost", 0, "/printers/%s", dest
);
264 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
265 "printer-uri", NULL
, uri
);
266 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
271 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
272 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
,
278 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
279 "requesting-user-name", NULL
, user
);
280 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
282 if (op
== IPP_CANCEL_JOBS
)
283 op
= IPP_CANCEL_MY_JOBS
;
286 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
287 "requesting-user-name", NULL
, cupsUser());
290 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", (char)purge
);
293 * Do the request and get back a response...
296 if (op
== IPP_CANCEL_JOBS
&& (!user
|| _cups_strcasecmp(user
, cupsUser())))
297 response
= cupsDoRequest(http
, request
, "/admin/");
299 response
= cupsDoRequest(http
, request
, "/jobs/");
301 if (response
== NULL
||
302 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
304 _cupsLangPrintf(stderr
, _("%s: %s failed: %s"), argv
[0],
305 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
306 cupsLastErrorString());
318 if (num_dests
== 0 && op
!= IPP_CANCEL_JOB
)
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", (char)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());