]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cancel.c
2 * "cancel" command for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include <cups/cups-private.h>
22 static void usage(void) _CUPS_NORETURN
;
26 * 'main()' - Parse options and cancel jobs.
29 int /* O - Exit status */
30 main(int argc
, /* I - Number of command-line arguments */
31 char *argv
[]) /* I - Command-line arguments */
33 http_t
*http
; /* HTTP connection to server */
34 int i
; /* Looping var */
35 int job_id
; /* Job ID */
36 int num_dests
; /* Number of destinations */
37 cups_dest_t
*dests
; /* Destinations */
38 char *opt
, /* Option pointer */
39 *dest
, /* Destination printer */
40 *job
, /* Job ID pointer */
41 *user
; /* Cancel jobs for a user */
42 int purge
; /* Purge or cancel jobs? */
43 char uri
[1024]; /* Printer or job URI */
44 ipp_t
*request
; /* IPP request */
45 ipp_t
*response
; /* IPP response */
46 ipp_op_t op
; /* Operation */
52 * Setup to cancel individual print jobs...
65 * Process command-line arguments...
68 for (i
= 1; i
< argc
; i
++)
70 if (!strcmp(argv
[i
], "--help"))
72 else if (argv
[i
][0] == '-' && argv
[i
][1])
74 for (opt
= argv
[i
] + 1; *opt
; opt
++)
78 case 'E' : /* Encrypt */
80 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
83 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
85 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
89 case 'U' : /* Username */
93 opt
+= strlen(opt
) - 1;
100 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
104 cupsSetUser(argv
[i
]);
108 case 'a' : /* Cancel all jobs */
109 op
= purge
? IPP_PURGE_JOBS
: IPP_CANCEL_JOBS
;
112 case 'h' : /* Connect to host */
121 cupsSetServer(opt
+ 1);
122 opt
+= strlen(opt
) - 1;
130 _cupsLangPrintf(stderr
, _("%s: Error - expected hostname after \"-h\" option."), argv
[0]);
134 cupsSetServer(argv
[i
]);
138 case 'u' : /* Username */
139 op
= IPP_CANCEL_MY_JOBS
;
144 opt
+= strlen(opt
) - 1;
152 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-u\" option."), argv
[0]);
160 case 'x' : /* Purge job(s) */
163 if (op
== IPP_CANCEL_JOBS
)
168 _cupsLangPrintf(stderr
, _("%s: Error - unknown option \"%c\"."), argv
[0], *opt
);
176 * Cancel a job or printer...
180 num_dests
= cupsGetDests(&dests
);
182 if (!strcmp(argv
[i
], "-"))
185 * Delete the current job...
191 else if (cupsGetDest(argv
[i
], NULL
, num_dests
, dests
) != NULL
)
194 * Delete the current job on the named destination...
200 else if ((job
= strrchr(argv
[i
], '-')) != NULL
&& isdigit(job
[1] & 255))
203 * Delete the specified job ID.
208 job_id
= atoi(job
+ 1);
210 else if (isdigit(argv
[i
][0] & 255))
213 * Delete the specified job ID.
218 job_id
= atoi(argv
[i
]);
226 _cupsLangPrintf(stderr
,
227 _("%s: Error - unknown destination \"%s\"."),
233 * For Solaris LP compatibility, ignore a destination name after
234 * cancelling a specific job ID...
237 if (job_id
&& (i
+ 1) < argc
&&
238 cupsGetDest(argv
[i
+ 1], NULL
, num_dests
, dests
) != NULL
)
242 * Open a connection to the server...
246 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
247 cupsEncryption())) == NULL
)
249 _cupsLangPrintf(stderr
,
250 _("%s: Unable to connect to server."), argv
[0]);
255 * Build an IPP request, which requires the following
259 * attributes-natural-language
260 * printer-uri + job-id *or* job-uri
261 * [requesting-user-name]
264 request
= ippNewRequest(op
);
268 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
269 "localhost", 0, "/printers/%s", dest
);
270 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
271 "printer-uri", NULL
, uri
);
272 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id",
277 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
278 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
,
284 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
285 "requesting-user-name", NULL
, user
);
286 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
288 if (op
== IPP_CANCEL_JOBS
)
289 op
= IPP_CANCEL_MY_JOBS
;
292 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
293 "requesting-user-name", NULL
, cupsUser());
296 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", (char)purge
);
299 * Do the request and get back a response...
302 if (op
== IPP_CANCEL_JOBS
&& (!user
|| _cups_strcasecmp(user
, cupsUser())))
303 response
= cupsDoRequest(http
, request
, "/admin/");
305 response
= cupsDoRequest(http
, request
, "/jobs/");
307 if (response
== NULL
||
308 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
310 _cupsLangPrintf(stderr
, _("%s: %s failed: %s"), argv
[0],
311 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
312 cupsLastErrorString());
324 if (num_dests
== 0 && op
!= IPP_CANCEL_JOB
)
327 * Open a connection to the server...
331 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
332 cupsEncryption())) == NULL
)
334 _cupsLangPrintf(stderr
, _("%s: Unable to contact server."), argv
[0]);
339 * Build an IPP request, which requires the following
343 * attributes-natural-language
344 * printer-uri + job-id *or* job-uri
345 * [requesting-user-name]
348 request
= ippNewRequest(op
);
350 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
351 "printer-uri", NULL
, "ipp://localhost/printers/");
355 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
356 "requesting-user-name", NULL
, user
);
357 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
360 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
361 "requesting-user-name", NULL
, cupsUser());
363 ippAddBoolean(request
, IPP_TAG_OPERATION
, "purge-jobs", (char)purge
);
366 * Do the request and get back a response...
369 response
= cupsDoRequest(http
, request
, "/admin/");
371 if (response
== NULL
||
372 response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
374 _cupsLangPrintf(stderr
, _("%s: %s failed: %s"), argv
[0],
375 op
== IPP_PURGE_JOBS
? "purge-jobs" : "cancel-job",
376 cupsLastErrorString());
392 * 'usage()' - Show program usage and exit.
398 _cupsLangPuts(stdout
, _("Usage: cancel [options] [id]\n"
399 " cancel [options] [destination]\n"
400 " cancel [options] [destination-id]"));
401 _cupsLangPuts(stdout
, _("Options:"));
402 _cupsLangPuts(stdout
, _("-a Cancel all jobs"));
403 _cupsLangPuts(stdout
, _("-E Encrypt the connection to the server"));
404 _cupsLangPuts(stdout
, _("-h server[:port] Connect to the named server and port"));
405 _cupsLangPuts(stdout
, _("-u owner Specify the owner to use for jobs"));
406 _cupsLangPuts(stdout
, _("-U username Specify the username to use for authentication"));
407 _cupsLangPuts(stdout
, _("-x Purge jobs rather than just canceling"));