]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpr.c
2 * "$Id: lpr.c 6649 2007-07-11 21:46:42Z mike $"
4 * "lpr" command for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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.
18 * sighandler() - Signal catcher for when we print from stdin...
22 * Include necessary headers...
29 #include <cups/string.h>
30 #include <cups/cups.h>
31 #include <cups/i18n.h>
50 char tempfile
[1024]; /* Temporary file for printing from stdin */
54 * 'main()' - Parse options and send files for printing.
58 main(int argc
, /* I - Number of command-line arguments */
59 char *argv
[]) /* I - Command-line arguments */
61 int i
, j
; /* Looping var */
62 int job_id
; /* Job ID */
63 char ch
; /* Option character */
64 char *printer
, /* Destination printer or class */
65 *instance
; /* Instance */
66 const char *title
, /* Job title */
67 *val
; /* Environment variable name */
68 int num_copies
; /* Number of copies per file */
69 int num_files
; /* Number of files to print */
70 const char *files
[1000]; /* Files to print */
71 int num_dests
; /* Number of destinations */
72 cups_dest_t
*dests
, /* Destinations */
73 *dest
; /* Selected destination */
74 int num_options
; /* Number of options */
75 cups_option_t
*options
; /* Options */
76 int deletefile
; /* Delete file after print? */
77 char buffer
[8192]; /* Copy buffer */
78 ssize_t bytes
; /* Bytes copied */
79 off_t filesize
; /* Size of temp file */
80 int temp
; /* Temporary file descriptor */
81 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
82 struct sigaction action
; /* Signal action */
83 struct sigaction oldaction
; /* Old signal action */
84 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
98 for (i
= 1; i
< argc
; i
++)
99 if (argv
[i
][0] == '-')
100 switch (ch
= argv
[i
][1])
102 case 'E' : /* Encrypt */
104 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
106 _cupsLangPrintf(stderr
,
107 _("%s: Sorry, no encryption support compiled in!\n"),
109 #endif /* HAVE_SSL */
112 case 'U' : /* Username */
113 if (argv
[i
][2] != '\0')
114 cupsSetUser(argv
[i
] + 2);
120 _cupsLangPrintf(stderr
,
121 _("%s: Error - expected username after "
127 cupsSetUser(argv
[i
]);
131 case 'H' : /* Connect to host */
132 if (argv
[i
][2] != '\0')
133 cupsSetServer(argv
[i
] + 2);
140 _cupsLangPrintf(stderr
,
141 _("%s: Error - expected hostname after "
147 cupsSetServer(argv
[i
]);
151 case '1' : /* TROFF font set 1 */
152 case '2' : /* TROFF font set 2 */
153 case '3' : /* TROFF font set 3 */
154 case '4' : /* TROFF font set 4 */
155 case 'i' : /* indent */
156 case 'w' : /* width */
157 if (argv
[i
][2] == '\0')
163 _cupsLangPrintf(stderr
,
164 _("%s: Error - expected value after \'-%c\' "
165 "option!\n"), argv
[0], ch
);
170 case 'c' : /* CIFPLOT */
172 case 'f' : /* FORTRAN */
173 case 'g' : /* plot */
174 case 'n' : /* Ditroff */
175 case 't' : /* Troff */
176 case 'v' : /* Raster image */
177 _cupsLangPrintf(stderr
,
178 _("%s: Warning - \'%c\' format modifier not "
179 "supported - output may not be correct!\n"),
183 case 'o' : /* Option */
184 if (argv
[i
][2] != '\0')
185 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
191 _cupsLangPrintf(stderr
,
192 _("%s: error - expected option=value after "
198 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
202 case 'l' : /* Literal/raw */
203 num_options
= cupsAddOption("raw", "", num_options
, &options
);
206 case 'p' : /* Prettyprint */
207 num_options
= cupsAddOption("prettyprint", "", num_options
,
211 case 'h' : /* Suppress burst page */
212 num_options
= cupsAddOption("job-sheets", "none", num_options
,
216 case 's' : /* Don't use symlinks */
219 case 'm' : /* Mail on completion */
221 char email
[1024]; /* EMail address */
224 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(),
225 httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
226 num_options
= cupsAddOption("notify-recipient-uri", email
,
227 num_options
, &options
);
231 case 'q' : /* Queue file but don't print */
232 num_options
= cupsAddOption("job-hold-until", "indefinite",
233 num_options
, &options
);
236 case 'r' : /* Remove file after printing */
240 case 'P' : /* Destination printer or class */
241 if (argv
[i
][2] != '\0')
242 printer
= argv
[i
] + 2;
248 _cupsLangPrintf(stderr
,
249 _("%s: Error - expected destination after "
258 if ((instance
= strrchr(printer
, '/')) != NULL
)
262 num_dests
= cupsGetDests(&dests
);
264 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) != NULL
)
266 for (j
= 0; j
< dest
->num_options
; j
++)
267 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
269 num_options
= cupsAddOption(dest
->options
[j
].name
,
270 dest
->options
[j
].value
,
271 num_options
, &options
);
275 case '#' : /* Number of copies */
276 if (argv
[i
][2] != '\0')
277 num_copies
= atoi(argv
[i
] + 2);
283 _cupsLangPrintf(stderr
,
284 _("%s: Error - expected copy count after "
290 num_copies
= atoi(argv
[i
]);
293 sprintf(buffer
, "%d", num_copies
);
294 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
297 case 'C' : /* Class */
298 case 'J' : /* Job name */
299 case 'T' : /* Title */
300 if (argv
[i
][2] != '\0')
307 _cupsLangPrintf(stderr
,
308 _("%s: Error - expected name after \'-%c\' "
309 "option!\n"), argv
[0], ch
);
318 _cupsLangPrintf(stderr
,
319 _("%s: Error - unknown option \'%c\'!\n"),
320 argv
[0], argv
[i
][1]);
323 else if (num_files
< 1000)
329 if (access(argv
[i
], R_OK
) != 0)
331 _cupsLangPrintf(stderr
,
332 _("%s: Error - unable to access \"%s\" - %s\n"),
333 argv
[0], argv
[i
], strerror(errno
));
337 files
[num_files
] = argv
[i
];
342 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
349 _cupsLangPrintf(stderr
,
350 _("%s: Error - too many files - \"%s\"\n"),
353 * See if we have any files to print; if not, print from stdin...
359 num_dests
= cupsGetDests(&dests
);
361 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
363 printer
= dest
->name
;
365 for (j
= 0; j
< dest
->num_options
; j
++)
366 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
367 num_options
= cupsAddOption(dest
->options
[j
].name
,
368 dest
->options
[j
].value
,
369 num_options
, &options
);
377 if ((printer
= getenv("LPDEST")) == NULL
)
379 if ((printer
= getenv("PRINTER")) != NULL
)
381 if (!strcmp(printer
, "lp"))
390 if (printer
&& !cupsGetDest(printer
, NULL
, num_dests
, dests
))
391 _cupsLangPrintf(stderr
,
392 _("%s: Error - %s environment variable names "
393 "non-existent destination \"%s\"!\n"),
394 argv
[0], val
, printer
);
395 else if (cupsLastError() == IPP_NOT_FOUND
)
396 _cupsLangPrintf(stderr
,
397 _("%s: Error - no default destination available.\n"),
400 _cupsLangPrintf(stderr
,
401 _("%s: Error - scheduler not responding!\n"),
409 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
411 if (deletefile
&& job_id
> 0)
414 * Delete print files after printing...
417 for (i
= 0; i
< num_files
; i
++)
426 # if defined(HAVE_SIGSET)
427 sigset(SIGHUP
, sighandler
);
428 if (sigset(SIGINT
, sighandler
) == SIG_IGN
)
429 sigset(SIGINT
, SIG_IGN
);
430 sigset(SIGTERM
, sighandler
);
431 # elif defined(HAVE_SIGACTION)
432 memset(&action
, 0, sizeof(action
));
433 action
.sa_handler
= sighandler
;
435 sigaction(SIGHUP
, &action
, NULL
);
436 sigaction(SIGINT
, NULL
, &oldaction
);
437 if (oldaction
.sa_handler
!= SIG_IGN
)
438 sigaction(SIGINT
, &action
, NULL
);
439 sigaction(SIGTERM
, &action
, NULL
);
441 signal(SIGHUP
, sighandler
);
442 if (signal(SIGINT
, sighandler
) == SIG_IGN
)
443 signal(SIGINT
, SIG_IGN
);
444 signal(SIGTERM
, sighandler
);
448 if ((temp
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
450 _cupsLangPrintf(stderr
,
451 _("%s: Error - unable to create temporary file "
453 argv
[0], tempfile
, strerror(errno
));
457 while ((bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
458 if (write(temp
, buffer
, bytes
) < 0)
460 _cupsLangPrintf(stderr
,
461 _("%s: Error - unable to write to temporary file "
463 argv
[0], tempfile
, strerror(errno
));
469 filesize
= lseek(temp
, 0, SEEK_CUR
);
474 _cupsLangPrintf(stderr
,
475 _("%s: Error - stdin is empty, so no job has been sent.\n"),
482 job_id
= cupsPrintFile(printer
, tempfile
, title
, num_options
, options
);
484 job_id
= cupsPrintFile(printer
, tempfile
, "(stdin)", num_options
, options
);
491 _cupsLangPrintf(stderr
, "%s: %s\n", argv
[0], cupsLastErrorString());
501 * 'sighandler()' - Signal catcher for when we print from stdin...
505 sighandler(int s
) /* I - Signal number */
508 * Remove the temporary file we're using to print from stdin...
523 * End of "$Id: lpr.c 6649 2007-07-11 21:46:42Z mike $".