2 * "lpr" command for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
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/".
15 * Include necessary headers...
18 #include <cups/cups-private.h>
22 * 'main()' - Parse options and send files for printing.
26 main(int argc
, /* I - Number of command-line arguments */
27 char *argv
[]) /* I - Command-line arguments */
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 */
57 for (i
= 1; i
< argc
; i
++)
59 if (argv
[i
][0] == '-')
61 for (opt
= argv
[i
] + 1; *opt
; opt
++)
65 case 'E' : /* Encrypt */
67 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
69 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
73 case 'U' : /* Username */
77 opt
+= strlen(opt
) - 1;
84 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
92 case 'H' : /* Connect to host */
95 cupsSetServer(opt
+ 1);
96 opt
+= strlen(opt
) - 1;
104 _cupsLangPrintf(stderr
, _("%s: Error - expected hostname after \"-H\" option."), argv
[0]);
108 cupsSetServer(argv
[i
]);
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 */
120 opt
+= strlen(opt
) - 1;
128 _cupsLangPrintf(stderr
,
129 _("%s: Error - expected value after \"-%c\" "
130 "option."), argv
[0], ch
);
135 case 'c' : /* CIFPLOT */
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
);
145 case 'o' : /* Option */
148 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
149 opt
+= strlen(opt
) - 1;
156 _cupsLangPrintf(stderr
, _("%s: Error - expected option=value after \"-o\" option."), argv
[0]);
160 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
164 case 'l' : /* Literal/raw */
165 num_options
= cupsAddOption("raw", "true", num_options
, &options
);
168 case 'p' : /* Prettyprint */
169 num_options
= cupsAddOption("prettyprint", "true", num_options
, &options
);
172 case 'h' : /* Suppress burst page */
173 num_options
= cupsAddOption("job-sheets", "none", num_options
, &options
);
176 case 's' : /* Don't use symlinks */
179 case 'm' : /* Mail on completion */
181 char email
[1024]; /* EMail address */
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
);
188 case 'q' : /* Queue file but don't print */
189 num_options
= cupsAddOption("job-hold-until", "indefinite", num_options
, &options
);
192 case 'r' : /* Remove file after printing */
196 case 'P' : /* Destination printer or class */
200 opt
+= strlen(opt
) - 1;
207 _cupsLangPrintf(stderr
, _("%s: Error - expected destination after \"-P\" option."), argv
[0]);
214 if ((instance
= strrchr(printer
, '/')) != NULL
)
217 if ((dest
= cupsGetNamedDest(NULL
, printer
, instance
)) != NULL
)
219 for (j
= 0; j
< dest
->num_options
; j
++)
220 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
222 num_options
= cupsAddOption(dest
->options
[j
].name
,
223 dest
->options
[j
].value
,
224 num_options
, &options
);
226 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
227 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
229 _cupsLangPrintf(stderr
, _("%s: Error - add '/version=1.1' to server name."), argv
[0]);
234 case '#' : /* Number of copies */
237 num_copies
= atoi(opt
+ 1);
238 opt
+= strlen(opt
) - 1;
245 _cupsLangPrintf(stderr
, _("%s: Error - expected copies after \"-#\" option."), argv
[0]);
249 num_copies
= atoi(argv
[i
]);
254 _cupsLangPrintf(stderr
, _("%s: Error - copies must be 1 or more."), argv
[0]);
258 sprintf(buffer
, "%d", num_copies
);
259 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
262 case 'C' : /* Class */
263 case 'J' : /* Job name */
264 case 'T' : /* Title */
268 opt
+= strlen(opt
) - 1;
275 _cupsLangPrintf(stderr
, _("%s: Error - expected name after \"-%c\" option."), argv
[0], ch
);
284 _cupsLangPrintf(stderr
, _("%s: Error - unknown option \"%c\"."), argv
[0], *opt
);
289 else if (num_files
< 1000)
295 if (access(argv
[i
], R_OK
) != 0)
297 _cupsLangPrintf(stderr
,
298 _("%s: Error - unable to access \"%s\" - %s"),
299 argv
[0], argv
[i
], strerror(errno
));
303 files
[num_files
] = argv
[i
];
308 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
316 _cupsLangPrintf(stderr
, _("%s: Error - too many files - \"%s\"."), argv
[0], argv
[i
]);
321 * See if we have any files to print; if not, print from stdin...
326 if ((dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) != NULL
)
328 printer
= dest
->name
;
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
);
336 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
337 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
339 _cupsLangPrintf(stderr
,
340 _("%s: Error - add '/version=1.1' to server "
350 if ((printer
= getenv("LPDEST")) == NULL
)
352 if ((printer
= getenv("PRINTER")) != NULL
)
354 if (!strcmp(printer
, "lp"))
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
,
368 else if (cupsLastError() == IPP_NOT_FOUND
)
369 _cupsLangPrintf(stderr
,
370 _("%s: Error - no default destination available."),
373 _cupsLangPrintf(stderr
, _("%s: Error - scheduler not responding."),
381 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
383 if (deletefile
&& job_id
> 0)
386 * Delete print files after printing...
389 for (i
= 0; i
< num_files
; i
++)
393 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
394 title
? title
: "(stdin)",
395 num_options
, options
)) > 0)
397 http_status_t status
; /* Write status */
398 const char *format
; /* Document format */
399 ssize_t bytes
; /* Bytes read */
401 if (cupsGetOption("raw", num_options
, options
))
402 format
= CUPS_FORMAT_RAW
;
403 else if ((format
= cupsGetOption("document-format", num_options
,
405 format
= CUPS_FORMAT_AUTO
;
407 status
= cupsStartDocument(CUPS_HTTP_DEFAULT
, printer
, job_id
, NULL
,
410 while (status
== HTTP_CONTINUE
&&
411 (bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
412 status
= cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, (size_t)bytes
);
414 if (status
!= HTTP_CONTINUE
)
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);
423 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
425 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
426 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
433 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());