Line 0
Link Here
|
|
|
1 |
/* |
2 |
* "$Id$" |
3 |
* |
4 |
* PDF to PostScript filter front-end for the Common UNIX Printing |
5 |
* System (CUPS). |
6 |
* |
7 |
* Copyright 2007-2009 by Apple Inc. |
8 |
* Copyright 1997-2006 by Easy Software Products. |
9 |
* |
10 |
* These coded instructions, statements, and computer programs are the |
11 |
* property of Apple Inc. and are protected by Federal copyright |
12 |
* law. Distribution and use rights are outlined in the file "LICENSE.txt" |
13 |
* which should have been included with this file. If this file is |
14 |
* file is missing or damaged, see the license at "http://www.cups.org/". |
15 |
* |
16 |
* Contents: |
17 |
* |
18 |
* main() - Main entry for filter... |
19 |
* cancel_job() - Flag the job as canceled. |
20 |
*/ |
21 |
|
22 |
/* |
23 |
* Include necessary headers... |
24 |
*/ |
25 |
|
26 |
#include <cups/cups.h> |
27 |
#include <cups/string.h> |
28 |
#include <cups/i18n.h> |
29 |
#include <signal.h> |
30 |
#include <sys/wait.h> |
31 |
#include <errno.h> |
32 |
|
33 |
|
34 |
/* |
35 |
* Local functions... |
36 |
*/ |
37 |
|
38 |
static void cancel_job(int sig); |
39 |
|
40 |
|
41 |
/* |
42 |
* Local globals... |
43 |
*/ |
44 |
|
45 |
static int job_canceled = 0; |
46 |
|
47 |
|
48 |
/* |
49 |
* 'main()' - Main entry for filter... |
50 |
*/ |
51 |
|
52 |
int /* O - Exit status */ |
53 |
main(int argc, /* I - Number of command-line args */ |
54 |
char *argv[]) /* I - Command-line arguments */ |
55 |
{ |
56 |
int fd; /* Copy file descriptor */ |
57 |
char *filename, /* PDF file to convert */ |
58 |
tempfile[1024]; /* Temporary file */ |
59 |
char buffer[8192]; /* Copy buffer */ |
60 |
int bytes; /* Bytes copied */ |
61 |
int num_options; /* Number of options */ |
62 |
cups_option_t *options; /* Options */ |
63 |
const char *val; /* Option value */ |
64 |
int orientation, /* Output orientation */ |
65 |
fit; /* Fit output to default page size? */ |
66 |
ppd_file_t *ppd; /* PPD file */ |
67 |
ppd_size_t *size; /* Current page size */ |
68 |
int pdfpid, /* Process ID for pdftops */ |
69 |
pdfwaitpid, /* Process ID from wait() */ |
70 |
pdfstatus, /* Status from pdftops */ |
71 |
pdfargc; /* Number of args for pdftops */ |
72 |
char *pdfargv[100], /* Arguments for pdftops/gs */ |
73 |
pdfwidth[255], /* Paper width */ |
74 |
pdfheight[255]; /* Paper height */ |
75 |
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) |
76 |
struct sigaction action; /* Actions for POSIX signals */ |
77 |
#endif /* HAVE_SIGACTION && !HAVE_SIGSET */ |
78 |
|
79 |
|
80 |
/* |
81 |
* Make sure status messages are not buffered... |
82 |
*/ |
83 |
|
84 |
setbuf(stderr, NULL); |
85 |
|
86 |
/* |
87 |
* Make sure we have the right number of arguments for CUPS! |
88 |
*/ |
89 |
|
90 |
if (argc < 6 || argc > 7) |
91 |
{ |
92 |
_cupsLangPrintf(stderr, |
93 |
_("Usage: %s job user title copies options [filename]\n"), |
94 |
argv[0]); |
95 |
return (1); |
96 |
} |
97 |
|
98 |
/* |
99 |
* Register a signal handler to cleanly cancel a job. |
100 |
*/ |
101 |
|
102 |
#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ |
103 |
sigset(SIGTERM, cancel_job); |
104 |
#elif defined(HAVE_SIGACTION) |
105 |
memset(&action, 0, sizeof(action)); |
106 |
|
107 |
sigemptyset(&action.sa_mask); |
108 |
action.sa_handler = cancel_job; |
109 |
sigaction(SIGTERM, &action, NULL); |
110 |
#else |
111 |
signal(SIGTERM, cancel_job); |
112 |
#endif /* HAVE_SIGSET */ |
113 |
|
114 |
/* |
115 |
* Copy stdin if needed... |
116 |
*/ |
117 |
|
118 |
if (argc == 6) |
119 |
{ |
120 |
/* |
121 |
* Copy stdin to a temp file... |
122 |
*/ |
123 |
|
124 |
if ((fd = cupsTempFd(tempfile, sizeof(tempfile))) < 0) |
125 |
{ |
126 |
_cupsLangPrintf(stderr, _("ERROR: Unable to copy PDF file: %s\n"), |
127 |
strerror(errno)); |
128 |
return (1); |
129 |
} |
130 |
|
131 |
fprintf(stderr, "DEBUG: pdftops - copying to temp print file \"%s\"\n", |
132 |
tempfile); |
133 |
|
134 |
while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0) |
135 |
write(fd, buffer, bytes); |
136 |
|
137 |
close(fd); |
138 |
|
139 |
filename = tempfile; |
140 |
} |
141 |
else |
142 |
{ |
143 |
/* |
144 |
* Use the filename on the command-line... |
145 |
*/ |
146 |
|
147 |
filename = argv[6]; |
148 |
tempfile[0] = '\0'; |
149 |
} |
150 |
|
151 |
/* |
152 |
* Load the PPD file and mark options... |
153 |
*/ |
154 |
|
155 |
ppd = ppdOpenFile(getenv("PPD")); |
156 |
num_options = cupsParseOptions(argv[5], 0, &options); |
157 |
|
158 |
ppdMarkDefaults(ppd); |
159 |
cupsMarkOptions(ppd, num_options, options); |
160 |
|
161 |
/* |
162 |
* Build the command-line for the pdftops or gs filter... |
163 |
*/ |
164 |
|
165 |
#ifdef HAVE_PDFTOPS |
166 |
pdfargv[0] = (char *)"pdftops"; |
167 |
pdfargc = 1; |
168 |
#else |
169 |
pdfargv[0] = (char *)"gs"; |
170 |
pdfargv[1] = (char *)"-q"; |
171 |
pdfargv[2] = (char *)"-dNOPAUSE"; |
172 |
pdfargv[3] = (char *)"-dBATCH"; |
173 |
pdfargv[4] = (char *)"-dSAFER"; |
174 |
pdfargv[5] = (char *)"-sDEVICE=pswrite"; |
175 |
pdfargv[6] = (char *)"-sOUTPUTFILE=%stdout"; |
176 |
pdfargc = 7; |
177 |
#endif /* HAVE_PDFTOPS */ |
178 |
|
179 |
if (ppd) |
180 |
{ |
181 |
/* |
182 |
* Set language level and TrueType font handling... |
183 |
*/ |
184 |
|
185 |
if (ppd->language_level == 1) |
186 |
{ |
187 |
#ifdef HAVE_PDFTOPS |
188 |
pdfargv[pdfargc++] = (char *)"-level1"; |
189 |
pdfargv[pdfargc++] = (char *)"-noembtt"; |
190 |
#else |
191 |
pdfargv[pdfargc++] = (char *)"-dLanguageLevel=1"; |
192 |
#endif /* HAVE_PDFTOPS */ |
193 |
} |
194 |
else if (ppd->language_level == 2) |
195 |
{ |
196 |
#ifdef HAVE_PDFTOPS |
197 |
pdfargv[pdfargc++] = (char *)"-level2"; |
198 |
if (!ppd->ttrasterizer) |
199 |
pdfargv[pdfargc++] = (char *)"-noembtt"; |
200 |
#else |
201 |
pdfargv[pdfargc++] = (char *)"-dLanguageLevel=2"; |
202 |
#endif /* HAVE_PDFTOPS */ |
203 |
} |
204 |
else |
205 |
#ifdef HAVE_PDFTOPS |
206 |
pdfargv[pdfargc++] = (char *)"-level3"; |
207 |
#else |
208 |
pdfargv[pdfargc++] = (char *)"-dLanguageLevel=3"; |
209 |
#endif /* HAVE_PDFTOPS */ |
210 |
|
211 |
if ((val = cupsGetOption("fitplot", num_options, options)) == NULL) |
212 |
val = cupsGetOption("fit-to-page", num_options, options); |
213 |
|
214 |
if (val && strcasecmp(val, "no") && strcasecmp(val, "off") && |
215 |
strcasecmp(val, "false")) |
216 |
fit = 1; |
217 |
else |
218 |
fit = 0; |
219 |
|
220 |
/* |
221 |
* Set output page size... |
222 |
*/ |
223 |
|
224 |
size = ppdPageSize(ppd, NULL); |
225 |
if (size && fit) |
226 |
{ |
227 |
/* |
228 |
* Got the size, now get the orientation... |
229 |
*/ |
230 |
|
231 |
orientation = 0; |
232 |
|
233 |
if ((val = cupsGetOption("landscape", num_options, options)) != NULL) |
234 |
{ |
235 |
if (strcasecmp(val, "no") != 0 && strcasecmp(val, "off") != 0 && |
236 |
strcasecmp(val, "false") != 0) |
237 |
orientation = 1; |
238 |
} |
239 |
else if ((val = cupsGetOption("orientation-requested", num_options, options)) != NULL) |
240 |
{ |
241 |
/* |
242 |
* Map IPP orientation values to 0 to 3: |
243 |
* |
244 |
* 3 = 0 degrees = 0 |
245 |
* 4 = 90 degrees = 1 |
246 |
* 5 = -90 degrees = 3 |
247 |
* 6 = 180 degrees = 2 |
248 |
*/ |
249 |
|
250 |
orientation = atoi(val) - 3; |
251 |
if (orientation >= 2) |
252 |
orientation ^= 1; |
253 |
} |
254 |
|
255 |
#ifdef HAVE_PDFTOPS |
256 |
if (orientation & 1) |
257 |
{ |
258 |
snprintf(pdfwidth, sizeof(pdfwidth), "%.0f", size->length); |
259 |
snprintf(pdfheight, sizeof(pdfheight), "%.0f", size->width); |
260 |
} |
261 |
else |
262 |
{ |
263 |
snprintf(pdfwidth, sizeof(pdfwidth), "%.0f", size->width); |
264 |
snprintf(pdfheight, sizeof(pdfheight), "%.0f", size->length); |
265 |
} |
266 |
|
267 |
pdfargv[pdfargc++] = (char *)"-paperw"; |
268 |
pdfargv[pdfargc++] = pdfwidth; |
269 |
pdfargv[pdfargc++] = (char *)"-paperh"; |
270 |
pdfargv[pdfargc++] = pdfheight; |
271 |
pdfargv[pdfargc++] = (char *)"-expand"; |
272 |
|
273 |
#else |
274 |
if (orientation & 1) |
275 |
{ |
276 |
snprintf(pdfwidth, sizeof(pdfwidth), "-dDEVICEWIDTHPOINTS=%.0f", |
277 |
size->length); |
278 |
snprintf(pdfheight, sizeof(pdfheight), "-dDEVICEHEIGHTPOINTS=%.0f", |
279 |
size->width); |
280 |
} |
281 |
else |
282 |
{ |
283 |
snprintf(pdfwidth, sizeof(pdfwidth), "-dDEVICEWIDTHPOINTS=%.0f", |
284 |
size->width); |
285 |
snprintf(pdfheight, sizeof(pdfheight), "-dDEVICEHEIGHTPOINTS=%.0f", |
286 |
size->length); |
287 |
} |
288 |
|
289 |
pdfargv[pdfargc++] = pdfwidth; |
290 |
pdfargv[pdfargc++] = pdfheight; |
291 |
#endif /* HAVE_PDFTOPS */ |
292 |
} |
293 |
} |
294 |
|
295 |
#ifdef HAVE_PDFTOPS |
296 |
pdfargv[pdfargc++] = filename; |
297 |
pdfargv[pdfargc++] = (char *)"-"; |
298 |
#else |
299 |
pdfargv[pdfargc++] = (char *)"-c"; |
300 |
pdfargv[pdfargc++] = (char *)"save pop"; |
301 |
pdfargv[pdfargc++] = (char *)"-f"; |
302 |
pdfargv[pdfargc++] = filename; |
303 |
#endif /* HAVE_PDFTOPS */ |
304 |
|
305 |
pdfargv[pdfargc] = NULL; |
306 |
|
307 |
if ((pdfpid = fork()) == 0) |
308 |
{ |
309 |
/* |
310 |
* Child comes here... |
311 |
*/ |
312 |
|
313 |
#ifdef HAVE_PDFTOPS |
314 |
execv(CUPS_PDFTOPS, pdfargv); |
315 |
_cupsLangPrintf(stderr, _("ERROR: Unable to execute pdftops program: %s\n"), |
316 |
strerror(errno)); |
317 |
#else |
318 |
execv(CUPS_GHOSTSCRIPT, pdfargv); |
319 |
_cupsLangPrintf(stderr, _("ERROR: Unable to execute gs program: %s\n"), |
320 |
strerror(errno)); |
321 |
#endif /* HAVE_PDFTOPS */ |
322 |
|
323 |
exit(1); |
324 |
} |
325 |
else if (pdfpid < 0) |
326 |
{ |
327 |
/* |
328 |
* Unable to fork! |
329 |
*/ |
330 |
|
331 |
#ifdef HAVE_PDFTOPS |
332 |
_cupsLangPrintf(stderr, _("ERROR: Unable to execute pdftops program: %s\n"), |
333 |
strerror(errno)); |
334 |
#else |
335 |
_cupsLangPrintf(stderr, _("ERROR: Unable to execute gs program: %s\n"), |
336 |
strerror(errno)); |
337 |
#endif /* HAVE_PDFTOPS */ |
338 |
|
339 |
pdfstatus = 1; |
340 |
} |
341 |
else |
342 |
{ |
343 |
/* |
344 |
* Parent comes here... |
345 |
*/ |
346 |
|
347 |
while ((pdfwaitpid = wait(&pdfstatus)) != pdfpid && errno == EINTR) |
348 |
{ |
349 |
/* |
350 |
* Wait until we get a valid process ID or the job is canceled... |
351 |
*/ |
352 |
|
353 |
if (job_canceled) |
354 |
{ |
355 |
kill(pdfpid, SIGTERM); |
356 |
job_canceled = 0; |
357 |
} |
358 |
} |
359 |
|
360 |
if (pdfstatus) |
361 |
{ |
362 |
if (WIFEXITED(pdfstatus)) |
363 |
{ |
364 |
pdfstatus = WEXITSTATUS(pdfstatus); |
365 |
|
366 |
_cupsLangPrintf(stderr, |
367 |
_("ERROR: pdftops filter exited with status %d!\n"), |
368 |
pdfstatus); |
369 |
} |
370 |
else |
371 |
{ |
372 |
pdfstatus = WTERMSIG(pdfstatus); |
373 |
|
374 |
_cupsLangPrintf(stderr, |
375 |
_("ERROR: pdftops filter crashed on signal %d!\n"), |
376 |
pdfstatus); |
377 |
} |
378 |
} |
379 |
} |
380 |
|
381 |
/* |
382 |
* Cleanup and exit... |
383 |
*/ |
384 |
|
385 |
if (tempfile[0]) |
386 |
unlink(tempfile); |
387 |
|
388 |
return (pdfstatus); |
389 |
} |
390 |
|
391 |
|
392 |
/* |
393 |
* 'cancel_job()' - Flag the job as canceled. |
394 |
*/ |
395 |
|
396 |
static void |
397 |
cancel_job(int sig) /* I - Signal number (unused) */ |
398 |
{ |
399 |
(void)sig; |
400 |
|
401 |
job_canceled = 1; |
402 |
} |
403 |
|
404 |
|
405 |
/* |
406 |
* End of "$Id$". |
407 |
*/ |
0 |
+ native |
408 |
+ native |
1 |
+ Id |
409 |
+ Id |