]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cancel.c
7314aa668582a311e805b5a716bb3a15271b24ef
2 * "cancel" command for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
18 * 'main()' - Parse options and cancel jobs.
21 int /* O - Exit status */
22 main(int argc
, /* I - Number of command-line arguments */
23 char *argv
[]) /* I - Command-line arguments */
25 http_t
*http
; /* HTTP connection to server */
26 int i
; /* Looping var */
27 int job_id
; /* Job ID */
28 int num_dests
; /* Number of destinations */
29 cups_dest_t
*dests
; /* Destinations */
30 char *opt
, /* Option pointer */
31 *dest
, /* Destination printer */
32 *job
, /* Job ID pointer */
33 *user
; /* Cancel jobs for a user */
34 int purge
; /* Purge or cancel jobs? */
35 char uri
[1024]; /* Printer or job URI */
36 ipp_t
*request
; /* IPP request */
37 ipp_t
*response
; /* IPP response */
38 ipp_op_t op
; /* Operation */
44 * Setup to cancel individual print jobs...
57 * Process command-line arguments...
60 for (i
= 1; i
< argc
; i
++)
62 if (argv
[i
][0] == '-' && argv
[i
][1])
64 for (opt
= argv
[i
] + 1; *opt
; opt
++)
68 case 'E' : /* Encrypt */
70 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
73 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
75 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
79 case 'U' : /* Username */
83 opt
+= strlen(opt
) - 1;
90 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-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 */
111 cupsSetServer(opt
+ 1);
112 opt
+= strlen(opt
) - 1;
120 _cupsLangPrintf(stderr
, _("%s: Error - expected hostname after \"-h\" option."), argv
[0]);
124 cupsSetServer(argv
[i
]);
128 case 'u' : /* Username */
129 op
= IPP_CANCEL_MY_JOBS
;
134 opt
+= strlen(opt
) - 1;
142 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-u\" option."), argv
[0]);
150 case 'x' : /* Purge job(s) */
153 if (op
== IPP_CANCEL_JOBS
)
158 _cupsLangPrintf(stderr
, _("%s: Error - unknown option \"%c\"."), argv
[0], *opt
);
166 * Cancel a job or printer...
170 num_dests
= cupsGetDests(&dests
);
172 if (!strcmp(argv
[i
], "-"))
175 * Delete the current job...
181 else if (cupsGetDest(argv
[i
], NULL
, num_dests
, dests
) != NULL
)
184 * Delete the current job on the named destination...
190 else if ((job
= strrchr(argv
[i
], '-')) != NULL
&& isdigit(job
[1] & 255))
193 * Delete the specified job ID.
198 job_id
= atoi(job
+ 1);
200 else if (isdigit(argv
[i
][0] & 255))
203 * Delete the specified job ID.
208 job_id
= atoi(argv
[i
]);
216 _cupsLangPrintf(stderr
,
217 _("%s: Error - unknown destination \"%s\"."),
223 * For Solaris LP compatibility, ignore a destination name after
224 * cancelling a specific job ID...
227 if (job_id
&& (i
+ 1) < argc
&&
228 cupsGetDest(argv
[i
+ 1], NULL
, num_dests
, dests
) != NULL
)
232 * Open a connection to the server...
236 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
237 cupsEncryption())) == NULL
)
239 _cupsLangPrintf(stderr
,
240 _("%s: Unable to connect to server."), argv
[0]);
245 * Build an IPP request, which requires the following
249 * attributes-natural-language
250 * printer-uri + job-id *or* job-uri
251 * [requesting-user-name]
254 request
= ippNewRequest(op
);
258 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
259 "localhost", 0, "/printers/%s", dest
);
260 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
261 "printer-uri", NULL
, uri
);
262 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
267 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
268 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
,
274 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
275 "requesting-user-name", NULL
, user
);
276 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
278 if (op
== IPP_CANCEL_JOBS
)
279 op
= IPP_CANCEL_MY_JOBS
;
282 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
283 "requesting-user-name", NULL
, cupsUser());
286 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", (char)purge
);
289 * Do the request and get back a response...
292 if (op
== IPP_CANCEL_JOBS
&& (!user
|| _cups_strcasecmp(user
, cupsUser())))
293 response
= cupsDoRequest(http
, request
, "/admin/");
295 response
= cupsDoRequest(http
, request
, "/jobs/");
297 if (response
== NULL
||
298 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
300 _cupsLangPrintf(stderr
, _("%s: %s failed: %s"), argv
[0],
301 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
302 cupsLastErrorString());
314 if (num_dests
== 0 && op
!= IPP_CANCEL_JOB
)
317 * Open a connection to the server...
321 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
322 cupsEncryption())) == NULL
)
324 _cupsLangPrintf(stderr
, _("%s: Unable to contact server."), argv
[0]);
329 * Build an IPP request, which requires the following
333 * attributes-natural-language
334 * printer-uri + job-id *or* job-uri
335 * [requesting-user-name]
338 request
= ippNewRequest(op
);
340 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
341 "printer-uri", NULL
, "ipp://localhost/printers/");
345 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
346 "requesting-user-name", NULL
, user
);
347 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
350 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
351 "requesting-user-name", NULL
, cupsUser());
353 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", (char)purge
);
356 * Do the request and get back a response...
359 response
= cupsDoRequest(http
, request
, "/admin/");
361 if (response
== NULL
||
362 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
364 _cupsLangPrintf(stderr
, _("%s: %s failed: %s"), argv
[0],
365 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
366 cupsLastErrorString());