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