]>
Commit | Line | Data |
---|---|---|
fc8c8467 | 1 | /* |
17438bf4 | 2 | * "$Id: cancel.c,v 1.23 2002/05/16 13:45:04 mike Exp $" |
fc8c8467 | 3 | * |
4 | * "cancel" command for the Common UNIX Printing System (CUPS). | |
5 | * | |
efb2f309 | 6 | * Copyright 1997-2002 by Easy Software Products. |
fc8c8467 | 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 |
fc8c8467 | 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> | |
b310a06a | 35 | #include <ctype.h> |
36 | ||
1c9e0181 | 37 | #include <config.h> |
fc8c8467 | 38 | #include <cups/cups.h> |
f9a8da2d | 39 | #include <cups/string.h> |
b310a06a | 40 | #include <cups/language.h> |
fc8c8467 | 41 | |
42 | ||
43 | /* | |
44 | * 'main()' - Parse options and cancel jobs. | |
45 | */ | |
46 | ||
b310a06a | 47 | int /* O - Exit status */ |
48 | main(int argc, /* I - Number of command-line arguments */ | |
49 | char *argv[]) /* I - Command-line arguments */ | |
fc8c8467 | 50 | { |
b310a06a | 51 | http_t *http; /* HTTP connection to server */ |
fc8c8467 | 52 | int i; /* Looping var */ |
53 | int job_id; /* Job ID */ | |
d7a02a53 | 54 | char *dest, /* Destination printer */ |
50b9556e | 55 | *host, /* Host name */ |
56 | *job; /* Job ID pointer */ | |
b310a06a | 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 */ | |
1c9e0181 | 63 | http_encryption_t encryption; /* Encryption? */ |
fc8c8467 | 64 | |
fc8c8467 | 65 | |
b310a06a | 66 | /* |
67 | * Setup to cancel individual print jobs... | |
68 | */ | |
fc8c8467 | 69 | |
1c9e0181 | 70 | op = IPP_CANCEL_JOB; |
71 | job_id = 0; | |
72 | dest = NULL; | |
73 | http = NULL; | |
74 | encryption = cupsEncryption(); | |
fc8c8467 | 75 | |
b310a06a | 76 | /* |
77 | * Process command-line arguments... | |
78 | */ | |
fc8c8467 | 79 | |
b310a06a | 80 | for (i = 1; i < argc; i ++) |
50b9556e | 81 | if (argv[i][0] == '-' && argv[i][1]) |
b310a06a | 82 | switch (argv[i][1]) |
83 | { | |
1c9e0181 | 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 | ||
b310a06a | 96 | case 'a' : /* Cancel all jobs */ |
97 | op = IPP_PURGE_JOBS; | |
fc8c8467 | 98 | break; |
99 | ||
808dd474 | 100 | case 'h' : /* Connect to host */ |
8dbf3e6a | 101 | if (http != NULL) |
102 | httpClose(http); | |
808dd474 | 103 | |
104 | if (argv[i][2] != '\0') | |
a1793153 | 105 | http = httpConnectEncrypt(argv[i] + 2, ippPort(), encryption); |
808dd474 | 106 | else |
107 | { | |
108 | i ++; | |
8a4fe7c7 | 109 | |
110 | if (i >= argc) | |
111 | { | |
2558a535 | 112 | fputs("cancel: Error - expected hostname after \'-h\' option!\n", stderr); |
8a4fe7c7 | 113 | return (1); |
114 | } | |
115 | else | |
a1793153 | 116 | http = httpConnectEncrypt(argv[i], ippPort(), encryption); |
808dd474 | 117 | } |
118 | ||
119 | if (http == NULL) | |
120 | { | |
121 | perror("cancel: Unable to connect to server"); | |
122 | return (1); | |
123 | } | |
124 | break; | |
125 | ||
2558a535 | 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 | ||
fc8c8467 | 143 | default : |
b310a06a | 144 | fprintf(stderr, "cancel: Unknown option \'%c\'!\n", argv[i][1]); |
fc8c8467 | 145 | return (1); |
146 | } | |
b310a06a | 147 | else |
fc8c8467 | 148 | { |
b310a06a | 149 | /* |
150 | * Cancel a job or printer... | |
151 | */ | |
fc8c8467 | 152 | |
b310a06a | 153 | if (isdigit(argv[i][0])) |
154 | { | |
155 | dest = NULL; | |
156 | op = IPP_CANCEL_JOB; | |
157 | job_id = atoi(argv[i]); | |
158 | } | |
50b9556e | 159 | else if (argv[i][0] == '-') |
160 | { | |
161 | dest = ""; | |
162 | job_id = 0; | |
163 | } | |
b310a06a | 164 | else |
165 | { | |
17438bf4 | 166 | strlcpy(name, argv[i], sizeof(name)); |
50b9556e | 167 | |
b310a06a | 168 | dest = name; |
169 | job_id = 0; | |
50b9556e | 170 | |
171 | if ((job = strrchr(name, '-')) != NULL) | |
172 | if (isdigit(job[1])) | |
173 | { | |
174 | *job++ = '\0'; | |
175 | job_id = atoi(job); | |
176 | } | |
177 | ||
b310a06a | 178 | if (job_id) |
179 | op = IPP_CANCEL_JOB; | |
808dd474 | 180 | |
181 | if ((host = strchr(name, '@')) != NULL) | |
182 | { | |
183 | /* | |
184 | * Reconnect to the named host... | |
185 | */ | |
186 | ||
8dbf3e6a | 187 | if (http != NULL) |
188 | httpClose(http); | |
808dd474 | 189 | |
190 | *host++ = '\0'; | |
191 | ||
a1793153 | 192 | if ((http = httpConnectEncrypt(host, ippPort(), encryption)) == NULL) |
808dd474 | 193 | { |
194 | perror("cancel: Unable to connect to server"); | |
195 | return (1); | |
196 | } | |
197 | } | |
b310a06a | 198 | } |
fc8c8467 | 199 | |
8dbf3e6a | 200 | /* |
201 | * Open a connection to the server... | |
202 | */ | |
203 | ||
204 | if (http == NULL) | |
a1793153 | 205 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), |
206 | encryption)) == NULL) | |
8dbf3e6a | 207 | { |
208 | fputs("cancel: Unable to contact server!\n", stderr); | |
209 | return (1); | |
210 | } | |
211 | ||
b310a06a | 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 | |
8dbf3e6a | 219 | * [requesting-user-name] |
b310a06a | 220 | */ |
fc8c8467 | 221 | |
b310a06a | 222 | request = ippNew(); |
fc8c8467 | 223 | |
b310a06a | 224 | request->request.op.operation_id = op; |
225 | request->request.op.request_id = 1; | |
fc8c8467 | 226 | |
b310a06a | 227 | language = cupsLangDefault(); |
fc8c8467 | 228 | |
b310a06a | 229 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, |
230 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
fc8c8467 | 231 | |
b310a06a | 232 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, |
233 | "attributes-natural-language", NULL, language->language); | |
fc8c8467 | 234 | |
b310a06a | 235 | if (dest) |
236 | { | |
c5808f70 | 237 | snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", dest); |
b310a06a | 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 | { | |
1fd07458 | 245 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); |
b310a06a | 246 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, |
247 | uri); | |
248 | } | |
fc8c8467 | 249 | |
8dbf3e6a | 250 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, |
251 | "requesting-user-name", NULL, cupsUser()); | |
252 | ||
b310a06a | 253 | /* |
254 | * Do the request and get back a response... | |
255 | */ | |
fc8c8467 | 256 | |
b310a06a | 257 | if (op == IPP_PURGE_JOBS) |
258 | response = cupsDoRequest(http, request, "/admin/"); | |
259 | else | |
260 | response = cupsDoRequest(http, request, "/jobs/"); | |
fc8c8467 | 261 | |
96ecd6f0 | 262 | if (response == NULL || |
263 | response->request.status.status_code > IPP_OK_CONFLICT) | |
8cf354e9 | 264 | { |
96ecd6f0 | 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); | |
8cf354e9 | 272 | |
b310a06a | 273 | return (1); |
274 | } | |
96ecd6f0 | 275 | |
276 | ippDelete(response); | |
fc8c8467 | 277 | } |
fc8c8467 | 278 | |
279 | return (0); | |
280 | } | |
281 | ||
282 | ||
283 | /* | |
17438bf4 | 284 | * End of "$Id: cancel.c,v 1.23 2002/05/16 13:45:04 mike Exp $". |
fc8c8467 | 285 | */ |