]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cancel.c
Remove all of the Subversion keywords from various source files.
[thirdparty/cups.git] / systemv / cancel.c
1 /*
2 * "cancel" command for CUPS.
3 *
4 * Copyright 2007-2013 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
6 *
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/".
12 */
13
14 /*
15 * Include necessary headers...
16 */
17
18 #include <cups/cups-private.h>
19
20
21 /*
22 * 'main()' - Parse options and cancel jobs.
23 */
24
25 int /* O - Exit status */
26 main(int argc, /* I - Number of command-line arguments */
27 char *argv[]) /* I - Command-line arguments */
28 {
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 */
42
43
44 _cupsSetLocale(argv);
45
46 /*
47 * Setup to cancel individual print jobs...
48 */
49
50 op = IPP_CANCEL_JOB;
51 purge = 0;
52 dest = NULL;
53 user = NULL;
54 http = NULL;
55 num_dests = 0;
56 dests = NULL;
57
58
59 /*
60 * Process command-line arguments...
61 */
62
63 for (i = 1; i < argc; i ++)
64 if (argv[i][0] == '-' && argv[i][1])
65 {
66 switch (argv[i][1])
67 {
68 case 'E' : /* Encrypt */
69 #ifdef HAVE_SSL
70 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
71
72 if (http)
73 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
74 #else
75 _cupsLangPrintf(stderr,
76 _("%s: Sorry, no encryption support."), argv[0]);
77 #endif /* HAVE_SSL */
78 break;
79
80 case 'U' : /* Username */
81 if (argv[i][2] != '\0')
82 cupsSetUser(argv[i] + 2);
83 else
84 {
85 i ++;
86 if (i >= argc)
87 {
88 _cupsLangPrintf(stderr,
89 _("%s: Error - expected username after "
90 "\"-U\" option."), argv[0]);
91 return (1);
92 }
93
94 cupsSetUser(argv[i]);
95 }
96 break;
97
98 case 'a' : /* Cancel all jobs */
99 op = purge ? IPP_PURGE_JOBS : IPP_CANCEL_JOBS;
100 break;
101
102 case 'h' : /* Connect to host */
103 if (http != NULL)
104 {
105 httpClose(http);
106 http = NULL;
107 }
108
109 if (argv[i][2] != '\0')
110 cupsSetServer(argv[i] + 2);
111 else
112 {
113 i ++;
114
115 if (i >= argc)
116 {
117 _cupsLangPrintf(stderr,
118 _("%s: Error - expected hostname after "
119 "\"-h\" option."), argv[0]);
120 return (1);
121 }
122 else
123 cupsSetServer(argv[i]);
124 }
125 break;
126
127 case 'u' : /* Username */
128 op = IPP_CANCEL_MY_JOBS;
129
130 if (argv[i][2] != '\0')
131 user = argv[i] + 2;
132 else
133 {
134 i ++;
135
136 if (i >= argc)
137 {
138 _cupsLangPrintf(stderr,
139 _("%s: Error - expected username after "
140 "\"-u\" option."), argv[0]);
141 return (1);
142 }
143 else
144 user = argv[i];
145 }
146 break;
147
148 case 'x' : /* Purge job(s) */
149 purge = 1;
150
151 if (op == IPP_CANCEL_JOBS)
152 op = IPP_PURGE_JOBS;
153 break;
154
155 default :
156 _cupsLangPrintf(stderr,
157 _("%s: Error - unknown option \"%c\"."),
158 argv[0], argv[i][1]);
159 return (1);
160 }
161 }
162 else
163 {
164 /*
165 * Cancel a job or printer...
166 */
167
168 if (num_dests == 0)
169 num_dests = cupsGetDests(&dests);
170
171 if (!strcmp(argv[i], "-"))
172 {
173 /*
174 * Delete the current job...
175 */
176
177 dest = "";
178 job_id = 0;
179 }
180 else if (cupsGetDest(argv[i], NULL, num_dests, dests) != NULL)
181 {
182 /*
183 * Delete the current job on the named destination...
184 */
185
186 dest = argv[i];
187 job_id = 0;
188 }
189 else if ((job = strrchr(argv[i], '-')) != NULL && isdigit(job[1] & 255))
190 {
191 /*
192 * Delete the specified job ID.
193 */
194
195 dest = NULL;
196 op = IPP_CANCEL_JOB;
197 job_id = atoi(job + 1);
198 }
199 else if (isdigit(argv[i][0] & 255))
200 {
201 /*
202 * Delete the specified job ID.
203 */
204
205 dest = NULL;
206 op = IPP_CANCEL_JOB;
207 job_id = atoi(argv[i]);
208 }
209 else
210 {
211 /*
212 * Bad printer name!
213 */
214
215 _cupsLangPrintf(stderr,
216 _("%s: Error - unknown destination \"%s\"."),
217 argv[0], argv[i]);
218 return (1);
219 }
220
221 /*
222 * For Solaris LP compatibility, ignore a destination name after
223 * cancelling a specific job ID...
224 */
225
226 if (job_id && (i + 1) < argc &&
227 cupsGetDest(argv[i + 1], NULL, num_dests, dests) != NULL)
228 i ++;
229
230 /*
231 * Open a connection to the server...
232 */
233
234 if (http == NULL)
235 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
236 cupsEncryption())) == NULL)
237 {
238 _cupsLangPrintf(stderr,
239 _("%s: Unable to connect to server."), argv[0]);
240 return (1);
241 }
242
243 /*
244 * Build an IPP request, which requires the following
245 * attributes:
246 *
247 * attributes-charset
248 * attributes-natural-language
249 * printer-uri + job-id *or* job-uri
250 * [requesting-user-name]
251 */
252
253 request = ippNewRequest(op);
254
255 if (dest)
256 {
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",
262 job_id);
263 }
264 else
265 {
266 sprintf(uri, "ipp://localhost/jobs/%d", job_id);
267 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
268 uri);
269 }
270
271 if (user)
272 {
273 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
274 "requesting-user-name", NULL, user);
275 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
276
277 if (op == IPP_CANCEL_JOBS)
278 op = IPP_CANCEL_MY_JOBS;
279 }
280 else
281 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
282 "requesting-user-name", NULL, cupsUser());
283
284 if (purge)
285 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge);
286
287 /*
288 * Do the request and get back a response...
289 */
290
291 if (op == IPP_CANCEL_JOBS && (!user || _cups_strcasecmp(user, cupsUser())))
292 response = cupsDoRequest(http, request, "/admin/");
293 else
294 response = cupsDoRequest(http, request, "/jobs/");
295
296 if (response == NULL ||
297 response->request.status.status_code > IPP_OK_CONFLICT)
298 {
299 _cupsLangPrintf(stderr, _("%s: %s failed: %s"), argv[0],
300 op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job",
301 cupsLastErrorString());
302
303 if (response)
304 ippDelete(response);
305
306 return (1);
307 }
308
309 ippDelete(response);
310 }
311
312 if (num_dests == 0 && op != IPP_CANCEL_JOB)
313 {
314 /*
315 * Open a connection to the server...
316 */
317
318 if (http == NULL)
319 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
320 cupsEncryption())) == NULL)
321 {
322 _cupsLangPrintf(stderr, _("%s: Unable to contact server."), argv[0]);
323 return (1);
324 }
325
326 /*
327 * Build an IPP request, which requires the following
328 * attributes:
329 *
330 * attributes-charset
331 * attributes-natural-language
332 * printer-uri + job-id *or* job-uri
333 * [requesting-user-name]
334 */
335
336 request = ippNewRequest(op);
337
338 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
339 "printer-uri", NULL, "ipp://localhost/printers/");
340
341 if (user)
342 {
343 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
344 "requesting-user-name", NULL, user);
345 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
346 }
347 else
348 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
349 "requesting-user-name", NULL, cupsUser());
350
351 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge);
352
353 /*
354 * Do the request and get back a response...
355 */
356
357 response = cupsDoRequest(http, request, "/admin/");
358
359 if (response == NULL ||
360 response->request.status.status_code > IPP_OK_CONFLICT)
361 {
362 _cupsLangPrintf(stderr, _("%s: %s failed: %s"), argv[0],
363 op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job",
364 cupsLastErrorString());
365
366 if (response)
367 ippDelete(response);
368
369 return (1);
370 }
371
372 ippDelete(response);
373 }
374
375 return (0);
376 }