]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cancel.c
Add strlcat() and strlcpy() checks and emulation functions.
[thirdparty/cups.git] / systemv / cancel.c
1 /*
2 * "$Id: cancel.c,v 1.23 2002/05/16 13:45:04 mike Exp $"
3 *
4 * "cancel" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2002 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/string.h>
40 #include <cups/language.h>
41
42
43 /*
44 * 'main()' - Parse options and cancel jobs.
45 */
46
47 int /* O - Exit status */
48 main(int argc, /* I - Number of command-line arguments */
49 char *argv[]) /* I - Command-line arguments */
50 {
51 http_t *http; /* HTTP connection to server */
52 int i; /* Looping var */
53 int job_id; /* Job ID */
54 char *dest, /* Destination printer */
55 *host, /* Host name */
56 *job; /* Job ID pointer */
57 char name[255]; /* Printer name */
58 char uri[1024]; /* Printer or job URI */
59 ipp_t *request; /* IPP request */
60 ipp_t *response; /* IPP response */
61 ipp_op_t op; /* Operation */
62 cups_lang_t *language; /* Language */
63 http_encryption_t encryption; /* Encryption? */
64
65
66 /*
67 * Setup to cancel individual print jobs...
68 */
69
70 op = IPP_CANCEL_JOB;
71 job_id = 0;
72 dest = NULL;
73 http = NULL;
74 encryption = cupsEncryption();
75
76 /*
77 * Process command-line arguments...
78 */
79
80 for (i = 1; i < argc; i ++)
81 if (argv[i][0] == '-' && argv[i][1])
82 switch (argv[i][1])
83 {
84 case 'E' : /* Encrypt */
85 #ifdef HAVE_LIBSSL
86 encryption = HTTP_ENCRYPT_REQUIRED;
87
88 if (http)
89 httpEncryption(http, encryption);
90 #else
91 fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n",
92 argv[0]);
93 #endif /* HAVE_LIBSSL */
94 break;
95
96 case 'a' : /* Cancel all jobs */
97 op = IPP_PURGE_JOBS;
98 break;
99
100 case 'h' : /* Connect to host */
101 if (http != NULL)
102 httpClose(http);
103
104 if (argv[i][2] != '\0')
105 http = httpConnectEncrypt(argv[i] + 2, ippPort(), encryption);
106 else
107 {
108 i ++;
109
110 if (i >= argc)
111 {
112 fputs("cancel: Error - expected hostname after \'-h\' option!\n", stderr);
113 return (1);
114 }
115 else
116 http = httpConnectEncrypt(argv[i], ippPort(), encryption);
117 }
118
119 if (http == NULL)
120 {
121 perror("cancel: Unable to connect to server");
122 return (1);
123 }
124 break;
125
126 case 'u' : /* Username */
127 if (argv[i][2] != '\0')
128 cupsSetUser(argv[i] + 2);
129 else
130 {
131 i ++;
132
133 if (i >= argc)
134 {
135 fputs("cancel: Error - expected username after \'-u\' option!\n", stderr);
136 return (1);
137 }
138 else
139 cupsSetUser(argv[i]);
140 }
141 break;
142
143 default :
144 fprintf(stderr, "cancel: Unknown option \'%c\'!\n", argv[i][1]);
145 return (1);
146 }
147 else
148 {
149 /*
150 * Cancel a job or printer...
151 */
152
153 if (isdigit(argv[i][0]))
154 {
155 dest = NULL;
156 op = IPP_CANCEL_JOB;
157 job_id = atoi(argv[i]);
158 }
159 else if (argv[i][0] == '-')
160 {
161 dest = "";
162 job_id = 0;
163 }
164 else
165 {
166 strlcpy(name, argv[i], sizeof(name));
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.23 2002/05/16 13:45:04 mike Exp $".
285 */