]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpr.c
4 * "lpr" command for CUPS.
6 * Copyright 2007-2012 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
, _("%s: Sorry, no encryption support."),
75 case 'U' : /* Username */
76 if (argv
[i
][2] != '\0')
77 cupsSetUser(argv
[i
] + 2);
83 _cupsLangPrintf(stderr
,
84 _("%s: Error - expected username after "
85 "\"-U\" option."), argv
[0]);
93 case 'H' : /* Connect to host */
94 if (argv
[i
][2] != '\0')
95 cupsSetServer(argv
[i
] + 2);
102 _cupsLangPrintf(stderr
,
103 _("%s: Error - expected hostname after "
104 "\"-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 */
118 if (argv
[i
][2] == '\0')
124 _cupsLangPrintf(stderr
,
125 _("%s: Error - expected value after \"-%c\" "
126 "option."), argv
[0], ch
);
131 case 'c' : /* CIFPLOT */
133 case 'f' : /* FORTRAN */
134 case 'g' : /* plot */
135 case 'n' : /* Ditroff */
136 case 't' : /* Troff */
137 case 'v' : /* Raster image */
138 _cupsLangPrintf(stderr
,
139 _("%s: Warning - \"%c\" format modifier not "
140 "supported - output may not be correct."),
144 case 'o' : /* Option */
145 if (argv
[i
][2] != '\0')
146 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
152 _cupsLangPrintf(stderr
,
153 _("%s: Error - expected option=value after "
154 "\"-o\" option."), argv
[0]);
158 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
162 case 'l' : /* Literal/raw */
163 num_options
= cupsAddOption("raw", "true", num_options
, &options
);
166 case 'p' : /* Prettyprint */
167 num_options
= cupsAddOption("prettyprint", "true", num_options
,
171 case 'h' : /* Suppress burst page */
172 num_options
= cupsAddOption("job-sheets", "none", num_options
,
176 case 's' : /* Don't use symlinks */
179 case 'm' : /* Mail on completion */
181 char email
[1024]; /* EMail address */
184 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(),
185 httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
186 num_options
= cupsAddOption("notify-recipient-uri", email
,
187 num_options
, &options
);
191 case 'q' : /* Queue file but don't print */
192 num_options
= cupsAddOption("job-hold-until", "indefinite",
193 num_options
, &options
);
196 case 'r' : /* Remove file after printing */
200 case 'P' : /* Destination printer or class */
201 if (argv
[i
][2] != '\0')
202 printer
= argv
[i
] + 2;
208 _cupsLangPrintf(stderr
,
209 _("%s: Error - expected destination after "
210 "\"-P\" option."), argv
[0]);
217 if ((instance
= strrchr(printer
, '/')) != NULL
)
220 if ((dest
= cupsGetNamedDest(NULL
, printer
, instance
)) != NULL
)
222 for (j
= 0; j
< dest
->num_options
; j
++)
223 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
225 num_options
= cupsAddOption(dest
->options
[j
].name
,
226 dest
->options
[j
].value
,
227 num_options
, &options
);
231 case '#' : /* Number of copies */
232 if (argv
[i
][2] != '\0')
233 num_copies
= atoi(argv
[i
] + 2);
239 _cupsLangPrintf(stderr
,
240 _("%s: Error - expected copies after "
241 "\"-#\" option."), argv
[0]);
245 num_copies
= atoi(argv
[i
]);
248 sprintf(buffer
, "%d", num_copies
);
249 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
252 case 'C' : /* Class */
253 case 'J' : /* Job name */
254 case 'T' : /* Title */
255 if (argv
[i
][2] != '\0')
262 _cupsLangPrintf(stderr
,
263 _("%s: Error - expected name after \"-%c\" "
264 "option."), argv
[0], ch
);
273 _cupsLangPrintf(stderr
,
274 _("%s: Error - unknown option \"%c\"."), argv
[0],
278 else if (num_files
< 1000)
284 if (access(argv
[i
], R_OK
) != 0)
286 _cupsLangPrintf(stderr
,
287 _("%s: Error - unable to access \"%s\" - %s"),
288 argv
[0], argv
[i
], strerror(errno
));
292 files
[num_files
] = argv
[i
];
297 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
304 _cupsLangPrintf(stderr
,
305 _("%s: Error - too many files - \"%s\"."), argv
[0],
308 * See if we have any files to print; if not, print from stdin...
313 if ((dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) != NULL
)
315 printer
= dest
->name
;
317 for (j
= 0; j
< dest
->num_options
; j
++)
318 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
319 num_options
= cupsAddOption(dest
->options
[j
].name
,
320 dest
->options
[j
].value
,
321 num_options
, &options
);
329 if ((printer
= getenv("LPDEST")) == NULL
)
331 if ((printer
= getenv("PRINTER")) != NULL
)
333 if (!strcmp(printer
, "lp"))
342 if (printer
&& !cupsGetNamedDest(NULL
, printer
, NULL
))
343 _cupsLangPrintf(stderr
,
344 _("%s: Error - %s environment variable names "
345 "non-existent destination \"%s\"."), argv
[0], val
,
347 else if (cupsLastError() == IPP_NOT_FOUND
)
348 _cupsLangPrintf(stderr
,
349 _("%s: Error - no default destination available."),
352 _cupsLangPrintf(stderr
, _("%s: Error - scheduler not responding."),
360 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
362 if (deletefile
&& job_id
> 0)
365 * Delete print files after printing...
368 for (i
= 0; i
< num_files
; i
++)
372 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
373 title
? title
: "(stdin)",
374 num_options
, options
)) > 0)
376 http_status_t status
; /* Write status */
377 const char *format
; /* Document format */
378 ssize_t bytes
; /* Bytes read */
380 if (cupsGetOption("raw", num_options
, options
))
381 format
= CUPS_FORMAT_RAW
;
382 else if ((format
= cupsGetOption("document-format", num_options
,
384 format
= CUPS_FORMAT_AUTO
;
386 status
= cupsStartDocument(CUPS_HTTP_DEFAULT
, printer
, job_id
, NULL
,
389 while (status
== HTTP_CONTINUE
&&
390 (bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
391 status
= cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, bytes
);
393 if (status
!= HTTP_CONTINUE
)
395 _cupsLangPrintf(stderr
, _("%s: Error - unable to queue from stdin - %s."),
396 argv
[0], httpStatus(status
));
397 cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
);
398 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
402 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
404 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
405 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
412 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());