]>
Commit | Line | Data |
---|---|---|
bd0b97ff | 1 | /* |
c9d3f842 | 2 | * "$Id$" |
bd0b97ff | 3 | * |
4 | * "lprm" command for the Common UNIX Printing System (CUPS). | |
5 | * | |
c9d3f842 | 6 | * Copyright 1997-2005 by Easy Software Products. |
bd0b97ff | 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 | |
8784b6a6 | 17 | * 44141 Airport View Drive, Suite 204 |
c9d3f842 | 18 | * Hollywood, Maryland 20636 USA |
bd0b97ff | 19 | * |
9639c4de | 20 | * Voice: (301) 373-9600 |
bd0b97ff | 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> | |
bd0b97ff | 35 | |
36 | #include <cups/cups.h> | |
3194190a | 37 | #include <cups/i18n.h> |
7680b71d | 38 | #include <cups/string.h> |
bd0b97ff | 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 */ | |
48574397 | 52 | const char *dest; /* Destination printer */ |
79e9fc64 | 53 | char *instance; /* Pointer to instance name */ |
bd0b97ff | 54 | char uri[1024]; /* Printer or job URI */ |
55 | ipp_t *request; /* IPP request */ | |
56 | ipp_t *response; /* IPP response */ | |
57 | ipp_op_t op; /* Operation */ | |
58 | cups_lang_t *language; /* Language */ | |
99cc28d6 | 59 | int num_dests; /* Number of destinations */ |
60 | cups_dest_t *dests; /* Destinations */ | |
1c9e0181 | 61 | http_encryption_t encryption; /* Encryption? */ |
bd0b97ff | 62 | |
63 | ||
64 | /* | |
65 | * Setup to cancel individual print jobs... | |
66 | */ | |
67 | ||
1c9e0181 | 68 | op = IPP_CANCEL_JOB; |
69 | job_id = 0; | |
70 | dest = NULL; | |
71 | response = NULL; | |
72 | http = NULL; | |
73 | encryption = cupsEncryption(); | |
3194190a | 74 | language = cupsLangDefault(); |
75 | num_dests = cupsGetDests(&dests); | |
99cc28d6 | 76 | |
77 | for (i = 0; i < num_dests; i ++) | |
78 | if (dests[i].is_default) | |
79 | dest = dests[i].name; | |
80 | ||
bd0b97ff | 81 | /* |
82 | * Open a connection to the server... | |
83 | */ | |
84 | ||
b5cb0608 | 85 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL) |
bd0b97ff | 86 | { |
3194190a | 87 | _cupsLangPuts(stderr, language, _("lprm: Unable to contact server!\n")); |
99cc28d6 | 88 | cupsFreeDests(num_dests, dests); |
bd0b97ff | 89 | return (1); |
90 | } | |
91 | ||
92 | /* | |
93 | * Process command-line arguments... | |
94 | */ | |
95 | ||
96 | for (i = 1; i < argc; i ++) | |
97 | if (argv[i][0] == '-' && argv[i][1] != '\0') | |
98 | switch (argv[i][1]) | |
99 | { | |
1c9e0181 | 100 | case 'E' : /* Encrypt */ |
bcf61448 | 101 | #ifdef HAVE_SSL |
1c9e0181 | 102 | encryption = HTTP_ENCRYPT_REQUIRED; |
103 | ||
104 | httpEncryption(http, encryption); | |
105 | #else | |
3194190a | 106 | _cupsLangPrintf(stderr, language, |
107 | _("%s: Sorry, no encryption support compiled in!\n"), | |
108 | argv[0]); | |
bcf61448 | 109 | #endif /* HAVE_SSL */ |
1c9e0181 | 110 | break; |
111 | ||
bd0b97ff | 112 | case 'P' : /* Cancel jobs on a printer */ |
113 | if (argv[i][2]) | |
114 | dest = argv[i] + 2; | |
115 | else | |
116 | { | |
117 | i ++; | |
118 | dest = argv[i]; | |
119 | } | |
79e9fc64 | 120 | |
121 | if ((instance = strchr(dest, '/')) != NULL) | |
122 | *instance = '\0'; | |
727e6d14 | 123 | |
124 | if (cupsGetDest(dest, NULL, num_dests, dests) == NULL) | |
125 | { | |
3194190a | 126 | _cupsLangPrintf(stderr, language, |
127 | _("lprm: Unknown destination \"%s\"!\n"), dest); | |
727e6d14 | 128 | cupsFreeDests(num_dests, dests); |
129 | httpClose(http); | |
130 | return(1); | |
131 | } | |
bd0b97ff | 132 | break; |
133 | ||
134 | default : | |
3194190a | 135 | _cupsLangPrintf(stderr, language, |
136 | _("lprm: Unknown option \'%c\'!\n"), argv[i][1]); | |
99cc28d6 | 137 | cupsFreeDests(num_dests, dests); |
138 | httpClose(http); | |
bd0b97ff | 139 | return (1); |
140 | } | |
141 | else | |
142 | { | |
143 | /* | |
144 | * Cancel a job or printer... | |
145 | */ | |
146 | ||
64bbab09 | 147 | if (isdigit(argv[i][0] & 255) && |
148 | cupsGetDest(argv[i], NULL, num_dests, dests) == NULL) | |
bd0b97ff | 149 | { |
150 | dest = NULL; | |
151 | op = IPP_CANCEL_JOB; | |
152 | job_id = atoi(argv[i]); | |
153 | } | |
154 | else if (strcmp(argv[i], "-") == 0) | |
155 | { | |
156 | /* | |
157 | * Cancel all jobs | |
158 | */ | |
159 | ||
160 | op = IPP_PURGE_JOBS; | |
161 | } | |
162 | else | |
50b9556e | 163 | { |
164 | dest = argv[i]; | |
bd0b97ff | 165 | job_id = 0; |
50b9556e | 166 | } |
bd0b97ff | 167 | |
168 | /* | |
169 | * Build an IPP request, which requires the following | |
170 | * attributes: | |
171 | * | |
172 | * attributes-charset | |
173 | * attributes-natural-language | |
174 | * printer-uri + job-id *or* job-uri | |
175 | * [requesting-user-name] | |
176 | */ | |
177 | ||
178 | request = ippNew(); | |
179 | ||
0a3ac972 | 180 | request->request.op.operation_id = op; |
181 | request->request.op.request_id = 1; | |
bd0b97ff | 182 | |
bd0b97ff | 183 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
184 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
185 | ||
186 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
187 | "attributes-natural-language", NULL, language->language); | |
188 | ||
189 | if (dest) | |
190 | { | |
911278f6 | 191 | httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0, |
192 | "/printers/%s", dest); | |
bd0b97ff | 193 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
194 | "printer-uri", NULL, uri); | |
195 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
196 | job_id); | |
197 | } | |
198 | else | |
199 | { | |
f8ab8b97 | 200 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); |
bd0b97ff | 201 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, |
202 | uri); | |
203 | } | |
204 | ||
8dbf3e6a | 205 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, |
206 | "requesting-user-name", NULL, cupsUser()); | |
207 | ||
bd0b97ff | 208 | /* |
209 | * Do the request and get back a response... | |
210 | */ | |
211 | ||
212 | if (op == IPP_PURGE_JOBS) | |
213 | response = cupsDoRequest(http, request, "/admin/"); | |
214 | else | |
215 | response = cupsDoRequest(http, request, "/jobs/"); | |
216 | ||
217 | if (response != NULL) | |
67e0502b | 218 | { |
0a3ac972 | 219 | switch (response->request.status.status_code) |
8dbf3e6a | 220 | { |
221 | case IPP_NOT_FOUND : | |
3194190a | 222 | _cupsLangPuts(stderr, language, |
223 | _("lprm: Job or printer not found!\n")); | |
8dbf3e6a | 224 | break; |
225 | case IPP_NOT_AUTHORIZED : | |
3194190a | 226 | _cupsLangPuts(stderr, language, |
227 | _("lprm: Not authorized to lprm job(s)!\n")); | |
8dbf3e6a | 228 | break; |
229 | case IPP_FORBIDDEN : | |
3194190a | 230 | _cupsLangPrintf(stderr, language, |
231 | _("lprm: You don't own job ID %d!\n"), job_id); | |
8dbf3e6a | 232 | break; |
233 | default : | |
0a3ac972 | 234 | if (response->request.status.status_code > IPP_OK_CONFLICT) |
3194190a | 235 | _cupsLangPuts(stderr, language, |
236 | _("lprm: Unable to lprm job(s)!\n")); | |
8dbf3e6a | 237 | break; |
238 | } | |
67e0502b | 239 | |
0a3ac972 | 240 | if (response->request.status.status_code > IPP_OK_CONFLICT) |
50b9556e | 241 | { |
242 | ippDelete(response); | |
243 | cupsFreeDests(num_dests, dests); | |
244 | httpClose(http); | |
245 | return (1); | |
246 | } | |
247 | ||
bd0b97ff | 248 | ippDelete(response); |
67e0502b | 249 | } |
bd0b97ff | 250 | else |
251 | { | |
3194190a | 252 | _cupsLangPuts(stderr, language, |
253 | _("lprm: Unable to cancel job(s)!\n")); | |
99cc28d6 | 254 | cupsFreeDests(num_dests, dests); |
255 | httpClose(http); | |
bd0b97ff | 256 | return (1); |
257 | } | |
258 | } | |
259 | ||
260 | /* | |
261 | * If nothing has been cancelled yet, cancel the current job on the specified | |
262 | * (or default) printer... | |
263 | */ | |
264 | ||
265 | if (response == NULL) | |
266 | if (!cupsCancelJob(dest, 0)) | |
267 | { | |
3194190a | 268 | _cupsLangPuts(stderr, language, |
269 | _("lprm: Unable to cancel job(s)!\n")); | |
99cc28d6 | 270 | cupsFreeDests(num_dests, dests); |
271 | httpClose(http); | |
bd0b97ff | 272 | return (1); |
273 | } | |
274 | ||
99cc28d6 | 275 | cupsFreeDests(num_dests, dests); |
276 | httpClose(http); | |
277 | ||
bd0b97ff | 278 | return (0); |
279 | } | |
280 | ||
281 | ||
282 | /* | |
c9d3f842 | 283 | * End of "$Id$". |
bd0b97ff | 284 | */ |