]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cancel.c
Merge changes from 1.1 tree.
[thirdparty/cups.git] / systemv / cancel.c
1 /*
2 * "$Id: cancel.c,v 1.19.2.1 2001/05/13 18:38:40 mike Exp $"
3 *
4 * "cancel" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2001 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Parse options and cancel jobs.
27 */
28
29 /*
30 * Include necessary headers...
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36
37 #include <config.h>
38 #include <cups/cups.h>
39 #include <cups/language.h>
40
41
42 /*
43 * 'main()' - Parse options and cancel jobs.
44 */
45
46 int /* O - Exit status */
47 main(int argc, /* I - Number of command-line arguments */
48 char *argv[]) /* I - Command-line arguments */
49 {
50 http_t *http; /* HTTP connection to server */
51 int i; /* Looping var */
52 int job_id; /* Job ID */
53 char *dest, /* Destination printer */
54 *host, /* Host name */
55 *job; /* Job ID pointer */
56 char name[255]; /* Printer name */
57 char uri[1024]; /* Printer or job URI */
58 ipp_t *request; /* IPP request */
59 ipp_t *response; /* IPP response */
60 ipp_op_t op; /* Operation */
61 cups_lang_t *language; /* Language */
62 http_encryption_t encryption; /* Encryption? */
63
64
65 /*
66 * Setup to cancel individual print jobs...
67 */
68
69 op = IPP_CANCEL_JOB;
70 job_id = 0;
71 dest = NULL;
72 http = NULL;
73 encryption = cupsEncryption();
74
75 /*
76 * Process command-line arguments...
77 */
78
79 for (i = 1; i < argc; i ++)
80 if (argv[i][0] == '-' && argv[i][1])
81 switch (argv[i][1])
82 {
83 case 'E' : /* Encrypt */
84 #ifdef HAVE_LIBSSL
85 encryption = HTTP_ENCRYPT_REQUIRED;
86
87 if (http)
88 httpEncryption(http, encryption);
89 #else
90 fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n",
91 argv[0]);
92 #endif /* HAVE_LIBSSL */
93 break;
94
95 case 'a' : /* Cancel all jobs */
96 op = IPP_PURGE_JOBS;
97 break;
98
99 case 'h' : /* Connect to host */
100 if (http != NULL)
101 httpClose(http);
102
103 if (argv[i][2] != '\0')
104 http = httpConnectEncrypt(argv[i] + 2, ippPort(), encryption);
105 else
106 {
107 i ++;
108
109 if (i >= argc)
110 {
111 fputs("cancel: Error - expected hostname after \'-h\' option!\n", stderr);
112 return (1);
113 }
114 else
115 http = httpConnectEncrypt(argv[i], ippPort(), encryption);
116 }
117
118 if (http == NULL)
119 {
120 perror("cancel: Unable to connect to server");
121 return (1);
122 }
123 break;
124
125 case 'u' : /* Username */
126 if (argv[i][2] != '\0')
127 cupsSetUser(argv[i] + 2);
128 else
129 {
130 i ++;
131
132 if (i >= argc)
133 {
134 fputs("cancel: Error - expected username after \'-u\' option!\n", stderr);
135 return (1);
136 }
137 else
138 cupsSetUser(argv[i]);
139 }
140 break;
141
142 default :
143 fprintf(stderr, "cancel: Unknown option \'%c\'!\n", argv[i][1]);
144 return (1);
145 }
146 else
147 {
148 /*
149 * Cancel a job or printer...
150 */
151
152 if (isdigit(argv[i][0]))
153 {
154 dest = NULL;
155 op = IPP_CANCEL_JOB;
156 job_id = atoi(argv[i]);
157 }
158 else if (argv[i][0] == '-')
159 {
160 dest = "";
161 job_id = 0;
162 }
163 else
164 {
165 strncpy(name, argv[i], sizeof(name) - 1);
166 name[sizeof(name) - 1] = '\0';
167
168 dest = name;
169 job_id = 0;
170
171 if ((job = strrchr(name, '-')) != NULL)
172 if (isdigit(job[1]))
173 {
174 *job++ = '\0';
175 job_id = atoi(job);
176 }
177
178 if (job_id)
179 op = IPP_CANCEL_JOB;
180
181 if ((host = strchr(name, '@')) != NULL)
182 {
183 /*
184 * Reconnect to the named host...
185 */
186
187 if (http != NULL)
188 httpClose(http);
189
190 *host++ = '\0';
191
192 if ((http = httpConnectEncrypt(host, ippPort(), encryption)) == NULL)
193 {
194 perror("cancel: Unable to connect to server");
195 return (1);
196 }
197 }
198 }
199
200 /*
201 * Open a connection to the server...
202 */
203
204 if (http == NULL)
205 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
206 encryption)) == NULL)
207 {
208 fputs("cancel: Unable to contact server!\n", stderr);
209 return (1);
210 }
211
212 /*
213 * Build an IPP request, which requires the following
214 * attributes:
215 *
216 * attributes-charset
217 * attributes-natural-language
218 * printer-uri + job-id *or* job-uri
219 * [requesting-user-name]
220 */
221
222 request = ippNew();
223
224 request->request.op.operation_id = op;
225 request->request.op.request_id = 1;
226
227 language = cupsLangDefault();
228
229 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
230 "attributes-charset", NULL, cupsLangEncoding(language));
231
232 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
233 "attributes-natural-language", NULL, language->language);
234
235 if (dest)
236 {
237 snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", dest);
238 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
239 "printer-uri", NULL, uri);
240 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
241 job_id);
242 }
243 else
244 {
245 sprintf(uri, "ipp://localhost/jobs/%d", job_id);
246 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
247 uri);
248 }
249
250 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
251 "requesting-user-name", NULL, cupsUser());
252
253 /*
254 * Do the request and get back a response...
255 */
256
257 if (op == IPP_PURGE_JOBS)
258 response = cupsDoRequest(http, request, "/admin/");
259 else
260 response = cupsDoRequest(http, request, "/jobs/");
261
262 if (response == NULL ||
263 response->request.status.status_code > IPP_OK_CONFLICT)
264 {
265 fprintf(stderr, "cancel: %s failed: %s\n",
266 op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job",
267 response ? ippErrorString(response->request.status.status_code) :
268 ippErrorString(cupsLastError()));
269
270 if (response)
271 ippDelete(response);
272
273 return (1);
274 }
275
276 ippDelete(response);
277 }
278
279 return (0);
280 }
281
282
283 /*
284 * End of "$Id: cancel.c,v 1.19.2.1 2001/05/13 18:38:40 mike Exp $".
285 */