File: pdftops.c

package info (click to toggle)
cups 1.4.4-7+squeeze10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 25,548 kB
  • ctags: 9,583
  • sloc: ansic: 138,214; sh: 37,926; cpp: 25,469; makefile: 3,285; perl: 190; python: 127; php: 28
file content (599 lines) | stat: -rw-r--r-- 14,052 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
 * "$Id: pdftops.c 8675 2009-05-22 19:30:39Z mike $"
 *
 *   PDF to PostScript filter front-end for the Common UNIX Printing
 *   System (CUPS).
 *
 *   Copyright 2007-2009 by Apple Inc.
 *   Copyright 1997-2006 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Apple Inc. and are protected by Federal copyright
 *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
 *   which should have been included with this file.  If this file is
 *   file is missing or damaged, see the license at "http://www.cups.org/".
 *
 * Contents:
 *
 *   main()       - Main entry for filter...
 *   cancel_job() - Flag the job as canceled.
 */

/*
 * Include necessary headers...
 */

#include <cups/cups.h>
#include <cups/string.h>
#include <cups/i18n.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>


/*
 * Local functions...
 */

static void		cancel_job(int sig);


/*
 * Local globals...
 */

static int		job_canceled = 0;


/*
 * 'main()' - Main entry for filter...
 */

int					/* O - Exit status */
main(int  argc,				/* I - Number of command-line args */
     char *argv[])			/* I - Command-line arguments */
{
  int		fd;			/* Copy file descriptor */
  char		*filename,		/* PDF file to convert */
		tempfile[1024];		/* Temporary file */
  char		buffer[8192];		/* Copy buffer */
  int		bytes;			/* Bytes copied */
  int		num_options;		/* Number of options */
  cups_option_t	*options;		/* Options */
  const char	*val;			/* Option value */
  int		orientation,		/* Output orientation */
		fit;			/* Fit output to default page size? */
  ppd_file_t	*ppd;			/* PPD file */
  ppd_size_t	*size;			/* Current page size */
  int		pdf_pid,		/* Process ID for pdftops */
		pdf_argc,		/* Number of args for pdftops */
		pstops_pid,		/* Process ID of pstops filter */
		pstops_pipe[2],		/* Pipe to pstops filter */
		wait_children,		/* Number of child processes left */
		wait_pid,		/* Process ID from wait() */
		wait_status,		/* Status from child */
		exit_status = 0;	/* Exit status */
  char		*pdf_argv[100],		/* Arguments for pdftops/gs */
		pdf_width[255],		/* Paper width */
		pdf_height[255],	/* Paper height */
		pstops_path[1024],	/* Path to pstops program */
		*pstops_argv[7],	/* Arguments for pstops filter */
		*pstops_options,	/* Options for pstops filter */
		*pstops_start,		/* Start of pstops filter option */
		*pstops_end;		/* End of pstops filter option */
  const char	*cups_serverbin;	/* CUPS_SERVERBIN environment variable */
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
  struct sigaction action;		/* Actions for POSIX signals */
#endif /* HAVE_SIGACTION && !HAVE_SIGSET */


 /*
  * Make sure status messages are not buffered...
  */

  setbuf(stderr, NULL);

 /*
  * Make sure we have the right number of arguments for CUPS!
  */

  if (argc < 6 || argc > 7)
  {
    _cupsLangPrintf(stderr,
                    _("Usage: %s job user title copies options [filename]\n"),
                    argv[0]);
    return (1);
  }

 /*
  * Register a signal handler to cleanly cancel a job.
  */

#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
  sigset(SIGTERM, cancel_job);
#elif defined(HAVE_SIGACTION)
  memset(&action, 0, sizeof(action));

  sigemptyset(&action.sa_mask);
  action.sa_handler = cancel_job;
  sigaction(SIGTERM, &action, NULL);
#else
  signal(SIGTERM, cancel_job);
#endif /* HAVE_SIGSET */

 /*
  * Copy stdin if needed...
  */

  if (argc == 6)
  {
   /*
    * Copy stdin to a temp file...
    */

    if ((fd = cupsTempFd(tempfile, sizeof(tempfile))) < 0)
    {
      _cupsLangPrintError(_("ERROR: Unable to copy PDF file"));
      return (1);
    }

    fprintf(stderr, "DEBUG: pdftops - copying to temp print file \"%s\"\n",
            tempfile);

    while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
      write(fd, buffer, bytes);

    close(fd);

    filename = tempfile;
  }
  else
  {
   /*
    * Use the filename on the command-line...
    */

    filename    = argv[6];
    tempfile[0] = '\0';
  }

 /*
  * Load the PPD file and mark options...
  */

  ppd         = ppdOpenFile(getenv("PPD"));
  num_options = cupsParseOptions(argv[5], 0, &options);

  ppdMarkDefaults(ppd);
  cupsMarkOptions(ppd, num_options, options);

 /*
  * Build the pstops command-line...
  */

  if ((cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
    cups_serverbin = CUPS_SERVERBIN;

  snprintf(pstops_path, sizeof(pstops_path), "%s/filter/pstops",
           cups_serverbin);

  pstops_options = strdup(argv[5]);

  if ((pstops_start = strstr(pstops_options, "fitplot")) != NULL &&
      (!pstops_start[7] || isspace(pstops_start[7] & 255)))
  {
   /*
    * Strip [no]fitplot option...
    */

    pstops_end = pstops_start + 7;

    if ((pstops_start - pstops_options) >= 2 &&
        !strncmp(pstops_start - 2, "no", 2))
      pstops_start -= 2;

    while (*pstops_end && isspace(*pstops_end & 255))
      pstops_end ++;

    _cups_strcpy(pstops_start, pstops_end);
  }

  if ((pstops_start = strstr(pstops_options, "fit-to-page")) != NULL &&
      (!pstops_start[11] || isspace(pstops_start[11] & 255)))
  {
   /*
    * Strip [no]fit-to-page option...
    */

    pstops_end = pstops_start + 11;

    if ((pstops_start - pstops_options) >= 2 &&
        !strncmp(pstops_start - 2, "no", 2))
      pstops_start -= 2;

    while (*pstops_end && isspace(*pstops_end & 255))
      pstops_end ++;

    _cups_strcpy(pstops_start, pstops_end);
  }

  if ((pstops_start = strstr(pstops_options, "landscape")) != NULL &&
      (!pstops_start[9] || isspace(pstops_start[9] & 255)))
  {
   /*
    * Strip [no]landscape option...
    */

    pstops_end = pstops_start + 9;

    if ((pstops_start - pstops_options) >= 2 &&
        !strncmp(pstops_start - 2, "no", 2))
      pstops_start -= 2;

    while (*pstops_end && isspace(*pstops_end & 255))
      pstops_end ++;

    _cups_strcpy(pstops_start, pstops_end);
  }

  if ((pstops_start = strstr(pstops_options, "orientation-requested=")) != NULL)
  {
   /*
    * Strip [no]fitplot option...
    */

    pstops_end = pstops_start + 22;
    while (*pstops_end && !isspace(*pstops_end & 255))
      pstops_end ++;

    _cups_strcpy(pstops_start, pstops_end);
  }

  pstops_argv[0] = argv[0];		/* Printer */
  pstops_argv[1] = argv[1];		/* Job */
  pstops_argv[2] = argv[2];		/* User */
  pstops_argv[3] = argv[3];		/* Title */
  pstops_argv[4] = argv[4];		/* Copies */
  pstops_argv[5] = pstops_options;	/* Options */
  pstops_argv[6] = NULL;

 /*
  * Build the command-line for the pdftops or gs filter...
  */

#ifdef HAVE_PDFTOPS
  pdf_argv[0] = (char *)"pdftops";
  pdf_argc    = 1;
#else
  pdf_argv[0] = (char *)"gs";
  pdf_argv[1] = (char *)"-q";
  pdf_argv[2] = (char *)"-dNOPAUSE";
  pdf_argv[3] = (char *)"-dBATCH";
  pdf_argv[4] = (char *)"-dSAFER";
  pdf_argv[5] = (char *)"-sDEVICE=pswrite";
  pdf_argv[6] = (char *)"-sOUTPUTFILE=%stdout";
  pdf_argc    = 7;
#endif /* HAVE_PDFTOPS */

  if (ppd)
  {
   /*
    * Set language level and TrueType font handling...
    */

    if (ppd->language_level == 1)
    {
#ifdef HAVE_PDFTOPS
      pdf_argv[pdf_argc++] = (char *)"-level1";
      pdf_argv[pdf_argc++] = (char *)"-noembtt";
#else
      pdf_argv[pdf_argc++] = (char *)"-dLanguageLevel=1";
#endif /* HAVE_PDFTOPS */
    }
    else if (ppd->language_level == 2)
    {
#ifdef HAVE_PDFTOPS
      pdf_argv[pdf_argc++] = (char *)"-level2";
      if (!ppd->ttrasterizer)
	pdf_argv[pdf_argc++] = (char *)"-noembtt";
#else
      pdf_argv[pdf_argc++] = (char *)"-dLanguageLevel=2";
#endif /* HAVE_PDFTOPS */
    }
    else
#ifdef HAVE_PDFTOPS
      pdf_argv[pdf_argc++] = (char *)"-level3";
#else
      pdf_argv[pdf_argc++] = (char *)"-dLanguageLevel=3";
#endif /* HAVE_PDFTOPS */

    if ((val = cupsGetOption("fitplot", num_options, options)) == NULL)
      val = cupsGetOption("fit-to-page", num_options, options);

    if (val && strcasecmp(val, "no") && strcasecmp(val, "off") &&
	strcasecmp(val, "false"))
      fit = 1;
    else
      fit = 0;

   /*
    * Set output page size...
    */

    size = ppdPageSize(ppd, NULL);
    if (size && fit)
    {
     /*
      * Got the size, now get the orientation...
      */

      orientation = 0;

      if ((val = cupsGetOption("landscape", num_options, options)) != NULL)
      {
	if (strcasecmp(val, "no") != 0 && strcasecmp(val, "off") != 0 &&
	    strcasecmp(val, "false") != 0)
	  orientation = 1;
      }
      else if ((val = cupsGetOption("orientation-requested", num_options, options)) != NULL)
      {
       /*
	* Map IPP orientation values to 0 to 3:
	*
	*   3 = 0 degrees   = 0
	*   4 = 90 degrees  = 1
	*   5 = -90 degrees = 3
	*   6 = 180 degrees = 2
	*/

	orientation = atoi(val) - 3;
	if (orientation >= 2)
	  orientation ^= 1;
      }

#ifdef HAVE_PDFTOPS
      if (orientation & 1)
      {
	snprintf(pdf_width, sizeof(pdf_width), "%.0f", size->length);
	snprintf(pdf_height, sizeof(pdf_height), "%.0f", size->width);
      }
      else
      {
	snprintf(pdf_width, sizeof(pdf_width), "%.0f", size->width);
	snprintf(pdf_height, sizeof(pdf_height), "%.0f", size->length);
      }

      pdf_argv[pdf_argc++] = (char *)"-paperw";
      pdf_argv[pdf_argc++] = pdf_width;
      pdf_argv[pdf_argc++] = (char *)"-paperh";
      pdf_argv[pdf_argc++] = pdf_height;
      pdf_argv[pdf_argc++] = (char *)"-expand";

#else
      if (orientation & 1)
      {
	snprintf(pdf_width, sizeof(pdf_width), "-dDEVICEWIDTHPOINTS=%.0f",
	         size->length);
	snprintf(pdf_height, sizeof(pdf_height), "-dDEVICEHEIGHTPOINTS=%.0f",
	         size->width);
      }
      else
      {
	snprintf(pdf_width, sizeof(pdf_width), "-dDEVICEWIDTHPOINTS=%.0f",
	         size->width);
	snprintf(pdf_height, sizeof(pdf_height), "-dDEVICEHEIGHTPOINTS=%.0f",
	         size->length);
      }

      pdf_argv[pdf_argc++] = pdf_width;
      pdf_argv[pdf_argc++] = pdf_height;
#endif /* HAVE_PDFTOPS */
    }
  }

#ifdef HAVE_PDFTOPS
  pdf_argv[pdf_argc++] = filename;
  pdf_argv[pdf_argc++] = (char *)"-";
#else
  pdf_argv[pdf_argc++] = (char *)"-c";
  pdf_argv[pdf_argc++] = (char *)"save pop";
  pdf_argv[pdf_argc++] = (char *)"-f";
  pdf_argv[pdf_argc++] = filename;
#endif /* HAVE_PDFTOPS */

  pdf_argv[pdf_argc] = NULL;

 /*
  * Execute "pdftops/gs | pstops"...
  */

  if (pipe(pstops_pipe))
  {
    _cupsLangPrintError(_("ERROR: Unable to create pipe"));

    exit_status = 1;
    goto error;
  }

  if ((pdf_pid = fork()) == 0)
  {
   /*
    * Child comes here...
    */

    dup2(pstops_pipe[1], 1);
    close(pstops_pipe[0]);
    close(pstops_pipe[1]);

#ifdef HAVE_PDFTOPS
    execv(CUPS_PDFTOPS, pdf_argv);
    _cupsLangPrintError(_("ERROR: Unable to execute pdftops program"));
#else
    execv(CUPS_GHOSTSCRIPT, pdf_argv);
    _cupsLangPrintError(_("ERROR: Unable to execute gs program"));
#endif /* HAVE_PDFTOPS */

    exit(1);
  }
  else if (pdf_pid < 0)
  {
   /*
    * Unable to fork!
    */

#ifdef HAVE_PDFTOPS
    _cupsLangPrintError(_("ERROR: Unable to execute pdftops program"));
#else
    _cupsLangPrintError(_("ERROR: Unable to execute gs program"));
#endif /* HAVE_PDFTOPS */

    exit_status = 1;
    goto error;
  }

  fprintf(stderr, "DEBUG: Started filter %s (PID %d)\n", pdf_argv[0], pdf_pid);

  if ((pstops_pid = fork()) == 0)
  {
   /*
    * Child comes here...
    */

    dup2(pstops_pipe[0], 0);
    close(pstops_pipe[0]);
    close(pstops_pipe[1]);

    execv(pstops_path, pstops_argv);
    _cupsLangPrintError(_("ERROR: Unable to execute pstops program"));

    exit(1);
  }
  else if (pstops_pid < 0)
  {
   /*
    * Unable to fork!
    */

    _cupsLangPrintError(_("ERROR: Unable to execute pstops program"));

    exit_status = 1;
    goto error;
  }

  fprintf(stderr, "DEBUG: Started filter pstops (PID %d)\n", pstops_pid);

  close(pstops_pipe[0]);
  close(pstops_pipe[1]);

 /*
  * Wait for the child processes to exit...
  */

  wait_children = 2;

  while (wait_children > 0)
  {
   /*
    * Wait until we get a valid process ID or the job is canceled...
    */

    while ((wait_pid = wait(&wait_status)) < 0 && errno == EINTR)
    {
      if (job_canceled)
      {
	kill(pdf_pid, SIGTERM);
	kill(pstops_pid, SIGTERM);

	job_canceled = 0;
      }
    }

    if (wait_pid < 0)
      break;

    wait_children --;

   /*
    * Report child status...
    */

    if (wait_status)
    {
      if (WIFEXITED(wait_status))
      {
	exit_status = WEXITSTATUS(wait_status);

        fprintf(stderr, "DEBUG: PID %d (%s) stopped with status %d!\n",
	        wait_pid,		
#ifdef HAVE_PDFTOPS
                wait_pid == pdf_pid ? "pdftops" : "pstops",
#else
                wait_pid == pdf_pid ? "gs" : "pstops",
#endif /* HAVE_PDFTOPS */
		exit_status);
      }
      else if (WTERMSIG(wait_status) == SIGTERM)
      {
        fprintf(stderr,
	        "DEBUG: PID %d (%s) was terminated normally with signal %d!\n",
	        wait_pid,		
#ifdef HAVE_PDFTOPS
                wait_pid == pdf_pid ? "pdftops" : "pstops",
#else
                wait_pid == pdf_pid ? "gs" : "pstops",
#endif /* HAVE_PDFTOPS */
		exit_status);
      }
      else
      {
	exit_status = WTERMSIG(wait_status);

        fprintf(stderr, "DEBUG: PID %d (%s) crashed on signal %d!\n", wait_pid,		
#ifdef HAVE_PDFTOPS
                wait_pid == pdf_pid ? "pdftops" : "pstops",
#else
                wait_pid == pdf_pid ? "gs" : "pstops",
#endif /* HAVE_PDFTOPS */
		exit_status);
      }
    }
    else
    {
      fprintf(stderr, "DEBUG: PID %d (%s) exited with no errors.\n", wait_pid,		
#ifdef HAVE_PDFTOPS
	      wait_pid == pdf_pid ? "pdftops" : "pstops");
#else
	      wait_pid == pdf_pid ? "gs" : "pstops");
#endif /* HAVE_PDFTOPS */
    }
  }

 /*
  * Cleanup and exit...
  */

  error:

  if (tempfile[0])
    unlink(tempfile);

  return (exit_status);
}


/*
 * 'cancel_job()' - Flag the job as canceled.
 */

static void
cancel_job(int sig)			/* I - Signal number (unused) */
{
  (void)sig;

  job_canceled = 1;
}


/*
 * End of "$Id: pdftops.c 8675 2009-05-22 19:30:39Z mike $".
 */