]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpr.c
cdc224df8059d9aeebebe5b179f1d529e857063b
4 * "lpr" command for CUPS.
6 * Copyright 2007-2010 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] == '-')
64 switch (ch
= argv
[i
][1])
66 case 'E' : /* Encrypt */
68 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
70 _cupsLangPrintf(stderr
,
71 _("%s: Sorry, no encryption support compiled in\n"),
76 case 'U' : /* Username */
77 if (argv
[i
][2] != '\0')
78 cupsSetUser(argv
[i
] + 2);
84 _cupsLangPrintf(stderr
,
85 _("%s: Error - expected username after "
95 case 'H' : /* Connect to host */
96 if (argv
[i
][2] != '\0')
97 cupsSetServer(argv
[i
] + 2);
104 _cupsLangPrintf(stderr
,
105 _("%s: Error - expected hostname after "
111 cupsSetServer(argv
[i
]);
115 case '1' : /* TROFF font set 1 */
116 case '2' : /* TROFF font set 2 */
117 case '3' : /* TROFF font set 3 */
118 case '4' : /* TROFF font set 4 */
119 case 'i' : /* indent */
120 case 'w' : /* width */
121 if (argv
[i
][2] == '\0')
127 _cupsLangPrintf(stderr
,
128 _("%s: Error - expected value after \'-%c\' "
129 "option\n"), argv
[0], ch
);
134 case 'c' : /* CIFPLOT */
136 case 'f' : /* FORTRAN */
137 case 'g' : /* plot */
138 case 'n' : /* Ditroff */
139 case 't' : /* Troff */
140 case 'v' : /* Raster image */
141 _cupsLangPrintf(stderr
,
142 _("%s: Warning - \'%c\' format modifier not "
143 "supported - output may not be correct\n"),
147 case 'o' : /* Option */
148 if (argv
[i
][2] != '\0')
149 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
155 _cupsLangPrintf(stderr
,
156 _("%s: error - expected option=value after "
162 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
166 case 'l' : /* Literal/raw */
167 num_options
= cupsAddOption("raw", "true", num_options
, &options
);
170 case 'p' : /* Prettyprint */
171 num_options
= cupsAddOption("prettyprint", "true", num_options
,
175 case 'h' : /* Suppress burst page */
176 num_options
= cupsAddOption("job-sheets", "none", num_options
,
180 case 's' : /* Don't use symlinks */
183 case 'm' : /* Mail on completion */
185 char email
[1024]; /* EMail address */
188 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(),
189 httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
190 num_options
= cupsAddOption("notify-recipient-uri", email
,
191 num_options
, &options
);
195 case 'q' : /* Queue file but don't print */
196 num_options
= cupsAddOption("job-hold-until", "indefinite",
197 num_options
, &options
);
200 case 'r' : /* Remove file after printing */
204 case 'P' : /* Destination printer or class */
205 if (argv
[i
][2] != '\0')
206 printer
= argv
[i
] + 2;
212 _cupsLangPrintf(stderr
,
213 _("%s: Error - expected destination after "
222 if ((instance
= strrchr(printer
, '/')) != NULL
)
225 if ((dest
= cupsGetNamedDest(NULL
, printer
, instance
)) != NULL
)
227 for (j
= 0; j
< dest
->num_options
; j
++)
228 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
230 num_options
= cupsAddOption(dest
->options
[j
].name
,
231 dest
->options
[j
].value
,
232 num_options
, &options
);
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 copy count after "
251 num_copies
= atoi(argv
[i
]);
254 sprintf(buffer
, "%d", num_copies
);
255 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
258 case 'C' : /* Class */
259 case 'J' : /* Job name */
260 case 'T' : /* Title */
261 if (argv
[i
][2] != '\0')
268 _cupsLangPrintf(stderr
,
269 _("%s: Error - expected name after \'-%c\' "
270 "option\n"), argv
[0], ch
);
279 _cupsLangPrintf(stderr
,
280 _("%s: Error - unknown option \'%c\'\n"),
281 argv
[0], argv
[i
][1]);
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\n"),
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\"\n"),
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
);
335 if ((printer
= getenv("LPDEST")) == NULL
)
337 if ((printer
= getenv("PRINTER")) != NULL
)
339 if (!strcmp(printer
, "lp"))
348 if (printer
&& !cupsGetNamedDest(NULL
, printer
, NULL
))
349 _cupsLangPrintf(stderr
,
350 _("%s: Error - %s environment variable names "
351 "non-existent destination \"%s\"\n"),
352 argv
[0], val
, printer
);
353 else if (cupsLastError() == IPP_NOT_FOUND
)
354 _cupsLangPrintf(stderr
,
355 _("%s: Error - no default destination available.\n"),
358 _cupsLangPrintf(stderr
,
359 _("%s: Error - scheduler not responding\n"),
367 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
369 if (deletefile
&& job_id
> 0)
372 * Delete print files after printing...
375 for (i
= 0; i
< num_files
; i
++)
379 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
380 title
? title
: "(stdin)",
381 num_options
, options
)) > 0)
383 http_status_t status
; /* Write status */
384 const char *format
; /* Document format */
385 ssize_t bytes
; /* Bytes read */
388 if (cupsGetOption("raw", num_options
, options
))
389 format
= CUPS_FORMAT_RAW
;
390 else if ((format
= cupsGetOption("document-format", num_options
,
392 format
= CUPS_FORMAT_AUTO
;
394 status
= cupsStartDocument(CUPS_HTTP_DEFAULT
, printer
, job_id
, NULL
,
397 while (status
== HTTP_CONTINUE
&&
398 (bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
399 status
= cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, bytes
);
401 if (status
!= HTTP_CONTINUE
)
403 _cupsLangPrintf(stderr
,
404 _("%s: Error - unable to queue from stdin - %s\n"),
405 argv
[0], httpStatus(status
));
409 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
415 _cupsLangPrintf(stderr
, "%s: %s\n", argv
[0], cupsLastErrorString());