4 * "lpr" command for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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/".
17 * Include necessary headers...
20 #include <cups/cups-private.h>
24 * 'main()' - Parse options and send files for printing.
28 main(int argc
, /* I - Number of command-line arguments */
29 char *argv
[]) /* I - Command-line arguments */
31 int i
, j
; /* Looping var */
32 int job_id
; /* Job ID */
33 char ch
; /* Option character */
34 char *printer
, /* Destination printer or class */
35 *instance
; /* Instance */
36 const char *title
, /* Job title */
37 *val
; /* Environment variable name */
38 int num_copies
; /* Number of copies per file */
39 int num_files
; /* Number of files to print */
40 const char *files
[1000]; /* Files to print */
41 cups_dest_t
*dest
; /* Selected destination */
42 int num_options
; /* Number of options */
43 cups_option_t
*options
; /* Options */
44 int deletefile
; /* Delete file after print? */
45 char buffer
[8192]; /* Copy buffer */
58 for (i
= 1; i
< argc
; i
++)
59 if (argv
[i
][0] == '-')
61 switch (ch
= argv
[i
][1])
63 case 'E' : /* Encrypt */
65 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
67 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
72 case 'U' : /* Username */
73 if (argv
[i
][2] != '\0')
74 cupsSetUser(argv
[i
] + 2);
80 _cupsLangPrintf(stderr
,
81 _("%s: Error - expected username after "
82 "\"-U\" option."), argv
[0]);
90 case 'H' : /* Connect to host */
91 if (argv
[i
][2] != '\0')
92 cupsSetServer(argv
[i
] + 2);
99 _cupsLangPrintf(stderr
,
100 _("%s: Error - expected hostname after "
101 "\"-H\" option."), argv
[0]);
105 cupsSetServer(argv
[i
]);
109 case '1' : /* TROFF font set 1 */
110 case '2' : /* TROFF font set 2 */
111 case '3' : /* TROFF font set 3 */
112 case '4' : /* TROFF font set 4 */
113 case 'i' : /* indent */
114 case 'w' : /* width */
115 if (argv
[i
][2] == '\0')
121 _cupsLangPrintf(stderr
,
122 _("%s: Error - expected value after \"-%c\" "
123 "option."), argv
[0], ch
);
128 case 'c' : /* CIFPLOT */
130 case 'f' : /* FORTRAN */
131 case 'g' : /* plot */
132 case 'n' : /* Ditroff */
133 case 't' : /* Troff */
134 case 'v' : /* Raster image */
135 _cupsLangPrintf(stderr
,
136 _("%s: Warning - \"%c\" format modifier not "
137 "supported - output may not be correct."),
141 case 'o' : /* Option */
142 if (argv
[i
][2] != '\0')
143 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
149 _cupsLangPrintf(stderr
,
150 _("%s: Error - expected option=value after "
151 "\"-o\" option."), argv
[0]);
155 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
159 case 'l' : /* Literal/raw */
160 num_options
= cupsAddOption("raw", "true", num_options
, &options
);
163 case 'p' : /* Prettyprint */
164 num_options
= cupsAddOption("prettyprint", "true", num_options
,
168 case 'h' : /* Suppress burst page */
169 num_options
= cupsAddOption("job-sheets", "none", num_options
,
173 case 's' : /* Don't use symlinks */
176 case 'm' : /* Mail on completion */
178 char email
[1024]; /* EMail address */
181 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(),
182 httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
183 num_options
= cupsAddOption("notify-recipient-uri", email
,
184 num_options
, &options
);
188 case 'q' : /* Queue file but don't print */
189 num_options
= cupsAddOption("job-hold-until", "indefinite",
190 num_options
, &options
);
193 case 'r' : /* Remove file after printing */
197 case 'P' : /* Destination printer or class */
198 if (argv
[i
][2] != '\0')
199 printer
= argv
[i
] + 2;
205 _cupsLangPrintf(stderr
,
206 _("%s: Error - expected destination after "
207 "\"-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
,
230 _("%s: Error - add '/version=1.1' to server "
236 case '#' : /* Number of copies */
237 if (argv
[i
][2] != '\0')
238 num_copies
= atoi(argv
[i
] + 2);
244 _cupsLangPrintf(stderr
,
245 _("%s: Error - expected copies after "
246 "\"-#\" option."), argv
[0]);
250 num_copies
= atoi(argv
[i
]);
253 sprintf(buffer
, "%d", num_copies
);
254 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
257 case 'C' : /* Class */
258 case 'J' : /* Job name */
259 case 'T' : /* Title */
260 if (argv
[i
][2] != '\0')
267 _cupsLangPrintf(stderr
,
268 _("%s: Error - expected name after \"-%c\" "
269 "option."), argv
[0], ch
);
278 _cupsLangPrintf(stderr
,
279 _("%s: Error - unknown option \"%c\"."), argv
[0],
284 else if (num_files
< 1000)
290 if (access(argv
[i
], R_OK
) != 0)
292 _cupsLangPrintf(stderr
,
293 _("%s: Error - unable to access \"%s\" - %s"),
294 argv
[0], argv
[i
], strerror(errno
));
298 files
[num_files
] = argv
[i
];
303 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
310 _cupsLangPrintf(stderr
,
311 _("%s: Error - too many files - \"%s\"."), argv
[0],
314 * See if we have any files to print; if not, print from stdin...
319 if ((dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) != NULL
)
321 printer
= dest
->name
;
323 for (j
= 0; j
< dest
->num_options
; j
++)
324 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
325 num_options
= cupsAddOption(dest
->options
[j
].name
,
326 dest
->options
[j
].value
,
327 num_options
, &options
);
329 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
330 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
332 _cupsLangPrintf(stderr
,
333 _("%s: Error - add '/version=1.1' to server "
343 if ((printer
= getenv("LPDEST")) == NULL
)
345 if ((printer
= getenv("PRINTER")) != NULL
)
347 if (!strcmp(printer
, "lp"))
356 if (printer
&& !cupsGetNamedDest(NULL
, printer
, NULL
))
357 _cupsLangPrintf(stderr
,
358 _("%s: Error - %s environment variable names "
359 "non-existent destination \"%s\"."), argv
[0], val
,
361 else if (cupsLastError() == IPP_NOT_FOUND
)
362 _cupsLangPrintf(stderr
,
363 _("%s: Error - no default destination available."),
366 _cupsLangPrintf(stderr
, _("%s: Error - scheduler not responding."),
374 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
376 if (deletefile
&& job_id
> 0)
379 * Delete print files after printing...
382 for (i
= 0; i
< num_files
; i
++)
386 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
387 title
? title
: "(stdin)",
388 num_options
, options
)) > 0)
390 http_status_t status
; /* Write status */
391 const char *format
; /* Document format */
392 ssize_t bytes
; /* Bytes read */
394 if (cupsGetOption("raw", num_options
, options
))
395 format
= CUPS_FORMAT_RAW
;
396 else if ((format
= cupsGetOption("document-format", num_options
,
398 format
= CUPS_FORMAT_AUTO
;
400 status
= cupsStartDocument(CUPS_HTTP_DEFAULT
, printer
, job_id
, NULL
,
403 while (status
== HTTP_CONTINUE
&&
404 (bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
405 status
= cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, (size_t)bytes
);
407 if (status
!= HTTP_CONTINUE
)
409 _cupsLangPrintf(stderr
, _("%s: Error - unable to queue from stdin - %s."),
410 argv
[0], httpStatus(status
));
411 cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
);
412 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
416 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
418 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
419 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
426 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());