]> git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lprm.c
Load cups into easysw/current.
[thirdparty/cups.git] / berkeley / lprm.c
1 /*
2 * "$Id: lprm.c 5926 2006-09-05 20:45:47Z mike $"
3 *
4 * "lprm" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2006 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 USA
19 *
20 * Voice: (301) 373-9600
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
36 #include <cups/cups.h>
37 #include <cups/i18n.h>
38 #include <cups/string.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 const char *dest; /* Destination printer */
53 char *instance; /* Pointer to instance name */
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 int num_dests; /* Number of destinations */
59 cups_dest_t *dests, /* Destinations */
60 *defdest; /* Default destination */
61 http_encryption_t encryption; /* Encryption? */
62
63
64 _cupsSetLocale(argv);
65
66 /*
67 * Setup to cancel individual print jobs...
68 */
69
70 op = IPP_CANCEL_JOB;
71 job_id = 0;
72 dest = NULL;
73 response = NULL;
74 http = NULL;
75 encryption = cupsEncryption();
76
77 /*
78 * Open a connection to the server...
79 */
80
81 if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL)
82 {
83 _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n"));
84 return (1);
85 }
86
87 num_dests = cupsGetDests2(http, &dests);
88 defdest = cupsGetDest(NULL, NULL, num_dests, dests);
89 dest = defdest ? defdest->name : NULL;
90
91 /*
92 * Process command-line arguments...
93 */
94
95 for (i = 1; i < argc; i ++)
96 if (argv[i][0] == '-' && argv[i][1] != '\0')
97 switch (argv[i][1])
98 {
99 case 'E' : /* Encrypt */
100 #ifdef HAVE_SSL
101 encryption = HTTP_ENCRYPT_REQUIRED;
102
103 httpEncryption(http, encryption);
104 cupsSetEncryption(encryption);
105 #else
106 _cupsLangPrintf(stderr,
107 _("%s: Sorry, no encryption support compiled in!\n"),
108 argv[0]);
109 #endif /* HAVE_SSL */
110 break;
111
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 }
120
121 if ((instance = strchr(dest, '/')) != NULL)
122 *instance = '\0';
123
124 if (cupsGetDest(dest, NULL, num_dests, dests) == NULL)
125 {
126 _cupsLangPrintf(stderr,
127 _("%s: Error - unknown destination \"%s\"!\n"),
128 argv[0], dest);
129 cupsFreeDests(num_dests, dests);
130 httpClose(http);
131 return(1);
132 }
133 break;
134
135 case 'U' : /* Username */
136 if (argv[i][2] != '\0')
137 cupsSetUser(argv[i] + 2);
138 else
139 {
140 i ++;
141 if (i >= argc)
142 {
143 _cupsLangPrintf(stderr,
144 _("%s: Error - expected username after "
145 "\'-U\' option!\n"),
146 argv[0]);
147 return (1);
148 }
149
150 cupsSetUser(argv[i]);
151 }
152 break;
153
154 case 'h' : /* Connect to host */
155 if (argv[i][2] != '\0')
156 cupsSetServer(argv[i] + 2);
157 else
158 {
159 i ++;
160
161 if (i >= argc)
162 {
163 _cupsLangPrintf(stderr,
164 _("%s: Error - expected hostname after "
165 "\'-h\' option!\n"),
166 argv[0]);
167 return (1);
168 }
169 else
170 cupsSetServer(argv[i]);
171 }
172
173 httpClose(http);
174 cupsFreeDests(num_dests, dests);
175
176 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
177 encryption)) == NULL)
178 {
179 _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n"));
180 return (1);
181 }
182
183 num_dests = cupsGetDests2(http, &dests);
184 defdest = cupsGetDest(NULL, NULL, num_dests, dests);
185 dest = defdest ? defdest->name : NULL;
186 break;
187
188 default :
189 _cupsLangPrintf(stderr,
190 _("%s: Error - unknown option \'%c\'!\n"),
191 argv[0], argv[i][1]);
192 cupsFreeDests(num_dests, dests);
193 httpClose(http);
194 return (1);
195 }
196 else
197 {
198 /*
199 * Cancel a job or printer...
200 */
201
202 if (isdigit(argv[i][0] & 255) &&
203 cupsGetDest(argv[i], NULL, num_dests, dests) == NULL)
204 {
205 dest = NULL;
206 op = IPP_CANCEL_JOB;
207 job_id = atoi(argv[i]);
208 }
209 else if (!strcmp(argv[i], "-"))
210 {
211 /*
212 * Cancel all jobs
213 */
214
215 op = IPP_PURGE_JOBS;
216 }
217 else
218 {
219 dest = argv[i];
220 job_id = 0;
221 }
222
223 /*
224 * Build an IPP request, which requires the following
225 * attributes:
226 *
227 * attributes-charset
228 * attributes-natural-language
229 * printer-uri + job-id *or* job-uri
230 * [requesting-user-name]
231 */
232
233 request = ippNewRequest(op);
234
235 if (dest)
236 {
237 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
238 "localhost", 0, "/printers/%s", dest);
239 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
240 "printer-uri", NULL, uri);
241 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
242 job_id);
243 }
244 else
245 {
246 sprintf(uri, "ipp://localhost/jobs/%d", job_id);
247 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
248 uri);
249 }
250
251 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
252 "requesting-user-name", NULL, cupsUser());
253
254 /*
255 * Do the request and get back a response...
256 */
257
258 if (op == IPP_PURGE_JOBS)
259 response = cupsDoRequest(http, request, "/admin/");
260 else
261 response = cupsDoRequest(http, request, "/jobs/");
262
263 ippDelete(response);
264
265 if (cupsLastError() > IPP_OK_CONFLICT)
266 {
267 _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString());
268
269 cupsFreeDests(num_dests, dests);
270 httpClose(http);
271 return (1);
272 }
273 }
274
275 /*
276 * If nothing has been canceled yet, cancel the current job on the specified
277 * (or default) printer...
278 */
279
280 if (response == NULL)
281 if (!cupsCancelJob(dest, 0))
282 {
283 _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString());
284 cupsFreeDests(num_dests, dests);
285 httpClose(http);
286 return (1);
287 }
288
289 cupsFreeDests(num_dests, dests);
290 httpClose(http);
291
292 return (0);
293 }
294
295
296 /*
297 * End of "$Id: lprm.c 5926 2006-09-05 20:45:47Z mike $".
298 */