]>
git.ipfire.org Git - thirdparty/cups.git/blob - berkeley/lpr.c
2 * "$Id: lpr.c,v 1.20.2.8 2003/01/15 04:25:48 mike Exp $"
4 * "lpr" command for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 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-3111 USA
20 * Voice: (301) 373-9603
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>
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 int num_copies
; /* Number of copies per file */
77 int num_files
; /* Number of files to print */
78 const char *files
[1000]; /* Files to print */
79 int num_dests
; /* Number of destinations */
80 cups_dest_t
*dests
, /* Destinations */
81 *dest
; /* Selected destination */
82 int num_options
; /* Number of options */
83 cups_option_t
*options
; /* Options */
84 int deletefile
; /* Delete file after print? */
85 char buffer
[8192]; /* Copy buffer */
86 int temp
; /* Temporary file descriptor */
87 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
88 struct sigaction action
; /* Signal action */
89 struct sigaction oldaction
; /* Old signal action */
90 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
102 for (i
= 1; i
< argc
; i
++)
103 if (argv
[i
][0] == '-')
104 switch (ch
= argv
[i
][1])
106 case 'E' : /* Encrypt */
108 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
110 fprintf(stderr
, "%s: Sorry, no encryption support compiled in!\n",
112 #endif /* HAVE_SSL */
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 fprintf(stderr
, "lpr: Expected value after -%c option!\n", ch
);
132 case 'c' : /* CIFPLOT */
134 case 'f' : /* FORTRAN */
135 case 'g' : /* plot */
136 case 'n' : /* Ditroff */
137 case 't' : /* Troff */
138 case 'v' : /* Raster image */
139 fprintf(stderr
, "Warning: \'%c\' format modifier not supported - output may not be correct!\n",
143 case 'o' : /* Option */
144 if (argv
[i
][2] != '\0')
145 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
151 fputs("lpr: Expected option=value after -o option!\n", stderr
);
155 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
159 case 'l' : /* Literal/raw */
160 num_options
= cupsAddOption("raw", "", num_options
, &options
);
163 case 'p' : /* Prettyprint */
164 num_options
= cupsAddOption("prettyprint", "", num_options
, &options
);
167 case 'h' : /* Suppress burst page */
168 num_options
= cupsAddOption("job-sheets", "none", num_options
, &options
);
171 case 's' : /* Don't use symlinks */
174 case 'm' : /* Mail on completion */
175 fputs("Warning: email notification is not supported!\n", stderr
);
178 case 'q' : /* Queue file but don't print */
179 num_options
= cupsAddOption("job-hold-until", "indefinite",
180 num_options
, &options
);
183 case 'r' : /* Remove file after printing */
187 case 'P' : /* Destination printer or class */
188 if (argv
[i
][2] != '\0')
189 printer
= argv
[i
] + 2;
195 fputs("lpr: Expected destination after -P option!\n", stderr
);
202 if ((instance
= strrchr(printer
, '/')) != NULL
)
206 num_dests
= cupsGetDests(&dests
);
208 if ((dest
= cupsGetDest(printer
, instance
, num_dests
, dests
)) != NULL
)
210 for (j
= 0; j
< dest
->num_options
; j
++)
211 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
212 num_options
= cupsAddOption(dest
->options
[j
].name
,
213 dest
->options
[j
].value
,
214 num_options
, &options
);
218 case '#' : /* Number of copies */
219 if (argv
[i
][2] != '\0')
220 num_copies
= atoi(argv
[i
] + 2);
226 fputs("lpr: Expected copy count after -# option!\n", stderr
);
230 num_copies
= atoi(argv
[i
]);
233 sprintf(buffer
, "%d", num_copies
);
234 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
237 case 'C' : /* Class */
238 case 'J' : /* Job name */
239 case 'T' : /* Title */
240 if (argv
[i
][2] != '\0')
247 fprintf(stderr
, "lpr: Expected name after -%c option!\n", ch
);
255 case 'U' : /* User */
256 if (argv
[i
][2] != '\0')
257 cupsSetUser(argv
[i
] + 2);
263 fputs("lpr: Expected username after -U option!\n", stderr
);
267 cupsSetUser(argv
[i
]);
272 fprintf(stderr
, "lpr: Unknown option \'%c\'!\n", argv
[i
][1]);
275 else if (num_files
< 1000)
281 if (access(argv
[i
], R_OK
) != 0)
283 fprintf(stderr
, "lpr: Unable to access \"%s\" - %s\n", argv
[i
],
288 files
[num_files
] = argv
[i
];
293 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
300 fprintf(stderr
, "lpr: Too many files - \"%s\"\n", argv
[i
]);
302 * See if we have any files to print; if not, print from stdin...
308 num_dests
= cupsGetDests(&dests
);
310 for (j
= 0, dest
= dests
; j
< num_dests
; j
++, dest
++)
311 if (dest
->is_default
)
313 printer
= dests
[j
].name
;
315 for (j
= 0; j
< dest
->num_options
; j
++)
316 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
317 num_options
= cupsAddOption(dest
->options
[j
].name
,
318 dest
->options
[j
].value
,
319 num_options
, &options
);
326 fputs("lpr: error - no default destination available.\n", stderr
);
332 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
337 * Delete print files after printing...
340 for (i
= 0; i
< num_files
; i
++)
349 # if defined(HAVE_SIGSET)
350 sigset(SIGHUP
, sighandler
);
351 if (sigset(SIGINT
, sighandler
) == SIG_IGN
)
352 sigset(SIGINT
, SIG_IGN
);
353 sigset(SIGTERM
, sighandler
);
354 # elif defined(HAVE_SIGACTION)
355 memset(&action
, 0, sizeof(action
));
356 action
.sa_handler
= sighandler
;
358 sigaction(SIGHUP
, &action
, NULL
);
359 sigaction(SIGINT
, NULL
, &oldaction
);
360 if (oldaction
.sa_handler
!= SIG_IGN
)
361 sigaction(SIGINT
, &action
, NULL
);
362 sigaction(SIGTERM
, &action
, NULL
);
364 signal(SIGHUP
, sighandler
);
365 if (signal(SIGINT
, sighandler
) == SIG_IGN
)
366 signal(SIGINT
, SIG_IGN
);
367 signal(SIGTERM
, sighandler
);
371 if ((temp
= cupsTempFd(tempfile
, sizeof(tempfile
))) < 0)
373 fputs("lpr: unable to create temporary file.\n", stderr
);
377 while ((i
= read(0, buffer
, sizeof(buffer
))) > 0)
378 write(temp
, buffer
, i
);
380 i
= lseek(temp
, 0, SEEK_CUR
);
385 fputs("lpr: stdin is empty, so no job has been sent.\n", stderr
);
390 job_id
= cupsPrintFile(printer
, tempfile
, title
, num_options
, options
);
392 job_id
= cupsPrintFile(printer
, tempfile
, "(stdin)", num_options
, options
);
399 fprintf(stderr
, "lpr: unable to print file: %s\n",
400 ippErrorString(cupsLastError()));
410 * 'sighandler()' - Signal catcher for when we print from stdin...
414 sighandler(int s
) /* I - Signal number */
417 * Remove the temporary file we're using to print from stdin...
432 * End of "$Id: lpr.c,v 1.20.2.8 2003/01/15 04:25:48 mike Exp $".