]>
Commit | Line | Data |
---|---|---|
bd0b97ff | 1 | /* |
997fbfa7 | 2 | * "$Id: lprm.c,v 1.21 2002/12/17 18:56:35 swdev Exp $" |
bd0b97ff | 3 | * |
4 | * "lprm" command for the Common UNIX Printing System (CUPS). | |
5 | * | |
997fbfa7 | 6 | * Copyright 1997-2003 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 |
bd0b97ff | 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> | |
bd0b97ff | 35 | |
36 | #include <cups/cups.h> | |
37 | #include <cups/language.h> | |
8c844439 | 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(); | |
bd0b97ff | 74 | |
99cc28d6 | 75 | num_dests = cupsGetDests(&dests); |
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 | ||
a1793153 | 85 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL) |
bd0b97ff | 86 | { |
87 | fputs("lprm: Unable to contact server!\n", stderr); | |
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 */ |
101 | #ifdef HAVE_LIBSSL | |
102 | encryption = HTTP_ENCRYPT_REQUIRED; | |
103 | ||
104 | httpEncryption(http, encryption); | |
105 | #else | |
106 | fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n", | |
107 | argv[0]); | |
108 | #endif /* HAVE_LIBSSL */ | |
109 | break; | |
110 | ||
bd0b97ff | 111 | case 'P' : /* Cancel jobs on a printer */ |
112 | if (argv[i][2]) | |
113 | dest = argv[i] + 2; | |
114 | else | |
115 | { | |
116 | i ++; | |
117 | dest = argv[i]; | |
118 | } | |
79e9fc64 | 119 | |
120 | if ((instance = strchr(dest, '/')) != NULL) | |
121 | *instance = '\0'; | |
504a6b20 | 122 | |
123 | if (cupsGetDest(dest, NULL, num_dests, dests) == NULL) | |
124 | { | |
125 | fprintf(stderr, "lprm: Unknown destination \"%s\"!\n", dest); | |
126 | cupsFreeDests(num_dests, dests); | |
127 | httpClose(http); | |
128 | return(1); | |
129 | } | |
bd0b97ff | 130 | break; |
131 | ||
132 | default : | |
133 | fprintf(stderr, "lprm: Unknown option \'%c\'!\n", argv[i][1]); | |
99cc28d6 | 134 | cupsFreeDests(num_dests, dests); |
135 | httpClose(http); | |
bd0b97ff | 136 | return (1); |
137 | } | |
138 | else | |
139 | { | |
140 | /* | |
141 | * Cancel a job or printer... | |
142 | */ | |
143 | ||
4ae5a89f | 144 | if (isdigit(argv[i][0]) && cupsGetDest(argv[i], NULL, num_dests, dests) == NULL) |
bd0b97ff | 145 | { |
146 | dest = NULL; | |
147 | op = IPP_CANCEL_JOB; | |
148 | job_id = atoi(argv[i]); | |
149 | } | |
150 | else if (strcmp(argv[i], "-") == 0) | |
151 | { | |
152 | /* | |
153 | * Cancel all jobs | |
154 | */ | |
155 | ||
156 | op = IPP_PURGE_JOBS; | |
157 | } | |
158 | else | |
50b9556e | 159 | { |
160 | dest = argv[i]; | |
bd0b97ff | 161 | job_id = 0; |
50b9556e | 162 | } |
bd0b97ff | 163 | |
164 | /* | |
165 | * Build an IPP request, which requires the following | |
166 | * attributes: | |
167 | * | |
168 | * attributes-charset | |
169 | * attributes-natural-language | |
170 | * printer-uri + job-id *or* job-uri | |
171 | * [requesting-user-name] | |
172 | */ | |
173 | ||
174 | request = ippNew(); | |
175 | ||
176 | request->request.op.operation_id = op; | |
177 | request->request.op.request_id = 1; | |
178 | ||
179 | language = cupsLangDefault(); | |
180 | ||
181 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET, | |
182 | "attributes-charset", NULL, cupsLangEncoding(language)); | |
183 | ||
184 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE, | |
185 | "attributes-natural-language", NULL, language->language); | |
186 | ||
187 | if (dest) | |
188 | { | |
970017a4 | 189 | snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", dest); |
bd0b97ff | 190 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
191 | "printer-uri", NULL, uri); | |
192 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
193 | job_id); | |
194 | } | |
195 | else | |
196 | { | |
f8ab8b97 | 197 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); |
bd0b97ff | 198 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, |
199 | uri); | |
200 | } | |
201 | ||
8dbf3e6a | 202 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, |
203 | "requesting-user-name", NULL, cupsUser()); | |
204 | ||
bd0b97ff | 205 | /* |
206 | * Do the request and get back a response... | |
207 | */ | |
208 | ||
209 | if (op == IPP_PURGE_JOBS) | |
210 | response = cupsDoRequest(http, request, "/admin/"); | |
211 | else | |
212 | response = cupsDoRequest(http, request, "/jobs/"); | |
213 | ||
214 | if (response != NULL) | |
67e0502b | 215 | { |
8dbf3e6a | 216 | switch (response->request.status.status_code) |
217 | { | |
218 | case IPP_NOT_FOUND : | |
219 | fputs("lprm: Job or printer not found!\n", stderr); | |
220 | break; | |
221 | case IPP_NOT_AUTHORIZED : | |
222 | fputs("lprm: Not authorized to lprm job(s)!\n", stderr); | |
223 | break; | |
224 | case IPP_FORBIDDEN : | |
225 | fprintf(stderr, "lprm: You don't own job ID %d!\n", job_id); | |
226 | break; | |
227 | default : | |
228 | if (response->request.status.status_code > IPP_OK_CONFLICT) | |
229 | fputs("lprm: Unable to lprm job(s)!\n", stderr); | |
230 | break; | |
231 | } | |
67e0502b | 232 | |
50b9556e | 233 | if (response->request.status.status_code > IPP_OK_CONFLICT) |
234 | { | |
235 | ippDelete(response); | |
236 | cupsFreeDests(num_dests, dests); | |
237 | httpClose(http); | |
238 | return (1); | |
239 | } | |
240 | ||
bd0b97ff | 241 | ippDelete(response); |
67e0502b | 242 | } |
bd0b97ff | 243 | else |
244 | { | |
245 | fputs("lprm: Unable to cancel job(s)!\n", stderr); | |
99cc28d6 | 246 | cupsFreeDests(num_dests, dests); |
247 | httpClose(http); | |
bd0b97ff | 248 | return (1); |
249 | } | |
250 | } | |
251 | ||
252 | /* | |
253 | * If nothing has been cancelled yet, cancel the current job on the specified | |
254 | * (or default) printer... | |
255 | */ | |
256 | ||
257 | if (response == NULL) | |
258 | if (!cupsCancelJob(dest, 0)) | |
259 | { | |
260 | fputs("lprm: Unable to cancel job(s)!\n", stderr); | |
99cc28d6 | 261 | cupsFreeDests(num_dests, dests); |
262 | httpClose(http); | |
bd0b97ff | 263 | return (1); |
264 | } | |
265 | ||
99cc28d6 | 266 | cupsFreeDests(num_dests, dests); |
267 | httpClose(http); | |
268 | ||
bd0b97ff | 269 | return (0); |
270 | } | |
271 | ||
272 | ||
273 | /* | |
997fbfa7 | 274 | * End of "$Id: lprm.c,v 1.21 2002/12/17 18:56:35 swdev Exp $". |
bd0b97ff | 275 | */ |