]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpr.c
2 * "$Id: lpr.c 4906 2006-01-10 20:53:28Z mike $"
4 * "lpr" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Parse options and send files for printing.
27 * sighandler() - Signal catcher for when we print from stdin...
31 * Include necessary headers...
38 #include <cups/string.h>
39 #include <cups/cups.h>
40 #include <cups/i18n.h>
59 char tempfile
[1024]; /* Temporary file for printing from stdin */
63 * 'main()' - Parse options and send files for printing.
67 main(int argc
, /* I - Number of command-line arguments */
68 char *argv
[]) /* I - Command-line arguments */
70 int i
, j
; /* Looping var */
71 int job_id
; /* Job ID */
72 char ch
; /* Option character */
73 char *printer
, /* Destination printer or class */
74 *instance
; /* Instance */
75 const char *title
, /* Job title */
76 *val
; /* Environment variable name */
77 int num_copies
; /* Number of copies per file */
78 int num_files
; /* Number of files to print */
79 const char *files
[1000]; /* Files to print */
80 int num_dests
; /* Number of destinations */
81 cups_dest_t
*dests
, /* Destinations */
82 *dest
; /* Selected destination */
83 int num_options
; /* Number of options */
84 cups_option_t
*options
; /* Options */
85 int deletefile
; /* Delete file after print? */
86 char buffer
[8192]; /* Copy buffer */
87 int temp
; /* Temporary file descriptor */
88 cups_lang_t
*language
; /* Language information */
89 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
90 struct sigaction action
; /* Signal action */
91 struct sigaction oldaction
; /* Old signal action */
92 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
103 language
= cupsLangDefault();
105 for (i
= 1; i
< argc
; i
++)
106 if (argv
[i
][0] == '-')
107 switch (ch
= argv
[i
][1])
109 case 'E' : /* Encrypt */
111 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
113 _cupsLangPrintf(stderr
, language
,
114 _("%s: Sorry, no encryption support compiled in!\n"),
116 #endif /* HAVE_SSL */
119 case '1' : /* TROFF font set 1 */
120 case '2' : /* TROFF font set 2 */
121 case '3' : /* TROFF font set 3 */
122 case '4' : /* TROFF font set 4 */
123 case 'i' : /* indent */
124 case 'w' : /* width */
125 if (argv
[i
][2] == '\0')
131 _cupsLangPrintf(stderr
, language
,
132 _("lpr: error - expected value after -%c "
138 case 'c' : /* CIFPLOT */
140 case 'f' : /* FORTRAN */
141 case 'g' : /* plot */
142 case 'n' : /* Ditroff */
143 case 't' : /* Troff */
144 case 'v' : /* Raster image */
145 _cupsLangPrintf(stderr
, language
,
146 _("lpr: warning - \'%c\' format modifier not "
147 "supported - output may not be correct!\n"), ch
);
150 case 'o' : /* Option */
151 if (argv
[i
][2] != '\0')
152 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
158 _cupsLangPuts(stderr
, language
,
159 _("lpr: error - expected option=value after "
164 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
168 case 'l' : /* Literal/raw */
169 num_options
= cupsAddOption("raw", "", num_options
, &options
);
172 case 'p' : /* Prettyprint */
173 num_options
= cupsAddOption("prettyprint", "", num_options
, &options
);
176 case 'h' : /* Suppress burst page */
177 num_options
= cupsAddOption("job-sheets", "none", num_options
, &options
);
180 case 's' : /* Don't use symlinks */
183 case 'm' : /* Mail on completion */
184 _cupsLangPuts(stderr
, language
,
185 _("lpr: warning - email notification is not "
186 "currently supported!\n"));
189 case 'q' : /* Queue file but don't print */
190 num_options
= cupsAddOption("job-hold-until", "indefinite",
191 num_options
, &options
);
194 case 'r' : /* Remove file after printing */
198 case 'P' : /* Destination printer or class */
199 if (argv
[i
][2] != '\0')
200 printer
= argv
[i
] + 2;
206 _cupsLangPuts(stderr
, language
,
207 _("lpr: error - expected destination after -P "
215 if ((instance
= strrchr(printer
, '/')) != NULL
)
219 num_dests
= cupsGetDests(&dests
);
221 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) != NULL
)
223 for (j
= 0; j
< dest
->num_options
; j
++)
224 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
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 _cupsLangPuts(stderr
, language
,
240 _("lpr: error - expected copy count after -# "
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
, language
,
263 _("lpr: error - expected name after -%c "
272 case 'U' : /* User */
273 if (argv
[i
][2] != '\0')
274 cupsSetUser(argv
[i
] + 2);
280 _cupsLangPuts(stderr
, language
,
281 _("lpr: error - expected username after -U "
286 cupsSetUser(argv
[i
]);
291 _cupsLangPrintf(stderr
, language
,
292 _("lpr: error - unknown option \'%c\'!\n"),
296 else if (num_files
< 1000)
302 if (access(argv
[i
], R_OK
) != 0)
304 _cupsLangPrintf(stderr
, language
,
305 _("lpr: error - unable to access \"%s\" - %s\n"),
306 argv
[i
], strerror(errno
));
310 files
[num_files
] = argv
[i
];
315 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
322 _cupsLangPrintf(stderr
, language
,
323 _("lpr: error - too many files - \"%s\"\n"), argv
[i
]);
325 * See if we have any files to print; if not, print from stdin...
331 num_dests
= cupsGetDests(&dests
);
333 if ((dest
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
335 printer
= dest
->name
;
337 for (j
= 0; j
< dest
->num_options
; j
++)
338 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
339 num_options
= cupsAddOption(dest
->options
[j
].name
,
340 dest
->options
[j
].value
,
341 num_options
, &options
);
349 if ((printer
= getenv("LPDEST")) == NULL
)
351 if ((printer
= getenv("PRINTER")) != NULL
)
353 if (!strcmp(printer
, "lp"))
362 if (printer
&& !cupsGetDest(printer
, NULL
, num_dests
, dests
))
363 _cupsLangPrintf(stderr
, language
,
364 _("lpr: error - %s environment variable names "
365 "non-existent destination \"%s\"!\n"),
367 else if (cupsLastError() == IPP_NOT_FOUND
)
368 _cupsLangPuts(stderr
, language
,
369 _("lpr: error - no default destination available.\n"));
371 _cupsLangPuts(stderr
, language
,
372 _("lpr: error - scheduler not responding!\n"));
379 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
381 if (deletefile
&& job_id
> 0)
384 * Delete print files after printing...
387 for (i
= 0; i
< num_files
; i
++)
396 # if defined(HAVE_SIGSET)
397 sigset(SIGHUP
, sighandler
);
398 if (sigset(SIGINT
, sighandler
) == SIG_IGN
)
399 sigset(SIGINT
, SIG_IGN
);
400 sigset(SIGTERM
, sighandler
);
401 # elif defined(HAVE_SIGACTION)
402 memset(&action
, 0, sizeof(action
));
403 action
.sa_handler
= sighandler
;
405 sigaction(SIGHUP
, &action
, NULL
);
406 sigaction(SIGINT
, NULL
, &oldaction
);
407 if (oldaction
.sa_handler
!= SIG_IGN
)
408 sigaction(SIGINT
, &action
, NULL
);
409 sigaction(SIGTERM
, &action
, NULL
);
411 signal(SIGHUP
, sighandler
);
412 if (signal(SIGINT
, sighandler
) == SIG_IGN
)
413 signal(SIGINT
, SIG_IGN
);
414 signal(SIGTERM
, sighandler
);
418 if ((temp
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
420 _cupsLangPrintf(stderr
, language
,
421 _("lpr: error - unable to create temporary file "
423 tempfile
, strerror(errno
));
427 while ((i
= read(0, buffer
, sizeof(buffer
))) > 0)
428 if (write(temp
, buffer
, i
) < 0)
430 _cupsLangPrintf(stderr
, language
,
431 _("lpr: error - unable to write to temporary file "
433 tempfile
, strerror(errno
));
439 i
= lseek(temp
, 0, SEEK_CUR
);
444 _cupsLangPuts(stderr
, language
,
445 _("lpr: error - stdin is empty, so no job has been sent.\n"));
451 job_id
= cupsPrintFile(printer
, tempfile
, title
, num_options
, options
);
453 job_id
= cupsPrintFile(printer
, tempfile
, "(stdin)", num_options
, options
);
460 _cupsLangPrintf(stderr
, language
,
461 _("lpr: error - unable to print file: %s\n"),
462 ippErrorString(cupsLastError()));
472 * 'sighandler()' - Signal catcher for when we print from stdin...
476 sighandler(int s
) /* I - Signal number */
479 * Remove the temporary file we're using to print from stdin...
494 * End of "$Id: lpr.c 4906 2006-01-10 20:53:28Z mike $".