]> 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 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * "lprm" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * Contents:
16 *
17 * main() - Parse options and cancel jobs.
18 */
19
20 /*
21 * Include necessary headers...
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include <cups/cups.h>
28 #include <cups/i18n.h>
29 #include <cups/string.h>
30
31
32 /*
33 * 'main()' - Parse options and cancel jobs.
34 */
35
36 int /* O - Exit status */
37 main(int argc, /* I - Number of command-line arguments */
38 char *argv[]) /* I - Command-line arguments */
39 {
40 http_t *http; /* HTTP connection to server */
41 int i; /* Looping var */
42 int job_id; /* Job ID */
43 const char *dest; /* Destination printer */
44 char *instance; /* Pointer to instance name */
45 char uri[1024]; /* Printer or job URI */
46 ipp_t *request; /* IPP request */
47 ipp_t *response; /* IPP response */
48 ipp_op_t op; /* Operation */
49 int num_dests; /* Number of destinations */
50 cups_dest_t *dests, /* Destinations */
51 *defdest; /* Default destination */
52 http_encryption_t encryption; /* Encryption? */
53
54
55 _cupsSetLocale(argv);
56
57 /*
58 * Setup to cancel individual print jobs...
59 */
60
61 op = IPP_CANCEL_JOB;
62 job_id = 0;
63 dest = NULL;
64 response = NULL;
65 http = NULL;
66 encryption = cupsEncryption();
67
68 /*
69 * Open a connection to the server...
70 */
71
72 if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL)
73 {
74 _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n"));
75 return (1);
76 }
77
78 num_dests = cupsGetDests2(http, &dests);
79 defdest = cupsGetDest(NULL, NULL, num_dests, dests);
80 dest = defdest ? defdest->name : NULL;
81
82 /*
83 * Process command-line arguments...
84 */
85
86 for (i = 1; i < argc; i ++)
87 if (argv[i][0] == '-' && argv[i][1] != '\0')
88 switch (argv[i][1])
89 {
90 case 'E' : /* Encrypt */
91 #ifdef HAVE_SSL
92 encryption = HTTP_ENCRYPT_REQUIRED;
93
94 httpEncryption(http, encryption);
95 cupsSetEncryption(encryption);
96 #else
97 _cupsLangPrintf(stderr,
98 _("%s: Sorry, no encryption support compiled in!\n"),
99 argv[0]);
100 #endif /* HAVE_SSL */
101 break;
102
103 case 'P' : /* Cancel jobs on a printer */
104 if (argv[i][2])
105 dest = argv[i] + 2;
106 else
107 {
108 i ++;
109 dest = argv[i];
110 }
111
112 if ((instance = strchr(dest, '/')) != NULL)
113 *instance = '\0';
114
115 if (cupsGetDest(dest, NULL, num_dests, dests) == NULL)
116 {
117 _cupsLangPrintf(stderr,
118 _("%s: Error - unknown destination \"%s\"!\n"),
119 argv[0], dest);
120 cupsFreeDests(num_dests, dests);
121 httpClose(http);
122 return(1);
123 }
124 break;
125
126 case 'U' : /* Username */
127 if (argv[i][2] != '\0')
128 cupsSetUser(argv[i] + 2);
129 else
130 {
131 i ++;
132 if (i >= argc)
133 {
134 _cupsLangPrintf(stderr,
135 _("%s: Error - expected username after "
136 "\'-U\' option!\n"),
137 argv[0]);
138 return (1);
139 }
140
141 cupsSetUser(argv[i]);
142 }
143 break;
144
145 case 'h' : /* Connect to host */
146 if (argv[i][2] != '\0')
147 cupsSetServer(argv[i] + 2);
148 else
149 {
150 i ++;
151
152 if (i >= argc)
153 {
154 _cupsLangPrintf(stderr,
155 _("%s: Error - expected hostname after "
156 "\'-h\' option!\n"),
157 argv[0]);
158 return (1);
159 }
160 else
161 cupsSetServer(argv[i]);
162 }
163
164 httpClose(http);
165 cupsFreeDests(num_dests, dests);
166
167 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
168 encryption)) == NULL)
169 {
170 _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n"));
171 return (1);
172 }
173
174 num_dests = cupsGetDests2(http, &dests);
175 defdest = cupsGetDest(NULL, NULL, num_dests, dests);
176 dest = defdest ? defdest->name : NULL;
177 break;
178
179 default :
180 _cupsLangPrintf(stderr,
181 _("%s: Error - unknown option \'%c\'!\n"),
182 argv[0], argv[i][1]);
183 cupsFreeDests(num_dests, dests);
184 httpClose(http);
185 return (1);
186 }
187 else
188 {
189 /*
190 * Cancel a job or printer...
191 */
192
193 if (isdigit(argv[i][0] & 255) &&
194 cupsGetDest(argv[i], NULL, num_dests, dests) == NULL)
195 {
196 dest = NULL;
197 op = IPP_CANCEL_JOB;
198 job_id = atoi(argv[i]);
199 }
200 else if (!strcmp(argv[i], "-"))
201 {
202 /*
203 * Cancel all jobs
204 */
205
206 op = IPP_PURGE_JOBS;
207 }
208 else
209 {
210 dest = argv[i];
211 job_id = 0;
212 }
213
214 /*
215 * Build an IPP request, which requires the following
216 * attributes:
217 *
218 * attributes-charset
219 * attributes-natural-language
220 * printer-uri + job-id *or* job-uri
221 * [requesting-user-name]
222 */
223
224 request = ippNewRequest(op);
225
226 if (dest)
227 {
228 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
229 "localhost", 0, "/printers/%s", dest);
230 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
231 "printer-uri", NULL, uri);
232 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
233 job_id);
234 }
235 else
236 {
237 sprintf(uri, "ipp://localhost/jobs/%d", job_id);
238 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
239 uri);
240 }
241
242 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
243 "requesting-user-name", NULL, cupsUser());
244
245 /*
246 * Do the request and get back a response...
247 */
248
249 if (op == IPP_PURGE_JOBS)
250 response = cupsDoRequest(http, request, "/admin/");
251 else
252 response = cupsDoRequest(http, request, "/jobs/");
253
254 ippDelete(response);
255
256 if (cupsLastError() > IPP_OK_CONFLICT)
257 {
258 _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString());
259
260 cupsFreeDests(num_dests, dests);
261 httpClose(http);
262 return (1);
263 }
264 }
265
266 /*
267 * If nothing has been canceled yet, cancel the current job on the specified
268 * (or default) printer...
269 */
270
271 if (response == NULL)
272 if (!cupsCancelJob(dest, 0))
273 {
274 _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString());
275 cupsFreeDests(num_dests, dests);
276 httpClose(http);
277 return (1);
278 }
279
280 cupsFreeDests(num_dests, dests);
281 httpClose(http);
282
283 return (0);
284 }
285
286
287 /*
288 * End of "$Id: lprm.c 6649 2007-07-11 21:46:42Z mike $".
289 */