]> git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpr.c
418ef4b5545d91acdc3e49044700850f8367f8c5
[thirdparty/cups.git] / berkeley / lpr.c
1 /*
2 * "lpr" command for CUPS.
3 *
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
6 *
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
12 */
13
14 /*
15 * Include necessary headers...
16 */
17
18 #include <cups/cups-private.h>
19
20
21 /*
22 * 'main()' - Parse options and send files for printing.
23 */
24
25 int
26 main(int argc, /* I - Number of command-line arguments */
27 char *argv[]) /* I - Command-line arguments */
28 {
29 int i, j; /* Looping var */
30 int job_id; /* Job ID */
31 char ch; /* Option character */
32 char *printer, /* Destination printer or class */
33 *instance, /* Instance */
34 *opt; /* Option pointer */
35 const char *title, /* Job title */
36 *val; /* Environment variable name */
37 int num_copies; /* Number of copies per file */
38 int num_files; /* Number of files to print */
39 const char *files[1000]; /* Files to print */
40 cups_dest_t *dest; /* Selected destination */
41 int num_options; /* Number of options */
42 cups_option_t *options; /* Options */
43 int deletefile; /* Delete file after print? */
44 char buffer[8192]; /* Copy buffer */
45
46
47 _cupsSetLocale(argv);
48
49 deletefile = 0;
50 printer = NULL;
51 dest = NULL;
52 num_options = 0;
53 options = NULL;
54 num_files = 0;
55 title = NULL;
56
57 for (i = 1; i < argc; i ++)
58 {
59 if (argv[i][0] == '-')
60 {
61 for (opt = argv[i] + 1; *opt; opt ++)
62 {
63 switch (ch = *opt)
64 {
65 case 'E' : /* Encrypt */
66 #ifdef HAVE_SSL
67 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
68 #else
69 _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), argv[0]);
70 #endif /* HAVE_SSL */
71 break;
72
73 case 'U' : /* Username */
74 if (opt[1] != '\0')
75 {
76 cupsSetUser(opt + 1);
77 opt += strlen(opt) - 1;
78 }
79 else
80 {
81 i ++;
82 if (i >= argc)
83 {
84 _cupsLangPrintf(stderr, _("%s: Error - expected username after \"-U\" option."), argv[0]);
85 return (1);
86 }
87
88 cupsSetUser(argv[i]);
89 }
90 break;
91
92 case 'H' : /* Connect to host */
93 if (opt[1] != '\0')
94 {
95 cupsSetServer(opt + 1);
96 opt += strlen(opt) - 1;
97 }
98 else
99 {
100 i ++;
101
102 if (i >= argc)
103 {
104 _cupsLangPrintf(stderr, _("%s: Error - expected hostname after \"-H\" option."), argv[0]);
105 return (1);
106 }
107 else
108 cupsSetServer(argv[i]);
109 }
110 break;
111
112 case '1' : /* TROFF font set 1 */
113 case '2' : /* TROFF font set 2 */
114 case '3' : /* TROFF font set 3 */
115 case '4' : /* TROFF font set 4 */
116 case 'i' : /* indent */
117 case 'w' : /* width */
118 if (opt[1] != '\0')
119 {
120 opt += strlen(opt) - 1;
121 }
122 else
123 {
124 i ++;
125
126 if (i >= argc)
127 {
128 _cupsLangPrintf(stderr,
129 _("%s: Error - expected value after \"-%c\" "
130 "option."), argv[0], ch);
131 return (1);
132 }
133 }
134
135 case 'c' : /* CIFPLOT */
136 case 'd' : /* DVI */
137 case 'f' : /* FORTRAN */
138 case 'g' : /* plot */
139 case 'n' : /* Ditroff */
140 case 't' : /* Troff */
141 case 'v' : /* Raster image */
142 _cupsLangPrintf(stderr, _("%s: Warning - \"%c\" format modifier not supported - output may not be correct."), argv[0], ch);
143 break;
144
145 case 'o' : /* Option */
146 if (opt[1] != '\0')
147 {
148 num_options = cupsParseOptions(opt + 1, num_options, &options);
149 opt += strlen(opt) - 1;
150 }
151 else
152 {
153 i ++;
154 if (i >= argc)
155 {
156 _cupsLangPrintf(stderr, _("%s: Error - expected option=value after \"-o\" option."), argv[0]);
157 return (1);
158 }
159
160 num_options = cupsParseOptions(argv[i], num_options, &options);
161 }
162 break;
163
164 case 'l' : /* Literal/raw */
165 num_options = cupsAddOption("raw", "true", num_options, &options);
166 break;
167
168 case 'p' : /* Prettyprint */
169 num_options = cupsAddOption("prettyprint", "true", num_options, &options);
170 break;
171
172 case 'h' : /* Suppress burst page */
173 num_options = cupsAddOption("job-sheets", "none", num_options, &options);
174 break;
175
176 case 's' : /* Don't use symlinks */
177 break;
178
179 case 'm' : /* Mail on completion */
180 {
181 char email[1024]; /* EMail address */
182
183 snprintf(email, sizeof(email), "mailto:%s@%s", cupsUser(), httpGetHostname(NULL, buffer, sizeof(buffer)));
184 num_options = cupsAddOption("notify-recipient-uri", email, num_options, &options);
185 }
186 break;
187
188 case 'q' : /* Queue file but don't print */
189 num_options = cupsAddOption("job-hold-until", "indefinite", num_options, &options);
190 break;
191
192 case 'r' : /* Remove file after printing */
193 deletefile = 1;
194 break;
195
196 case 'P' : /* Destination printer or class */
197 if (opt[1] != '\0')
198 {
199 printer = opt + 1;
200 opt += strlen(opt) - 1;
201 }
202 else
203 {
204 i ++;
205 if (i >= argc)
206 {
207 _cupsLangPrintf(stderr, _("%s: Error - expected destination after \"-P\" option."), argv[0]);
208 return (1);
209 }
210
211 printer = argv[i];
212 }
213
214 if ((instance = strrchr(printer, '/')) != NULL)
215 *instance++ = '\0';
216
217 if ((dest = cupsGetNamedDest(NULL, printer, instance)) != NULL)
218 {
219 for (j = 0; j < dest->num_options; j ++)
220 if (cupsGetOption(dest->options[j].name, num_options,
221 options) == NULL)
222 num_options = cupsAddOption(dest->options[j].name,
223 dest->options[j].value,
224 num_options, &options);
225 }
226 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST ||
227 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)
228 {
229 _cupsLangPrintf(stderr, _("%s: Error - add '/version=1.1' to server name."), argv[0]);
230 return (1);
231 }
232 break;
233
234 case '#' : /* Number of copies */
235 if (opt[1] != '\0')
236 {
237 num_copies = atoi(opt + 1);
238 opt += strlen(opt) - 1;
239 }
240 else
241 {
242 i ++;
243 if (i >= argc)
244 {
245 _cupsLangPrintf(stderr, _("%s: Error - expected copies after \"-#\" option."), argv[0]);
246 return (1);
247 }
248
249 num_copies = atoi(argv[i]);
250 }
251
252 if (num_copies < 1)
253 {
254 _cupsLangPrintf(stderr, _("%s: Error - copies must be 1 or more."), argv[0]);
255 return (1);
256 }
257
258 sprintf(buffer, "%d", num_copies);
259 num_options = cupsAddOption("copies", buffer, num_options, &options);
260 break;
261
262 case 'C' : /* Class */
263 case 'J' : /* Job name */
264 case 'T' : /* Title */
265 if (opt[1] != '\0')
266 {
267 title = opt + 1;
268 opt += strlen(opt) - 1;
269 }
270 else
271 {
272 i ++;
273 if (i >= argc)
274 {
275 _cupsLangPrintf(stderr, _("%s: Error - expected name after \"-%c\" option."), argv[0], ch);
276 return (1);
277 }
278
279 title = argv[i];
280 }
281 break;
282
283 default :
284 _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."), argv[0], *opt);
285 return (1);
286 }
287 }
288 }
289 else if (num_files < 1000)
290 {
291 /*
292 * Print a file...
293 */
294
295 if (access(argv[i], R_OK) != 0)
296 {
297 _cupsLangPrintf(stderr,
298 _("%s: Error - unable to access \"%s\" - %s"),
299 argv[0], argv[i], strerror(errno));
300 return (1);
301 }
302
303 files[num_files] = argv[i];
304 num_files ++;
305
306 if (title == NULL)
307 {
308 if ((title = strrchr(argv[i], '/')) != NULL)
309 title ++;
310 else
311 title = argv[i];
312 }
313 }
314 else
315 {
316 _cupsLangPrintf(stderr, _("%s: Error - too many files - \"%s\"."), argv[0], argv[i]);
317 }
318 }
319
320 /*
321 * See if we have any files to print; if not, print from stdin...
322 */
323
324 if (printer == NULL)
325 {
326 if ((dest = cupsGetNamedDest(NULL, NULL, NULL)) != NULL)
327 {
328 printer = dest->name;
329
330 for (j = 0; j < dest->num_options; j ++)
331 if (cupsGetOption(dest->options[j].name, num_options, options) == NULL)
332 num_options = cupsAddOption(dest->options[j].name,
333 dest->options[j].value,
334 num_options, &options);
335 }
336 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST ||
337 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED)
338 {
339 _cupsLangPrintf(stderr,
340 _("%s: Error - add '/version=1.1' to server "
341 "name."), argv[0]);
342 return (1);
343 }
344 }
345
346 if (printer == NULL)
347 {
348 val = NULL;
349
350 if ((printer = getenv("LPDEST")) == NULL)
351 {
352 if ((printer = getenv("PRINTER")) != NULL)
353 {
354 if (!strcmp(printer, "lp"))
355 printer = NULL;
356 else
357 val = "PRINTER";
358 }
359 }
360 else
361 val = "LPDEST";
362
363 if (printer && !cupsGetNamedDest(NULL, printer, NULL))
364 _cupsLangPrintf(stderr,
365 _("%s: Error - %s environment variable names "
366 "non-existent destination \"%s\"."), argv[0], val,
367 printer);
368 else if (cupsLastError() == IPP_NOT_FOUND)
369 _cupsLangPrintf(stderr,
370 _("%s: Error - no default destination available."),
371 argv[0]);
372 else
373 _cupsLangPrintf(stderr, _("%s: Error - scheduler not responding."),
374 argv[0]);
375
376 return (1);
377 }
378
379 if (num_files > 0)
380 {
381 job_id = cupsPrintFiles(printer, num_files, files, title, num_options, options);
382
383 if (deletefile && job_id > 0)
384 {
385 /*
386 * Delete print files after printing...
387 */
388
389 for (i = 0; i < num_files; i ++)
390 unlink(files[i]);
391 }
392 }
393 else if ((job_id = cupsCreateJob(CUPS_HTTP_DEFAULT, printer,
394 title ? title : "(stdin)",
395 num_options, options)) > 0)
396 {
397 http_status_t status; /* Write status */
398 const char *format; /* Document format */
399 ssize_t bytes; /* Bytes read */
400
401 if (cupsGetOption("raw", num_options, options))
402 format = CUPS_FORMAT_RAW;
403 else if ((format = cupsGetOption("document-format", num_options,
404 options)) == NULL)
405 format = CUPS_FORMAT_AUTO;
406
407 status = cupsStartDocument(CUPS_HTTP_DEFAULT, printer, job_id, NULL,
408 format, 1);
409
410 while (status == HTTP_CONTINUE &&
411 (bytes = read(0, buffer, sizeof(buffer))) > 0)
412 status = cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, (size_t)bytes);
413
414 if (status != HTTP_CONTINUE)
415 {
416 _cupsLangPrintf(stderr, _("%s: Error - unable to queue from stdin - %s."),
417 argv[0], httpStatus(status));
418 cupsFinishDocument(CUPS_HTTP_DEFAULT, printer);
419 cupsCancelJob2(CUPS_HTTP_DEFAULT, printer, job_id, 0);
420 return (1);
421 }
422
423 if (cupsFinishDocument(CUPS_HTTP_DEFAULT, printer) != IPP_OK)
424 {
425 _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
426 cupsCancelJob2(CUPS_HTTP_DEFAULT, printer, job_id, 0);
427 return (1);
428 }
429 }
430
431 if (job_id < 1)
432 {
433 _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
434 return (1);
435 }
436
437 return (0);
438 }