]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cancel.c
Y2k copyright changes.
[thirdparty/cups.git] / systemv / cancel.c
1 /*
2 * "$Id: cancel.c,v 1.14 2000/01/04 13:46:11 mike Exp $"
3 *
4 * "cancel" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2000 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 <cups/cups.h>
38 #include <cups/language.h>
39
40
41 /*
42 * 'main()' - Parse options and cancel jobs.
43 */
44
45 int /* O - Exit status */
46 main(int argc, /* I - Number of command-line arguments */
47 char *argv[]) /* I - Command-line arguments */
48 {
49 http_t *http; /* HTTP connection to server */
50 int i; /* Looping var */
51 int job_id; /* Job ID */
52 char *dest, /* Destination printer */
53 *host; /* Host name */
54 char name[255]; /* Printer name */
55 char uri[1024]; /* Printer or job URI */
56 ipp_t *request; /* IPP request */
57 ipp_t *response; /* IPP response */
58 ipp_op_t op; /* Operation */
59 cups_lang_t *language; /* Language */
60
61
62 /*
63 * Setup to cancel individual print jobs...
64 */
65
66 op = IPP_CANCEL_JOB;
67 job_id = 0;
68 dest = NULL;
69 http = NULL;
70
71 /*
72 * Process command-line arguments...
73 */
74
75 for (i = 1; i < argc; i ++)
76 if (argv[i][0] == '-')
77 switch (argv[i][1])
78 {
79 case 'a' : /* Cancel all jobs */
80 op = IPP_PURGE_JOBS;
81 break;
82
83 case 'h' : /* Connect to host */
84 if (http != NULL)
85 httpClose(http);
86
87 if (argv[i][2] != '\0')
88 http = httpConnect(argv[i] + 2, ippPort());
89 else
90 {
91 i ++;
92
93 if (i >= argc)
94 {
95 fputs("Error: need hostname after \'-h\' option!\n", stderr);
96 return (1);
97 }
98 else
99 http = httpConnect(argv[i], ippPort());
100 }
101
102 if (http == NULL)
103 {
104 perror("cancel: Unable to connect to server");
105 return (1);
106 }
107 break;
108
109 default :
110 fprintf(stderr, "cancel: Unknown option \'%c\'!\n", argv[i][1]);
111 return (1);
112 }
113 else
114 {
115 /*
116 * Cancel a job or printer...
117 */
118
119 if (isdigit(argv[i][0]))
120 {
121 dest = NULL;
122 op = IPP_CANCEL_JOB;
123 job_id = atoi(argv[i]);
124 }
125 else
126 {
127 dest = name;
128 job_id = 0;
129 sscanf(argv[i], "%254[^-]-%d", name, &job_id);
130 if (job_id)
131 op = IPP_CANCEL_JOB;
132
133 if ((host = strchr(name, '@')) != NULL)
134 {
135 /*
136 * Reconnect to the named host...
137 */
138
139 if (http != NULL)
140 httpClose(http);
141
142 *host++ = '\0';
143
144 if ((http = httpConnect(host, ippPort())) == NULL)
145 {
146 perror("cancel: Unable to connect to server");
147 return (1);
148 }
149 }
150 }
151
152 /*
153 * Open a connection to the server...
154 */
155
156 if (http == NULL)
157 if ((http = httpConnect(cupsServer(), ippPort())) == NULL)
158 {
159 fputs("cancel: Unable to contact server!\n", stderr);
160 return (1);
161 }
162
163 /*
164 * Build an IPP request, which requires the following
165 * attributes:
166 *
167 * attributes-charset
168 * attributes-natural-language
169 * printer-uri + job-id *or* job-uri
170 * [requesting-user-name]
171 */
172
173 request = ippNew();
174
175 request->request.op.operation_id = op;
176 request->request.op.request_id = 1;
177
178 language = cupsLangDefault();
179
180 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
181 "attributes-charset", NULL, cupsLangEncoding(language));
182
183 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
184 "attributes-natural-language", NULL, language->language);
185
186 if (dest)
187 {
188 snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", dest);
189 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
190 "printer-uri", NULL, uri);
191 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
192 job_id);
193 }
194 else
195 {
196 sprintf(uri, "ipp://localhost/jobs/%d", job_id);
197 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
198 uri);
199 }
200
201 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
202 "requesting-user-name", NULL, cupsUser());
203
204 /*
205 * Do the request and get back a response...
206 */
207
208 if (op == IPP_PURGE_JOBS)
209 response = cupsDoRequest(http, request, "/admin/");
210 else
211 response = cupsDoRequest(http, request, "/jobs/");
212
213 if (response != NULL)
214 {
215 switch (response->request.status.status_code)
216 {
217 case IPP_NOT_FOUND :
218 fputs("cancel: Job or printer not found!\n", stderr);
219 break;
220 case IPP_NOT_AUTHORIZED :
221 fputs("cancel: Not authorized to cancel job(s)!\n", stderr);
222 break;
223 case IPP_FORBIDDEN :
224 fprintf(stderr, "cancel: You don't own job ID %d!\n", job_id);
225 break;
226 default :
227 if (response->request.status.status_code > IPP_OK_CONFLICT)
228 fputs("cancel: Unable to cancel job(s)!\n", stderr);
229 break;
230 }
231
232 ippDelete(response);
233 }
234 else
235 {
236 fputs("cancel: Unable to cancel job(s)!\n", stderr);
237 return (1);
238 }
239 }
240
241 return (0);
242 }
243
244
245 /*
246 * End of "$Id: cancel.c,v 1.14 2000/01/04 13:46:11 mike Exp $".
247 */