]>
Commit | Line | Data |
---|---|---|
bd0b97ff | 1 | /* |
8dbf3e6a | 2 | * "$Id: lpr.c,v 1.8 1999/10/26 14:40:55 mike Exp $" |
bd0b97ff | 3 | * |
4 | * "lpr" command for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-1999 by Easy Software Products. | |
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 | ||
47 | void sighandler(void); | |
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 */ | |
79 | ||
80 | ||
81 | silent = 0; | |
7dd958a9 | 82 | deletefile = 0; |
bd0b97ff | 83 | dest = cupsGetDefault(); |
84 | num_options = 0; | |
85 | options = NULL; | |
86 | num_files = 0; | |
87 | title = NULL; | |
88 | ||
89 | for (i = 1; i < argc; i ++) | |
90 | if (argv[i][0] == '-') | |
91 | switch (argv[i][1]) | |
92 | { | |
93 | case 'i' : /* indent */ | |
94 | case 'w' : /* width */ | |
95 | if (argv[i][2] == '\0') | |
96 | i ++; | |
97 | case 'c' : /* CIFPLOT */ | |
98 | case 'd' : /* DVI */ | |
99 | case 'f' : /* FORTRAN */ | |
100 | case 'g' : /* plot */ | |
101 | case 'n' : /* Ditroff */ | |
102 | case 't' : /* Troff */ | |
103 | case 'v' : /* Raster image */ | |
104 | fprintf(stderr, "Warning: \'%c\' format modifier not supported - output may not be correct!\n", | |
105 | argv[i][1]); | |
106 | break; | |
107 | ||
5b187d9c | 108 | case 'o' : /* Option */ |
109 | if (argv[i][2] != '\0') | |
110 | num_options = cupsParseOptions(argv[i] + 2, num_options, &options); | |
111 | else | |
112 | { | |
113 | i ++; | |
114 | num_options = cupsParseOptions(argv[i], num_options, &options); | |
115 | } | |
116 | break; | |
117 | ||
bd0b97ff | 118 | case 'l' : /* Literal/raw */ |
119 | num_options = cupsParseOptions("raw", num_options, &options); | |
120 | break; | |
121 | ||
122 | case 'p' : /* Prettyprint */ | |
123 | num_options = cupsParseOptions("prettyprint", num_options, &options); | |
124 | break; | |
125 | ||
126 | case 'h' : /* Suppress burst page */ | |
127 | case 's' : /* Don't use symlinks */ | |
128 | break; | |
129 | ||
7dd958a9 | 130 | case 'm' : /* Mail on completion */ |
bd0b97ff | 131 | fputs("Warning: email notification is not supported!\n", stderr); |
132 | break; | |
133 | ||
7dd958a9 | 134 | case 'r' : /* Remove file after printing */ |
135 | deletefile = 1; | |
136 | break; | |
137 | ||
bd0b97ff | 138 | case 'P' : /* Destination printer or class */ |
139 | if (argv[i][2] != '\0') | |
140 | dest = argv[i] + 2; | |
141 | else | |
142 | { | |
143 | i ++; | |
144 | dest = argv[i]; | |
145 | } | |
146 | break; | |
147 | ||
148 | case '#' : /* Number of copies */ | |
149 | if (argv[i][2] != '\0') | |
150 | num_copies = atoi(argv[i] + 2); | |
151 | else | |
152 | { | |
153 | i ++; | |
154 | num_copies = atoi(argv[i]); | |
155 | } | |
156 | ||
157 | if (num_copies < 1 || num_copies > 100) | |
158 | { | |
159 | fputs("lpr: Number copies must be between 1 and 100.\n", stderr); | |
160 | return (1); | |
161 | } | |
162 | ||
163 | sprintf(buffer, "%d", num_copies); | |
164 | num_options = cupsAddOption("copies", buffer, num_options, &options); | |
165 | break; | |
166 | ||
167 | case 'C' : /* Class */ | |
168 | case 'J' : /* Job name */ | |
169 | case 'T' : /* Title */ | |
170 | if (argv[i][2] != '\0') | |
171 | title = argv[i] + 2; | |
172 | else | |
173 | { | |
174 | i ++; | |
175 | title = argv[i]; | |
176 | } | |
177 | break; | |
178 | ||
179 | default : | |
180 | fprintf(stderr, "lpr: Unknown option \'%c\'!\n", argv[i][1]); | |
181 | return (1); | |
182 | } | |
183 | else | |
184 | { | |
185 | /* | |
186 | * Print a file... | |
187 | */ | |
188 | ||
189 | if (dest == NULL) | |
190 | { | |
191 | fputs("lpr: error - no default destination available.\n", stderr); | |
192 | return (1); | |
193 | } | |
194 | ||
195 | num_files ++; | |
196 | if (title) | |
197 | job_id = cupsPrintFile(dest, argv[i], title, num_options, options); | |
198 | else | |
199 | { | |
200 | char *filename; | |
201 | ||
202 | if ((filename = strrchr(argv[i], '/')) != NULL) | |
203 | filename ++; | |
204 | else | |
205 | filename = argv[i]; | |
206 | ||
207 | job_id = cupsPrintFile(dest, argv[i], filename, num_options, options); | |
208 | } | |
209 | ||
210 | if (job_id < 1) | |
211 | { | |
8dbf3e6a | 212 | fprintf(stderr, "lpr: unable to print file \'%s\' - error code %x.\n", |
213 | argv[i], cupsLastError()); | |
bd0b97ff | 214 | return (1); |
215 | } | |
7dd958a9 | 216 | else if (deletefile) |
217 | unlink(argv[i]); | |
bd0b97ff | 218 | } |
219 | ||
220 | /* | |
221 | * See if we printed anything; if not, print from stdin... | |
222 | */ | |
223 | ||
224 | if (num_files == 0) | |
225 | { | |
226 | if (dest == NULL) | |
227 | { | |
228 | fputs("lpr: error - no default destination available.\n", stderr); | |
229 | return (1); | |
230 | } | |
231 | ||
8dbf3e6a | 232 | #ifndef WIN32 |
233 | signal(SIGTERM, sighandler); | |
234 | #endif /* !WIN32 */ | |
235 | ||
642d82f2 | 236 | temp = fopen(cupsTempFile(tempfile, sizeof(tempfile)), "w"); |
bd0b97ff | 237 | |
238 | if (temp == NULL) | |
239 | { | |
240 | fputs("lpr: unable to create temporary file.\n", stderr); | |
241 | return (1); | |
242 | } | |
243 | ||
244 | while ((i = fread(buffer, 1, sizeof(buffer), stdin)) > 0) | |
245 | fwrite(buffer, 1, i, temp); | |
246 | ||
247 | i = ftell(temp); | |
248 | fclose(temp); | |
249 | ||
250 | if (i == 0) | |
251 | { | |
bf56a667 | 252 | fputs("lpr: standard input is empty, so no job has been sent.\n", stderr); |
bd0b97ff | 253 | return (1); |
254 | } | |
255 | ||
256 | if (title) | |
257 | job_id = cupsPrintFile(dest, tempfile, title, num_options, options); | |
258 | else | |
259 | job_id = cupsPrintFile(dest, tempfile, "(stdin)", num_options, options); | |
260 | ||
68ea7095 | 261 | unlink(tempfile); |
262 | ||
bd0b97ff | 263 | if (job_id < 1) |
264 | { | |
8dbf3e6a | 265 | fprintf(stderr, "lpr: unable to print standard input - error code %x.\n", |
266 | cupsLastError()); | |
bd0b97ff | 267 | return (1); |
268 | } | |
269 | } | |
270 | ||
271 | return (0); | |
272 | } | |
273 | ||
274 | ||
8dbf3e6a | 275 | #ifndef WIN32 |
276 | /* | |
277 | * 'sighandler()' - Signal catcher for when we print from stdin... | |
278 | */ | |
279 | ||
280 | void | |
281 | sighandler(void) | |
282 | { | |
283 | /* | |
284 | * Remove the temporary file we're using to print from stdin... | |
285 | */ | |
286 | ||
287 | unlink(tempfile); | |
288 | } | |
289 | #endif /* !WIN32 */ | |
290 | ||
291 | ||
bd0b97ff | 292 | /* |
8dbf3e6a | 293 | * End of "$Id: lpr.c,v 1.8 1999/10/26 14:40:55 mike Exp $". |
bd0b97ff | 294 | */ |