2 * "$Id: lpr.c 11101 2013-07-08 11:20:33Z msweet $"
4 * "lpr" command for CUPS.
6 * Copyright 2007-2013 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 * main() - Parse options and send files for printing.
21 * Include necessary headers...
24 #include <cups/cups-private.h>
28 * 'main()' - Parse options and send files for printing.
32 main(int argc
, /* I - Number of command-line arguments */
33 char *argv
[]) /* I - Command-line arguments */
35 int i
, j
; /* Looping var */
36 int job_id
; /* Job ID */
37 char ch
; /* Option character */
38 char *printer
, /* Destination printer or class */
39 *instance
; /* Instance */
40 const char *title
, /* Job title */
41 *val
; /* Environment variable name */
42 int num_copies
; /* Number of copies per file */
43 int num_files
; /* Number of files to print */
44 const char *files
[1000]; /* Files to print */
45 cups_dest_t
*dest
; /* Selected destination */
46 int num_options
; /* Number of options */
47 cups_option_t
*options
; /* Options */
48 int deletefile
; /* Delete file after print? */
49 char buffer
[8192]; /* Copy buffer */
62 for (i
= 1; i
< argc
; i
++)
63 if (argv
[i
][0] == '-')
65 switch (ch
= argv
[i
][1])
67 case 'E' : /* Encrypt */
69 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
71 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
76 case 'U' : /* Username */
77 if (argv
[i
][2] != '\0')
78 cupsSetUser(argv
[i
] + 2);
84 _cupsLangPrintf(stderr
,
85 _("%s: Error - expected username after "
86 "\"-U\" option."), argv
[0]);
94 case 'H' : /* Connect to host */
95 if (argv
[i
][2] != '\0')
96 cupsSetServer(argv
[i
] + 2);
103 _cupsLangPrintf(stderr
,
104 _("%s: Error - expected hostname after "
105 "\"-H\" option."), argv
[0]);
109 cupsSetServer(argv
[i
]);
113 case '1' : /* TROFF font set 1 */
114 case '2' : /* TROFF font set 2 */
115 case '3' : /* TROFF font set 3 */
116 case '4' : /* TROFF font set 4 */
117 case 'i' : /* indent */
118 case 'w' : /* width */
119 if (argv
[i
][2] == '\0')
125 _cupsLangPrintf(stderr
,
126 _("%s: Error - expected value after \"-%c\" "
127 "option."), argv
[0], ch
);
132 case 'c' : /* CIFPLOT */
134 case 'f' : /* FORTRAN */
135 case 'g' : /* plot */
136 case 'n' : /* Ditroff */
137 case 't' : /* Troff */
138 case 'v' : /* Raster image */
139 _cupsLangPrintf(stderr
,
140 _("%s: Warning - \"%c\" format modifier not "
141 "supported - output may not be correct."),
145 case 'o' : /* Option */
146 if (argv
[i
][2] != '\0')
147 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
153 _cupsLangPrintf(stderr
,
154 _("%s: Error - expected option=value after "
155 "\"-o\" option."), argv
[0]);
159 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
163 case 'l' : /* Literal/raw */
164 num_options
= cupsAddOption("raw", "true", num_options
, &options
);
167 case 'p' : /* Prettyprint */
168 num_options
= cupsAddOption("prettyprint", "true", num_options
,
172 case 'h' : /* Suppress burst page */
173 num_options
= cupsAddOption("job-sheets", "none", num_options
,
177 case 's' : /* Don't use symlinks */
180 case 'm' : /* Mail on completion */
182 char email
[1024]; /* EMail address */
185 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(),
186 httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
187 num_options
= cupsAddOption("notify-recipient-uri", email
,
188 num_options
, &options
);
192 case 'q' : /* Queue file but don't print */
193 num_options
= cupsAddOption("job-hold-until", "indefinite",
194 num_options
, &options
);
197 case 'r' : /* Remove file after printing */
201 case 'P' : /* Destination printer or class */
202 if (argv
[i
][2] != '\0')
203 printer
= argv
[i
] + 2;
209 _cupsLangPrintf(stderr
,
210 _("%s: Error - expected destination after "
211 "\"-P\" option."), argv
[0]);
218 if ((instance
= strrchr(printer
, '/')) != NULL
)
221 if ((dest
= cupsGetNamedDest(NULL
, printer
, instance
)) != NULL
)
223 for (j
= 0; j
< dest
->num_options
; j
++)
224 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
226 num_options
= cupsAddOption(dest
->options
[j
].name
,
227 dest
->options
[j
].value
,
228 num_options
, &options
);
230 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
231 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
233 _cupsLangPrintf(stderr
,
234 _("%s: Error - add '/version=1.1' to server "
240 case '#' : /* Number of copies */
241 if (argv
[i
][2] != '\0')
242 num_copies
= atoi(argv
[i
] + 2);
248 _cupsLangPrintf(stderr
,
249 _("%s: Error - expected copies after "
250 "\"-#\" option."), argv
[0]);
254 num_copies
= atoi(argv
[i
]);
257 sprintf(buffer
, "%d", num_copies
);
258 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
261 case 'C' : /* Class */
262 case 'J' : /* Job name */
263 case 'T' : /* Title */
264 if (argv
[i
][2] != '\0')
271 _cupsLangPrintf(stderr
,
272 _("%s: Error - expected name after \"-%c\" "
273 "option."), argv
[0], ch
);
282 _cupsLangPrintf(stderr
,
283 _("%s: Error - unknown option \"%c\"."), argv
[0],
288 else if (num_files
< 1000)
294 if (access(argv
[i
], R_OK
) != 0)
296 _cupsLangPrintf(stderr
,
297 _("%s: Error - unable to access \"%s\" - %s"),
298 argv
[0], argv
[i
], strerror(errno
));
302 files
[num_files
] = argv
[i
];
307 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
314 _cupsLangPrintf(stderr
,
315 _("%s: Error - too many files - \"%s\"."), argv
[0],
318 * See if we have any files to print; if not, print from stdin...
323 if ((dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) != NULL
)
325 printer
= dest
->name
;
327 for (j
= 0; j
< dest
->num_options
; j
++)
328 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
329 num_options
= cupsAddOption(dest
->options
[j
].name
,
330 dest
->options
[j
].value
,
331 num_options
, &options
);
333 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
334 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
336 _cupsLangPrintf(stderr
,
337 _("%s: Error - add '/version=1.1' to server "
347 if ((printer
= getenv("LPDEST")) == NULL
)
349 if ((printer
= getenv("PRINTER")) != NULL
)
351 if (!strcmp(printer
, "lp"))
360 if (printer
&& !cupsGetNamedDest(NULL
, printer
, NULL
))
361 _cupsLangPrintf(stderr
,
362 _("%s: Error - %s environment variable names "
363 "non-existent destination \"%s\"."), argv
[0], val
,
365 else if (cupsLastError() == IPP_NOT_FOUND
)
366 _cupsLangPrintf(stderr
,
367 _("%s: Error - no default destination available."),
370 _cupsLangPrintf(stderr
, _("%s: Error - scheduler not responding."),
378 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
380 if (deletefile
&& job_id
> 0)
383 * Delete print files after printing...
386 for (i
= 0; i
< num_files
; i
++)
390 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
391 title
? title
: "(stdin)",
392 num_options
, options
)) > 0)
394 http_status_t status
; /* Write status */
395 const char *format
; /* Document format */
396 ssize_t bytes
; /* Bytes read */
398 if (cupsGetOption("raw", num_options
, options
))
399 format
= CUPS_FORMAT_RAW
;
400 else if ((format
= cupsGetOption("document-format", num_options
,
402 format
= CUPS_FORMAT_AUTO
;
404 status
= cupsStartDocument(CUPS_HTTP_DEFAULT
, printer
, job_id
, NULL
,
407 while (status
== HTTP_CONTINUE
&&
408 (bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
409 status
= cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, bytes
);
411 if (status
!= HTTP_CONTINUE
)
413 _cupsLangPrintf(stderr
, _("%s: Error - unable to queue from stdin - %s."),
414 argv
[0], httpStatus(status
));
415 cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
);
416 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
420 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
422 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
423 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
430 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
439 * End of "$Id: lpr.c 11101 2013-07-08 11:20:33Z msweet $".