2 * "lpr" command for CUPS.
4 * Copyright 2007-2014 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 const char *title
, /* Job title */
35 *val
; /* Environment variable name */
36 int num_copies
; /* Number of copies per file */
37 int num_files
; /* Number of files to print */
38 const char *files
[1000]; /* Files to print */
39 cups_dest_t
*dest
; /* Selected destination */
40 int num_options
; /* Number of options */
41 cups_option_t
*options
; /* Options */
42 int deletefile
; /* Delete file after print? */
43 char buffer
[8192]; /* Copy buffer */
56 for (i
= 1; i
< argc
; i
++)
57 if (argv
[i
][0] == '-')
59 switch (ch
= argv
[i
][1])
61 case 'E' : /* Encrypt */
63 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
65 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
70 case 'U' : /* Username */
71 if (argv
[i
][2] != '\0')
72 cupsSetUser(argv
[i
] + 2);
78 _cupsLangPrintf(stderr
,
79 _("%s: Error - expected username after "
80 "\"-U\" option."), argv
[0]);
88 case 'H' : /* Connect to host */
89 if (argv
[i
][2] != '\0')
90 cupsSetServer(argv
[i
] + 2);
97 _cupsLangPrintf(stderr
,
98 _("%s: Error - expected hostname after "
99 "\"-H\" option."), argv
[0]);
103 cupsSetServer(argv
[i
]);
107 case '1' : /* TROFF font set 1 */
108 case '2' : /* TROFF font set 2 */
109 case '3' : /* TROFF font set 3 */
110 case '4' : /* TROFF font set 4 */
111 case 'i' : /* indent */
112 case 'w' : /* width */
113 if (argv
[i
][2] == '\0')
119 _cupsLangPrintf(stderr
,
120 _("%s: Error - expected value after \"-%c\" "
121 "option."), argv
[0], ch
);
126 case 'c' : /* CIFPLOT */
128 case 'f' : /* FORTRAN */
129 case 'g' : /* plot */
130 case 'n' : /* Ditroff */
131 case 't' : /* Troff */
132 case 'v' : /* Raster image */
133 _cupsLangPrintf(stderr
,
134 _("%s: Warning - \"%c\" format modifier not "
135 "supported - output may not be correct."),
139 case 'o' : /* Option */
140 if (argv
[i
][2] != '\0')
141 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
147 _cupsLangPrintf(stderr
,
148 _("%s: Error - expected option=value after "
149 "\"-o\" option."), argv
[0]);
153 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
157 case 'l' : /* Literal/raw */
158 num_options
= cupsAddOption("raw", "true", num_options
, &options
);
161 case 'p' : /* Prettyprint */
162 num_options
= cupsAddOption("prettyprint", "true", num_options
,
166 case 'h' : /* Suppress burst page */
167 num_options
= cupsAddOption("job-sheets", "none", num_options
,
171 case 's' : /* Don't use symlinks */
174 case 'm' : /* Mail on completion */
176 char email
[1024]; /* EMail address */
179 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(),
180 httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
181 num_options
= cupsAddOption("notify-recipient-uri", email
,
182 num_options
, &options
);
186 case 'q' : /* Queue file but don't print */
187 num_options
= cupsAddOption("job-hold-until", "indefinite",
188 num_options
, &options
);
191 case 'r' : /* Remove file after printing */
195 case 'P' : /* Destination printer or class */
196 if (argv
[i
][2] != '\0')
197 printer
= argv
[i
] + 2;
203 _cupsLangPrintf(stderr
,
204 _("%s: Error - expected destination after "
205 "\"-P\" option."), argv
[0]);
212 if ((instance
= strrchr(printer
, '/')) != NULL
)
215 if ((dest
= cupsGetNamedDest(NULL
, printer
, instance
)) != NULL
)
217 for (j
= 0; j
< dest
->num_options
; j
++)
218 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
220 num_options
= cupsAddOption(dest
->options
[j
].name
,
221 dest
->options
[j
].value
,
222 num_options
, &options
);
224 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
225 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
227 _cupsLangPrintf(stderr
,
228 _("%s: Error - add '/version=1.1' to server "
234 case '#' : /* Number of copies */
235 if (argv
[i
][2] != '\0')
236 num_copies
= atoi(argv
[i
] + 2);
242 _cupsLangPrintf(stderr
,
243 _("%s: Error - expected copies after "
244 "\"-#\" option."), argv
[0]);
248 num_copies
= atoi(argv
[i
]);
251 sprintf(buffer
, "%d", num_copies
);
252 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
255 case 'C' : /* Class */
256 case 'J' : /* Job name */
257 case 'T' : /* Title */
258 if (argv
[i
][2] != '\0')
265 _cupsLangPrintf(stderr
,
266 _("%s: Error - expected name after \"-%c\" "
267 "option."), argv
[0], ch
);
276 _cupsLangPrintf(stderr
,
277 _("%s: Error - unknown option \"%c\"."), argv
[0],
282 else if (num_files
< 1000)
288 if (access(argv
[i
], R_OK
) != 0)
290 _cupsLangPrintf(stderr
,
291 _("%s: Error - unable to access \"%s\" - %s"),
292 argv
[0], argv
[i
], strerror(errno
));
296 files
[num_files
] = argv
[i
];
301 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
308 _cupsLangPrintf(stderr
,
309 _("%s: Error - too many files - \"%s\"."), argv
[0],
312 * See if we have any files to print; if not, print from stdin...
317 if ((dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) != NULL
)
319 printer
= dest
->name
;
321 for (j
= 0; j
< dest
->num_options
; j
++)
322 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
323 num_options
= cupsAddOption(dest
->options
[j
].name
,
324 dest
->options
[j
].value
,
325 num_options
, &options
);
327 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
328 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
330 _cupsLangPrintf(stderr
,
331 _("%s: Error - add '/version=1.1' to server "
341 if ((printer
= getenv("LPDEST")) == NULL
)
343 if ((printer
= getenv("PRINTER")) != NULL
)
345 if (!strcmp(printer
, "lp"))
354 if (printer
&& !cupsGetNamedDest(NULL
, printer
, NULL
))
355 _cupsLangPrintf(stderr
,
356 _("%s: Error - %s environment variable names "
357 "non-existent destination \"%s\"."), argv
[0], val
,
359 else if (cupsLastError() == IPP_NOT_FOUND
)
360 _cupsLangPrintf(stderr
,
361 _("%s: Error - no default destination available."),
364 _cupsLangPrintf(stderr
, _("%s: Error - scheduler not responding."),
372 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
374 if (deletefile
&& job_id
> 0)
377 * Delete print files after printing...
380 for (i
= 0; i
< num_files
; i
++)
384 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
385 title
? title
: "(stdin)",
386 num_options
, options
)) > 0)
388 http_status_t status
; /* Write status */
389 const char *format
; /* Document format */
390 ssize_t bytes
; /* Bytes read */
392 if (cupsGetOption("raw", num_options
, options
))
393 format
= CUPS_FORMAT_RAW
;
394 else if ((format
= cupsGetOption("document-format", num_options
,
396 format
= CUPS_FORMAT_AUTO
;
398 status
= cupsStartDocument(CUPS_HTTP_DEFAULT
, printer
, job_id
, NULL
,
401 while (status
== HTTP_CONTINUE
&&
402 (bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
403 status
= cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, (size_t)bytes
);
405 if (status
!= HTTP_CONTINUE
)
407 _cupsLangPrintf(stderr
, _("%s: Error - unable to queue from stdin - %s."),
408 argv
[0], httpStatus(status
));
409 cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
);
410 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
414 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
416 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
417 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
424 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());