]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpr.c
2 * "$Id: lpr.c 7720 2008-07-11 22:46:21Z mike $"
4 * "lpr" command for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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...
28 #include <cups/string.h>
29 #include <cups/cups.h>
30 #include <cups/i18n.h>
34 * 'main()' - Parse options and send files for printing.
38 main(int argc
, /* I - Number of command-line arguments */
39 char *argv
[]) /* I - Command-line arguments */
41 int i
, j
; /* Looping var */
42 int job_id
; /* Job ID */
43 char ch
; /* Option character */
44 char *printer
, /* Destination printer or class */
45 *instance
; /* Instance */
46 const char *title
, /* Job title */
47 *val
; /* Environment variable name */
48 int num_copies
; /* Number of copies per file */
49 int num_files
; /* Number of files to print */
50 const char *files
[1000]; /* Files to print */
51 cups_dest_t
*dest
; /* Selected destination */
52 int num_options
; /* Number of options */
53 cups_option_t
*options
; /* Options */
54 int deletefile
; /* Delete file after print? */
55 char buffer
[8192]; /* Copy buffer */
68 for (i
= 1; i
< argc
; i
++)
69 if (argv
[i
][0] == '-')
70 switch (ch
= argv
[i
][1])
72 case 'E' : /* Encrypt */
74 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
76 _cupsLangPrintf(stderr
,
77 _("%s: Sorry, no encryption support compiled in!\n"),
82 case 'U' : /* Username */
83 if (argv
[i
][2] != '\0')
84 cupsSetUser(argv
[i
] + 2);
90 _cupsLangPrintf(stderr
,
91 _("%s: Error - expected username after "
101 case 'H' : /* Connect to host */
102 if (argv
[i
][2] != '\0')
103 cupsSetServer(argv
[i
] + 2);
110 _cupsLangPrintf(stderr
,
111 _("%s: Error - expected hostname after "
117 cupsSetServer(argv
[i
]);
121 case '1' : /* TROFF font set 1 */
122 case '2' : /* TROFF font set 2 */
123 case '3' : /* TROFF font set 3 */
124 case '4' : /* TROFF font set 4 */
125 case 'i' : /* indent */
126 case 'w' : /* width */
127 if (argv
[i
][2] == '\0')
133 _cupsLangPrintf(stderr
,
134 _("%s: Error - expected value after \'-%c\' "
135 "option!\n"), argv
[0], ch
);
140 case 'c' : /* CIFPLOT */
142 case 'f' : /* FORTRAN */
143 case 'g' : /* plot */
144 case 'n' : /* Ditroff */
145 case 't' : /* Troff */
146 case 'v' : /* Raster image */
147 _cupsLangPrintf(stderr
,
148 _("%s: Warning - \'%c\' format modifier not "
149 "supported - output may not be correct!\n"),
153 case 'o' : /* Option */
154 if (argv
[i
][2] != '\0')
155 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
161 _cupsLangPrintf(stderr
,
162 _("%s: error - expected option=value after "
168 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
172 case 'l' : /* Literal/raw */
173 num_options
= cupsAddOption("raw", "true", num_options
, &options
);
176 case 'p' : /* Prettyprint */
177 num_options
= cupsAddOption("prettyprint", "true", num_options
,
181 case 'h' : /* Suppress burst page */
182 num_options
= cupsAddOption("job-sheets", "none", num_options
,
186 case 's' : /* Don't use symlinks */
189 case 'm' : /* Mail on completion */
191 char email
[1024]; /* EMail address */
194 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(),
195 httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
196 num_options
= cupsAddOption("notify-recipient-uri", email
,
197 num_options
, &options
);
201 case 'q' : /* Queue file but don't print */
202 num_options
= cupsAddOption("job-hold-until", "indefinite",
203 num_options
, &options
);
206 case 'r' : /* Remove file after printing */
210 case 'P' : /* Destination printer or class */
211 if (argv
[i
][2] != '\0')
212 printer
= argv
[i
] + 2;
218 _cupsLangPrintf(stderr
,
219 _("%s: Error - expected destination after "
228 if ((instance
= strrchr(printer
, '/')) != NULL
)
231 if ((dest
= cupsGetNamedDest(NULL
, printer
, instance
)) != NULL
)
233 for (j
= 0; j
< dest
->num_options
; j
++)
234 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
236 num_options
= cupsAddOption(dest
->options
[j
].name
,
237 dest
->options
[j
].value
,
238 num_options
, &options
);
242 case '#' : /* Number of copies */
243 if (argv
[i
][2] != '\0')
244 num_copies
= atoi(argv
[i
] + 2);
250 _cupsLangPrintf(stderr
,
251 _("%s: Error - expected copy count after "
257 num_copies
= atoi(argv
[i
]);
260 sprintf(buffer
, "%d", num_copies
);
261 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
264 case 'C' : /* Class */
265 case 'J' : /* Job name */
266 case 'T' : /* Title */
267 if (argv
[i
][2] != '\0')
274 _cupsLangPrintf(stderr
,
275 _("%s: Error - expected name after \'-%c\' "
276 "option!\n"), argv
[0], ch
);
285 _cupsLangPrintf(stderr
,
286 _("%s: Error - unknown option \'%c\'!\n"),
287 argv
[0], argv
[i
][1]);
290 else if (num_files
< 1000)
296 if (access(argv
[i
], R_OK
) != 0)
298 _cupsLangPrintf(stderr
,
299 _("%s: Error - unable to access \"%s\" - %s\n"),
300 argv
[0], argv
[i
], strerror(errno
));
304 files
[num_files
] = argv
[i
];
309 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
316 _cupsLangPrintf(stderr
,
317 _("%s: Error - too many files - \"%s\"\n"),
320 * See if we have any files to print; if not, print from stdin...
325 if ((dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) != NULL
)
327 printer
= dest
->name
;
329 for (j
= 0; j
< dest
->num_options
; j
++)
330 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
331 num_options
= cupsAddOption(dest
->options
[j
].name
,
332 dest
->options
[j
].value
,
333 num_options
, &options
);
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\"!\n"),
358 argv
[0], val
, printer
);
359 else if (cupsLastError() == IPP_NOT_FOUND
)
360 _cupsLangPrintf(stderr
,
361 _("%s: Error - no default destination available.\n"),
364 _cupsLangPrintf(stderr
,
365 _("%s: Error - scheduler not responding!\n"),
373 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
375 if (deletefile
&& job_id
> 0)
378 * Delete print files after printing...
381 for (i
= 0; i
< num_files
; i
++)
385 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
386 title
? title
: "(stdin)",
387 num_options
, options
)) > 0)
389 http_status_t status
; /* Write status */
390 const char *format
; /* Document format */
391 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
, bytes
);
407 if (status
!= HTTP_CONTINUE
)
409 _cupsLangPrintf(stderr
,
410 _("%s: Error - unable to queue from stdin - %s\n"),
411 argv
[0], httpStatus(status
));
415 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
421 _cupsLangPrintf(stderr
, "%s: %s\n", argv
[0], cupsLastErrorString());
430 * End of "$Id: lpr.c 7720 2008-07-11 22:46:21Z mike $".