]>
Commit | Line | Data |
---|---|---|
bd0b97ff | 1 | /* |
4e243213 | 2 | * "$Id: lpr.c,v 1.11 2000/02/24 15:20:18 mike Exp $" |
bd0b97ff | 3 | * |
4 | * "lpr" command for the Common UNIX Printing System (CUPS). | |
5 | * | |
71fe22b7 | 6 | * Copyright 1997-2000 by Easy Software Products. |
bd0b97ff | 7 | * |
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 | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636-3111 USA | |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
8dbf3e6a | 26 | * main() - Parse options and send files for printing. |
27 | * sighandler() - Signal catcher for when we print from stdin... | |
bd0b97ff | 28 | */ |
29 | ||
30 | /* | |
31 | * Include necessary headers... | |
32 | */ | |
33 | ||
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <cups/cups.h> | |
37 | ||
38 | ||
8dbf3e6a | 39 | #ifndef WIN32 |
40 | # include <signal.h> | |
41 | ||
42 | ||
43 | /* | |
44 | * Local functions. | |
45 | */ | |
46 | ||
977acbd3 | 47 | void sighandler(int); |
8dbf3e6a | 48 | #endif /* !WIN32 */ |
49 | ||
50 | ||
51 | /* | |
52 | * Globals... | |
53 | */ | |
54 | ||
55 | char tempfile[1024]; /* Temporary file for printing from stdin */ | |
56 | ||
57 | ||
bd0b97ff | 58 | /* |
59 | * 'main()' - Parse options and send files for printing. | |
60 | */ | |
61 | ||
62 | int | |
63 | main(int argc, /* I - Number of command-line arguments */ | |
64 | char *argv[]) /* I - Command-line arguments */ | |
65 | { | |
66 | int i; /* Looping var */ | |
67 | int job_id; /* Job ID */ | |
48574397 | 68 | const char *dest; /* Destination printer */ |
bf56a667 | 69 | const char *title; /* Job title */ |
bd0b97ff | 70 | int priority; /* Job priority (1-100) */ |
71 | int num_copies; /* Number of copies per file */ | |
72 | int num_files; /* Number of files printed */ | |
73 | int num_options; /* Number of options */ | |
74 | cups_option_t *options; /* Options */ | |
7dd958a9 | 75 | int silent, /* Silent or verbose output? */ |
76 | deletefile; /* Delete file after print? */ | |
bd0b97ff | 77 | char buffer[8192]; /* Copy buffer */ |
78 | FILE *temp; /* Temporary file pointer */ | |
977acbd3 | 79 | #ifdef HAVE_SIGACTION |
80 | struct sigaction action; /* Signal action */ | |
81 | #endif /* HAVE_SIGACTION */ | |
bd0b97ff | 82 | |
83 | ||
84 | silent = 0; | |
7dd958a9 | 85 | deletefile = 0; |
bd0b97ff | 86 | dest = cupsGetDefault(); |
87 | num_options = 0; | |
88 | options = NULL; | |
89 | num_files = 0; | |
90 | title = NULL; | |
91 | ||
92 | for (i = 1; i < argc; i ++) | |
93 | if (argv[i][0] == '-') | |
94 | switch (argv[i][1]) | |
95 | { | |
96 | case 'i' : /* indent */ | |
97 | case 'w' : /* width */ | |
98 | if (argv[i][2] == '\0') | |
99 | i ++; | |
100 | case 'c' : /* CIFPLOT */ | |
101 | case 'd' : /* DVI */ | |
102 | case 'f' : /* FORTRAN */ | |
103 | case 'g' : /* plot */ | |
104 | case 'n' : /* Ditroff */ | |
105 | case 't' : /* Troff */ | |
106 | case 'v' : /* Raster image */ | |
107 | fprintf(stderr, "Warning: \'%c\' format modifier not supported - output may not be correct!\n", | |
108 | argv[i][1]); | |
109 | break; | |
110 | ||
5b187d9c | 111 | case 'o' : /* Option */ |
112 | if (argv[i][2] != '\0') | |
113 | num_options = cupsParseOptions(argv[i] + 2, num_options, &options); | |
114 | else | |
115 | { | |
116 | i ++; | |
117 | num_options = cupsParseOptions(argv[i], num_options, &options); | |
118 | } | |
119 | break; | |
120 | ||
bd0b97ff | 121 | case 'l' : /* Literal/raw */ |
122 | num_options = cupsParseOptions("raw", num_options, &options); | |
123 | break; | |
124 | ||
125 | case 'p' : /* Prettyprint */ | |
126 | num_options = cupsParseOptions("prettyprint", num_options, &options); | |
127 | break; | |
128 | ||
129 | case 'h' : /* Suppress burst page */ | |
130 | case 's' : /* Don't use symlinks */ | |
131 | break; | |
132 | ||
7dd958a9 | 133 | case 'm' : /* Mail on completion */ |
bd0b97ff | 134 | fputs("Warning: email notification is not supported!\n", stderr); |
135 | break; | |
136 | ||
7dd958a9 | 137 | case 'r' : /* Remove file after printing */ |
138 | deletefile = 1; | |
139 | break; | |
140 | ||
bd0b97ff | 141 | case 'P' : /* Destination printer or class */ |
142 | if (argv[i][2] != '\0') | |
143 | dest = argv[i] + 2; | |
144 | else | |
145 | { | |
146 | i ++; | |
147 | dest = argv[i]; | |
148 | } | |
149 | break; | |
150 | ||
151 | case '#' : /* Number of copies */ | |
152 | if (argv[i][2] != '\0') | |
153 | num_copies = atoi(argv[i] + 2); | |
154 | else | |
155 | { | |
156 | i ++; | |
157 | num_copies = atoi(argv[i]); | |
158 | } | |
159 | ||
160 | if (num_copies < 1 || num_copies > 100) | |
161 | { | |
162 | fputs("lpr: Number copies must be between 1 and 100.\n", stderr); | |
163 | return (1); | |
164 | } | |
165 | ||
166 | sprintf(buffer, "%d", num_copies); | |
167 | num_options = cupsAddOption("copies", buffer, num_options, &options); | |
168 | break; | |
169 | ||
170 | case 'C' : /* Class */ | |
171 | case 'J' : /* Job name */ | |
172 | case 'T' : /* Title */ | |
173 | if (argv[i][2] != '\0') | |
174 | title = argv[i] + 2; | |
175 | else | |
176 | { | |
177 | i ++; | |
178 | title = argv[i]; | |
179 | } | |
180 | break; | |
181 | ||
182 | default : | |
183 | fprintf(stderr, "lpr: Unknown option \'%c\'!\n", argv[i][1]); | |
184 | return (1); | |
185 | } | |
186 | else | |
187 | { | |
188 | /* | |
189 | * Print a file... | |
190 | */ | |
191 | ||
192 | if (dest == NULL) | |
193 | { | |
194 | fputs("lpr: error - no default destination available.\n", stderr); | |
195 | return (1); | |
196 | } | |
197 | ||
198 | num_files ++; | |
199 | if (title) | |
200 | job_id = cupsPrintFile(dest, argv[i], title, num_options, options); | |
201 | else | |
202 | { | |
203 | char *filename; | |
204 | ||
205 | if ((filename = strrchr(argv[i], '/')) != NULL) | |
206 | filename ++; | |
207 | else | |
208 | filename = argv[i]; | |
209 | ||
210 | job_id = cupsPrintFile(dest, argv[i], filename, num_options, options); | |
211 | } | |
212 | ||
213 | if (job_id < 1) | |
214 | { | |
4e243213 | 215 | fprintf(stderr, "lpr: unable to print file \'%s\' - %s.\n", |
216 | argv[i], ippErrorString(cupsLastError())); | |
bd0b97ff | 217 | return (1); |
218 | } | |
7dd958a9 | 219 | else if (deletefile) |
220 | unlink(argv[i]); | |
bd0b97ff | 221 | } |
222 | ||
223 | /* | |
224 | * See if we printed anything; if not, print from stdin... | |
225 | */ | |
226 | ||
227 | if (num_files == 0) | |
228 | { | |
229 | if (dest == NULL) | |
230 | { | |
231 | fputs("lpr: error - no default destination available.\n", stderr); | |
232 | return (1); | |
233 | } | |
234 | ||
8dbf3e6a | 235 | #ifndef WIN32 |
977acbd3 | 236 | # if defined(HAVE_SIGSET) |
237 | sigset(SIGHUP, sighandler); | |
238 | sigset(SIGINT, sighandler); | |
239 | sigset(SIGTERM, sighandler); | |
240 | # elif defined(HAVE_SIGACTION) | |
241 | memset(&action, 0, sizeof(action)); | |
242 | action.sa_handler = sighandler; | |
243 | ||
244 | sigaction(SIGHUP, &action, NULL); | |
245 | sigaction(SIGINT, &action, NULL); | |
246 | sigaction(SIGTERM, &action, NULL); | |
247 | # else | |
248 | signal(SIGHUP, sighandler); | |
249 | signal(SIGINT, sighandler); | |
8dbf3e6a | 250 | signal(SIGTERM, sighandler); |
977acbd3 | 251 | # endif |
8dbf3e6a | 252 | #endif /* !WIN32 */ |
253 | ||
642d82f2 | 254 | temp = fopen(cupsTempFile(tempfile, sizeof(tempfile)), "w"); |
bd0b97ff | 255 | |
256 | if (temp == NULL) | |
257 | { | |
258 | fputs("lpr: unable to create temporary file.\n", stderr); | |
259 | return (1); | |
260 | } | |
261 | ||
262 | while ((i = fread(buffer, 1, sizeof(buffer), stdin)) > 0) | |
263 | fwrite(buffer, 1, i, temp); | |
264 | ||
265 | i = ftell(temp); | |
266 | fclose(temp); | |
267 | ||
268 | if (i == 0) | |
269 | { | |
bf56a667 | 270 | fputs("lpr: standard input is empty, so no job has been sent.\n", stderr); |
bd0b97ff | 271 | return (1); |
272 | } | |
273 | ||
274 | if (title) | |
275 | job_id = cupsPrintFile(dest, tempfile, title, num_options, options); | |
276 | else | |
277 | job_id = cupsPrintFile(dest, tempfile, "(stdin)", num_options, options); | |
278 | ||
68ea7095 | 279 | unlink(tempfile); |
280 | ||
bd0b97ff | 281 | if (job_id < 1) |
282 | { | |
4e243213 | 283 | fprintf(stderr, "lpr: unable to print standard input - %s.\n", |
284 | ippErrorString(cupsLastError())); | |
bd0b97ff | 285 | return (1); |
286 | } | |
287 | } | |
288 | ||
289 | return (0); | |
290 | } | |
291 | ||
292 | ||
8dbf3e6a | 293 | #ifndef WIN32 |
294 | /* | |
295 | * 'sighandler()' - Signal catcher for when we print from stdin... | |
296 | */ | |
297 | ||
298 | void | |
977acbd3 | 299 | sighandler(int s) /* I - Signal number */ |
8dbf3e6a | 300 | { |
301 | /* | |
302 | * Remove the temporary file we're using to print from stdin... | |
303 | */ | |
304 | ||
305 | unlink(tempfile); | |
977acbd3 | 306 | |
307 | /* | |
308 | * Exit... | |
309 | */ | |
310 | ||
311 | exit(s); | |
8dbf3e6a | 312 | } |
313 | #endif /* !WIN32 */ | |
314 | ||
315 | ||
bd0b97ff | 316 | /* |
4e243213 | 317 | * End of "$Id: lpr.c,v 1.11 2000/02/24 15:20:18 mike Exp $". |
bd0b97ff | 318 | */ |