]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/cupsfilter.c
Import changes from CUPS 1.4svn-r8704.
[thirdparty/cups.git] / scheduler / cupsfilter.c
CommitLineData
bc44d920 1/*
b19ccc9e 2 * "$Id: cupsfilter.c 7952 2008-09-17 00:56:20Z mike $"
bc44d920 3 *
4 * CUPS filtering program for the Common UNIX Printing System (CUPS).
5 *
bf3816c7 6 * Copyright 2007-2009 by Apple Inc.
bc44d920 7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * Contents:
16 *
17 * main() - Main entry for the test program.
18 * compare_pids() - Compare two filter PIDs...
19 * escape_options() - Convert an options array to a string.
20 * exec_filter() - Execute a single filter.
21 * exec_filters() - Execute filters for the given file and options.
2e4ff8af 22 * get_job_file() - Get the specified job file.
bc44d920 23 * open_pipe() - Create a pipe which is closed on exec.
24 * read_cupsd_conf() - Read the cupsd.conf file to get the filter settings.
25 * set_string() - Copy and set a string.
26 * usage() - Show program usage...
27 */
28
29/*
30 * Include necessary headers...
31 */
32
33#include <cups/cups.h>
34#include <cups/i18n.h>
35#include <cups/string.h>
36#include <errno.h>
37#include "mime.h"
38#include <stdlib.h>
2e4ff8af 39#include <limits.h>
bc44d920 40#include <unistd.h>
41#include <fcntl.h>
2e4ff8af 42#include <signal.h>
bc44d920 43#include <sys/wait.h>
44#if defined(__APPLE__)
45# include <libgen.h>
46#endif /* __APPLE__ */
47
48
49/*
50 * Local globals...
51 */
52
53static char *DataDir = NULL;/* CUPS_DATADIR environment variable */
54static char *FontPath = NULL;
55 /* CUPS_FONTPATH environment variable */
56static mime_filter_t GZIPFilter = /* gziptoany filter */
57{
58 NULL, /* Source type */
59 NULL, /* Destination type */
60 0, /* Cost */
61 "gziptoany" /* Filter program to run */
62};
63static char *Path = NULL; /* PATH environment variable */
64static char *ServerBin = NULL;
65 /* CUPS_SERVERBIN environment variable */
66static char *ServerRoot = NULL;
67 /* CUPS_SERVERROOT environment variable */
68static char *RIPCache = NULL;
69 /* RIP_CACHE environment variable */
2e4ff8af
MS
70static char TempFile[1024] = "";
71 /* Temporary file */
bc44d920 72
73
74/*
75 * Local functions...
76 */
77
b19ccc9e
MS
78static void add_printer_filter(const char *command, mime_t *mime,
79 mime_type_t *printer_type,
80 const char *filter);
81static mime_type_t *add_printer_filters(const char *command,
82 mime_t *mime, const char *printer,
83 const char *ppdfile,
84 mime_type_t **prefilter_type);
85static int compare_pids(mime_filter_t *a, mime_filter_t *b);
86static char *escape_options(int num_options, cups_option_t *options);
87static int exec_filter(const char *filter, char **argv,
88 char **envp, int infd, int outfd);
1340db2d
MS
89static int exec_filters(mime_type_t *srctype,
90 cups_array_t *filters, const char *infile,
b19ccc9e
MS
91 const char *outfile, const char *ppdfile,
92 const char *printer, const char *user,
93 const char *title, int num_options,
94 cups_option_t *options);
95static void get_job_file(const char *job);
96static int open_pipe(int *fds);
97static int read_cupsd_conf(const char *filename);
98static void set_string(char **s, const char *val);
99static void sighandler(int sig);
100static void usage(const char *command, const char *opt);
bc44d920 101
102
103/*
104 * 'main()' - Main entry for the test program.
105 */
106
107int /* O - Exit status */
108main(int argc, /* I - Number of command-line args */
109 char *argv[]) /* I - Command-line arguments */
110{
111 int i; /* Looping vars */
cc0d019f 112 const char *command, /* Command name */
b19ccc9e
MS
113 *opt, /* Current option */
114 *printer; /* Printer name */
115 mime_type_t *printer_type, /* Printer MIME type */
116 *prefilter_type; /* Printer prefilter MIME type */
cc0d019f
MS
117 char *srctype, /* Source type */
118 *dsttype, /* Destination type */
119 super[MIME_MAX_SUPER], /* Super-type name */
bc44d920 120 type[MIME_MAX_TYPE]; /* Type name */
121 int compression; /* Compression of file */
122 int cost; /* Cost of filters */
123 mime_t *mime; /* MIME database */
dd1abb6b 124 char mimedir[1024]; /* MIME directory */
cc0d019f
MS
125 char *infile, /* File to filter */
126 *outfile; /* File to create */
bc44d920 127 char cupsdconf[1024]; /* cupsd.conf file */
128 const char *server_root; /* CUPS_SERVERROOT environment variable */
129 mime_type_t *src, /* Source type */
130 *dst; /* Destination type */
131 cups_array_t *filters; /* Filters for the file */
132 int num_options; /* Number of options */
133 cups_option_t *options; /* Options */
134 const char *ppdfile; /* PPD file */
cc0d019f
MS
135 const char *title, /* Title string */
136 *user; /* Username */
1340db2d
MS
137 int all_filters, /* Use all filters */
138 removeppd, /* Remove PPD file */
cc0d019f
MS
139 removeinfile; /* Remove input file */
140 int status; /* Execution status */
bc44d920 141
142
143 /*
144 * Setup defaults...
145 */
146
cc0d019f
MS
147 if ((command = strrchr(argv[0], '/')) != NULL)
148 command ++;
149 else
150 command = argv[0];
151
b19ccc9e 152 printer = !strcmp(command, "convert") ? "tofile" : "cupsfilter";
cc0d019f
MS
153 mime = NULL;
154 srctype = NULL;
91c84a35 155 compression = 0;
cc0d019f
MS
156 dsttype = "application/pdf";
157 infile = NULL;
158 outfile = NULL;
159 num_options = 0;
160 options = NULL;
161 ppdfile = NULL;
162 title = NULL;
163 user = cupsUser();
1340db2d 164 all_filters = 0;
cc0d019f
MS
165 removeppd = 0;
166 removeinfile = 0;
bc44d920 167
168 if ((server_root = getenv("CUPS_SERVERROOT")) == NULL)
169 server_root = CUPS_SERVERROOT;
170
171 snprintf(cupsdconf, sizeof(cupsdconf), "%s/cupsd.conf", server_root);
172
173 /*
174 * Process command-line arguments...
175 */
176
177 _cupsSetLocale(argv);
178
179 for (i = 1; i < argc; i ++)
180 if (argv[i][0] == '-')
181 {
182 for (opt = argv[i] + 1; *opt; opt ++)
183 switch (*opt)
184 {
185 case '-' : /* Next argument is a filename... */
186 i ++;
cc0d019f
MS
187 if (i < argc && !infile)
188 infile = argv[i];
bc44d920 189 else
cc0d019f
MS
190 usage(command, opt);
191 break;
192
193 case 'a' : /* Specify option... */
194 i ++;
195 if (i < argc)
196 num_options = cupsParseOptions(argv[i], num_options, &options);
197 else
198 usage(command, opt);
bc44d920 199 break;
200
201 case 'c' : /* Specify cupsd.conf file location... */
202 i ++;
203 if (i < argc)
cc0d019f
MS
204 {
205 if (!strcmp(command, "convert"))
206 num_options = cupsAddOption("copies", argv[i], num_options,
207 &options);
208 else
209 strlcpy(cupsdconf, argv[i], sizeof(cupsdconf));
210 }
211 else
212 usage(command, opt);
213 break;
214
215 case 'D' : /* Delete input file after conversion */
216 removeinfile = 1;
217 break;
218
1340db2d
MS
219 case 'e' : /* Use every filter from the PPD file */
220 all_filters = 1;
221 break;
222
cc0d019f
MS
223 case 'f' : /* Specify input file... */
224 i ++;
225 if (i < argc && !infile)
226 infile = argv[i];
227 else
228 usage(command, opt);
229 break;
230
231 case 'i' : /* Specify source MIME type... */
232 i ++;
233 if (i < argc)
234 {
235 if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2)
236 usage(command, opt);
237
238 srctype = argv[i];
239 }
bc44d920 240 else
cc0d019f 241 usage(command, opt);
bc44d920 242 break;
243
2e4ff8af
MS
244 case 'j' : /* Get job file or specify destination MIME type... */
245 if (strcmp(command, "convert"))
246 {
247 i ++;
248 if (i < argc)
249 {
250 get_job_file(argv[i]);
251 infile = TempFile;
252 }
253 else
254 usage(command, opt);
255
256 break;
257 }
258
bc44d920 259 case 'm' : /* Specify destination MIME type... */
260 i ++;
261 if (i < argc)
262 {
263 if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2)
cc0d019f
MS
264 usage(command, opt);
265
266 dsttype = argv[i];
bc44d920 267 }
268 else
cc0d019f 269 usage(command, opt);
bc44d920 270 break;
271
272 case 'n' : /* Specify number of copies... */
273 i ++;
274 if (i < argc)
275 num_options = cupsAddOption("copies", argv[i], num_options,
276 &options);
277 else
cc0d019f 278 usage(command, opt);
bc44d920 279 break;
280
cc0d019f 281 case 'o' : /* Specify option(s) or output filename */
bc44d920 282 i ++;
283 if (i < argc)
cc0d019f
MS
284 {
285 if (!strcmp(command, "convert"))
286 {
287 if (outfile)
288 usage(command, NULL);
289 else
290 outfile = argv[i];
291 }
292 else
293 num_options = cupsParseOptions(argv[i], num_options,
294 &options);
295 }
bc44d920 296 else
cc0d019f 297 usage(command, opt);
bc44d920 298 break;
299
300 case 'p' : /* Specify PPD file... */
cc0d019f 301 case 'P' : /* Specify PPD file... */
bc44d920 302 i ++;
303 if (i < argc)
304 ppdfile = argv[i];
305 else
cc0d019f 306 usage(command, opt);
bc44d920 307 break;
308
cc0d019f
MS
309 case 't' : /* Specify title... */
310 case 'J' : /* Specify title... */
bc44d920 311 i ++;
312 if (i < argc)
313 title = argv[i];
314 else
cc0d019f
MS
315 usage(command, opt);
316 break;
317
318 case 'u' : /* Delete PPD file after conversion */
319 removeinfile = 1;
320 break;
321
322 case 'U' : /* Specify username... */
323 i ++;
324 if (i < argc)
325 user = argv[i];
326 else
327 usage(command, opt);
bc44d920 328 break;
329
330 default : /* Something we don't understand... */
cc0d019f 331 usage(command, opt);
bc44d920 332 break;
333 }
334 }
cc0d019f
MS
335 else if (!infile)
336 {
337 if (strcmp(command, "convert"))
338 infile = argv[i];
339 else
340 {
341 _cupsLangPuts(stderr,
342 _("convert: Use the -f option to specify a file to "
343 "convert.\n"));
344 usage(command, NULL);
345 }
346 }
bc44d920 347 else
348 {
349 _cupsLangPuts(stderr,
350 _("cupsfilter: Only one filename can be specified!\n"));
cc0d019f 351 usage(command, NULL);
bc44d920 352 }
353
cc0d019f
MS
354 if (!infile && !srctype)
355 usage(command, NULL);
bc44d920 356
357 if (!title)
358 {
cc0d019f
MS
359 if (!infile)
360 title = "(stdin)";
361 else if ((title = strrchr(infile, '/')) != NULL)
bc44d920 362 title ++;
363 else
cc0d019f 364 title = infile;
bc44d920 365 }
366
367 /*
368 * Load the cupsd.conf file and create the MIME database...
369 */
370
371 if (read_cupsd_conf(cupsdconf))
372 return (1);
373
75bd9771
MS
374 snprintf(mimedir, sizeof(mimedir), "%s/mime", DataDir);
375
376 mime = mimeLoadTypes(NULL, mimedir);
377 mime = mimeLoadTypes(mime, ServerRoot);
378 mime = mimeLoadFilters(mime, mimedir, Path);
379 mime = mimeLoadFilters(mime, ServerRoot, Path);
380
381 if (!mime)
bc44d920 382 {
383 _cupsLangPrintf(stderr,
75bd9771
MS
384 _("%s: Unable to read MIME database from \"%s\" or "
385 "\"%s\"!\n"),
386 command, mimedir, ServerRoot);
bc44d920 387 return (1);
388 }
389
1340db2d
MS
390 if (all_filters)
391 {
392 printer_type = add_printer_filters(command, mime, printer, ppdfile,
393 &prefilter_type);
394 }
395 else
396 {
397 printer_type = mimeType(mime, "application", "vnd.cups-postscript");
398 prefilter_type = NULL;
399 }
b19ccc9e 400
bc44d920 401 /*
402 * Get the source and destination types...
403 */
404
cc0d019f
MS
405 if (srctype)
406 {
407 sscanf(srctype, "%15[^/]/%255s", super, type);
408 if ((src = mimeType(mime, super, type)) == NULL)
409 {
410 _cupsLangPrintf(stderr,
411 _("%s: Unknown source MIME type %s/%s!\n"),
412 command, super, type);
413 return (1);
414 }
415 }
416 else if ((src = mimeFileType(mime, infile, infile, &compression)) == NULL)
bc44d920 417 {
418 _cupsLangPrintf(stderr,
cc0d019f
MS
419 _("%s: Unable to determine MIME type of \"%s\"!\n"),
420 command, infile);
bc44d920 421 return (1);
422 }
423
cc0d019f 424 sscanf(dsttype, "%15[^/]/%255s", super, type);
b19ccc9e
MS
425 if (!strcasecmp(super, "printer"))
426 dst = printer_type;
427 else if ((dst = mimeType(mime, super, type)) == NULL)
bc44d920 428 {
429 _cupsLangPrintf(stderr,
cc0d019f
MS
430 _("%s: Unknown destination MIME type %s/%s!\n"),
431 command, super, type);
bc44d920 432 return (1);
433 }
434
435 /*
436 * Figure out how to filter the file...
437 */
438
439 if (src == dst)
440 {
441 /*
442 * Special case - no filtering needed...
443 */
444
445 filters = cupsArrayNew(NULL, NULL);
446 cupsArrayAdd(filters, &GZIPFilter);
1340db2d
MS
447 GZIPFilter.src = src;
448 GZIPFilter.dst = dst;
bc44d920 449 }
450 else if ((filters = mimeFilter(mime, src, dst, &cost)) == NULL)
451 {
452 _cupsLangPrintf(stderr,
cc0d019f
MS
453 _("%s: No filter to convert from %s/%s to %s/%s!\n"),
454 command, src->super, src->type, dst->super, dst->type);
bc44d920 455 return (1);
456 }
457 else if (compression)
458 cupsArrayInsert(filters, &GZIPFilter);
459
b19ccc9e
MS
460 if (prefilter_type)
461 {
462 /*
463 * Add pre-filters...
464 */
465
466 mime_filter_t *filter, /* Current filter */
467 *prefilter; /* Current pre-filter */
468 cups_array_t *prefilters = cupsArrayNew(NULL, NULL);
469 /* New filters array */
470
471
472 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
473 filter;
474 filter = (mime_filter_t *)cupsArrayNext(filters))
475 {
1340db2d
MS
476 if ((prefilter = mimeFilterLookup(mime, filter->src,
477 prefilter_type)) != NULL)
b19ccc9e
MS
478 cupsArrayAdd(prefilters, prefilter);
479
480 cupsArrayAdd(prefilters, filter);
481 }
482
483 cupsArrayDelete(filters);
484 filters = prefilters;
485 }
486
bc44d920 487 /*
488 * Do it!
489 */
490
1340db2d 491 status = exec_filters(src, filters, infile, outfile, ppdfile, printer, user,
b19ccc9e 492 title, num_options, options);
cc0d019f
MS
493
494 /*
495 * Remove files as needed, then exit...
496 */
497
2e4ff8af
MS
498 if (TempFile[0])
499 unlink(TempFile);
500
cc0d019f
MS
501 if (removeppd && ppdfile)
502 unlink(ppdfile);
503
504 if (removeinfile && infile)
505 unlink(infile);
506
507 return (status);
bc44d920 508}
509
510
b19ccc9e
MS
511/*
512 * 'add_printer_filter()' - Add a single filters from a PPD file.
513 */
514
515static void
516add_printer_filter(
517 const char *command, /* I - Command name */
518 mime_t *mime, /* I - MIME database */
519 mime_type_t *filtertype, /* I - Printer or prefilter MIME type */
520 const char *filter) /* I - Filter to add */
521{
522 char super[MIME_MAX_SUPER], /* Super-type for filter */
523 type[MIME_MAX_TYPE], /* Type for filter */
524 program[1024]; /* Program/filter name */
525 int cost; /* Cost of filter */
526 mime_type_t *temptype; /* MIME type looping var */
527 char filename[1024]; /* Full filter filename */
528
529
530 /*
531 * Parse the filter string; it should be in the following format:
532 *
533 * super/type cost program
534 */
535
536 if (sscanf(filter, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super, type, &cost,
537 program) != 4)
538 {
539 _cupsLangPrintf(stderr, _("%s: Invalid filter string \"%s\"\n"), command,
540 filter);
541 return;
542 }
543
544 /*
545 * See if the filter program exists; if not, stop the printer and flag
546 * the error!
547 */
548
549 if (strcmp(program, "-"))
550 {
551 if (program[0] == '/')
552 strlcpy(filename, program, sizeof(filename));
553 else
554 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
555
556 if (access(filename, X_OK))
557 {
558 _cupsLangPrintf(stderr, _("%s: Filter \"%s\" not available: %s\n"),
559 command, program, strerror(errno));
560 return;
561 }
562 }
563
564 /*
565 * Add the filter to the MIME database, supporting wildcards as needed...
566 */
567
568 for (temptype = mimeFirstType(mime);
569 temptype;
570 temptype = mimeNextType(mime))
571 if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
572 !strcasecmp(temptype->super, super)) &&
573 (type[0] == '*' || !strcasecmp(temptype->type, type)))
574 mimeAddFilter(mime, temptype, filtertype, cost, program);
575}
576
577
578/*
579 * 'add_printer_filters()' - Add filters from a PPD file.
580 */
581
582static mime_type_t * /* O - Printer type or NULL on error */
583add_printer_filters(
584 const char *command, /* I - Command name */
585 mime_t *mime, /* I - MIME database */
586 const char *printer, /* I - Printer name */
587 const char *ppdfile, /* I - PPD file */
588 mime_type_t **prefilter_type) /* O - Prefilter type */
589{
590 int i; /* Looping var */
591 mime_type_t *printer_type; /* Printer MIME type */
592 ppd_file_t *ppd; /* PPD file data */
593 ppd_attr_t *ppdattr; /* Current prefilter */
594
595
bf3816c7
MS
596 *prefilter_type = NULL;
597
b19ccc9e
MS
598 if ((ppd = ppdOpenFile(ppdfile)) == NULL)
599 {
600 ppd_status_t status; /* PPD load status */
601
602 status = ppdLastError(&i);
603 _cupsLangPrintf(stderr, _("%s: Unable to open PPD file: %s on line %d\n"),
604 command, ppdErrorString(status), i);
605 return (NULL);
606 }
607
608 printer_type = mimeAddType(mime, "printer", printer);
609
610 if (ppd->num_filters > 0)
611 {
612 for (i = 0; i < ppd->num_filters; i ++)
613 add_printer_filter(command, mime, printer_type, ppd->filters[i]);
614 }
615 else
616 {
617 add_printer_filter(command, mime, printer_type,
618 "application/vnd.cups-command 0 commandtops");
619 add_printer_filter(command, mime, printer_type,
620 "application/vnd.cups-postscript 0 -");
621 }
622
623 if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
624 {
625 *prefilter_type = mimeAddType(mime, "prefilter", printer);
626
627 for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
628 if (ppdattr->value)
629 add_printer_filter(command, mime, *prefilter_type, ppdattr->value);
630 }
1340db2d
MS
631 else
632 *prefilter_type = NULL;
b19ccc9e
MS
633
634 return (printer_type);
635}
636
637
bc44d920 638/*
639 * 'compare_pids()' - Compare two filter PIDs...
640 */
641
642static int /* O - Result of comparison */
643compare_pids(mime_filter_t *a, /* I - First filter */
644 mime_filter_t *b) /* I - Second filter */
645{
646 /*
647 * Because we're particularly lazy, we store the process ID in the "cost"
648 * variable...
649 */
650
651 return (a->cost - b->cost);
652}
653
654
655/*
656 * 'escape_options()' - Convert an options array to a string.
657 */
658
659static char * /* O - Option string */
660escape_options(
661 int num_options, /* I - Number of options */
662 cups_option_t *options) /* I - Options */
663{
664 int i; /* Looping var */
665 cups_option_t *option; /* Current option */
666 int bytes; /* Number of bytes needed */
667 char *s, /* Option string */
668 *sptr, /* Pointer into string */
669 *vptr; /* Pointer into value */
670
671
672 /*
673 * Figure out the worst-case number of bytes we need for the option string.
674 */
675
676 for (i = num_options, option = options, bytes = 1; i > 0; i --, option ++)
677 bytes += 2 * (strlen(option->name) + strlen(option->value)) + 2;
678
91c84a35
MS
679 if ((s = malloc(bytes)) == NULL)
680 return (NULL);
bc44d920 681
682 /*
683 * Copy the options to the string...
684 */
685
686 for (i = num_options, option = options, sptr = s; i > 0; i --, option ++)
687 {
688 if (!strcmp(option->name, "copies"))
689 continue;
690
691 if (sptr > s)
692 *sptr++ = ' ';
693
694 strcpy(sptr, option->name);
695 sptr += strlen(sptr);
696 *sptr++ = '=';
697
698 for (vptr = option->value; *vptr;)
699 {
700 if (strchr("\\ \t\n", *vptr))
701 *sptr++ = '\\';
702
703 *sptr++ = *vptr++;
704 }
705 }
706
707 *sptr = '\0';
708
bc44d920 709 return (s);
710}
711
712
713/*
714 * 'exec_filter()' - Execute a single filter.
715 */
716
717static int /* O - Process ID or -1 on error */
718exec_filter(const char *filter, /* I - Filter to execute */
719 char **argv, /* I - Argument list */
720 char **envp, /* I - Environment list */
721 int infd, /* I - Stdin file descriptor */
722 int outfd) /* I - Stdout file descriptor */
723{
97c9a8d7
MS
724 int pid, /* Process ID */
725 fd; /* Temporary file descriptor */
bc44d920 726#if defined(__APPLE__)
727 char processPath[1024], /* CFProcessPath environment variable */
728 linkpath[1024]; /* Link path for symlinks... */
729 int linkbytes; /* Bytes for link path */
730
731
732 /*
733 * Add special voodoo magic for MacOS X - this allows MacOS X
734 * programs to access their bundle resources properly...
735 */
736
737 if ((linkbytes = readlink(filter, linkpath, sizeof(linkpath) - 1)) > 0)
738 {
739 /*
740 * Yes, this is a symlink to the actual program, nul-terminate and
741 * use it...
742 */
743
744 linkpath[linkbytes] = '\0';
745
746 if (linkpath[0] == '/')
747 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
748 linkpath);
749 else
750 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
751 dirname((char *)filter), linkpath);
752 }
753 else
754 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", filter);
755
756 envp[0] = processPath; /* Replace <CFProcessPath> string */
757#endif /* __APPLE__ */
758
759 if ((pid = fork()) == 0)
760 {
761 /*
762 * Child process goes here...
763 *
764 * Update stdin/stdout/stderr as needed...
765 */
766
767 if (infd != 0)
768 {
97c9a8d7
MS
769 if (infd < 0)
770 infd = open("/dev/null", O_RDONLY);
771
bc44d920 772 if (infd > 0)
97c9a8d7
MS
773 {
774 dup2(infd, 0);
775 close(infd);
776 }
bc44d920 777 }
778
779 if (outfd != 1)
780 {
97c9a8d7
MS
781 if (outfd < 0)
782 outfd = open("/dev/null", O_WRONLY);
783
784 if (outfd > 1)
785 {
786 dup2(outfd, 1);
787 close(outfd);
788 }
bc44d920 789 }
790
97c9a8d7
MS
791 if ((fd = open("/dev/null", O_RDWR)) > 3)
792 {
793 dup2(fd, 3);
794 close(fd);
795 }
bc44d920 796 fcntl(3, F_SETFL, O_NDELAY);
797
97c9a8d7
MS
798 if ((fd = open("/dev/null", O_RDWR)) > 4)
799 {
800 dup2(fd, 4);
801 close(fd);
802 }
bc44d920 803 fcntl(4, F_SETFL, O_NDELAY);
804
805 /*
806 * Execute command...
807 */
808
809 execve(filter, argv, envp);
810
811 perror(filter);
812
813 exit(errno);
814 }
815
816 return (pid);
817}
818
819
820/*
821 * 'exec_filters()' - Execute filters for the given file and options.
822 */
823
824static int /* O - 0 on success, 1 on error */
1340db2d
MS
825exec_filters(mime_type_t *srctype, /* I - Source type */
826 cups_array_t *filters, /* I - Array of filters to run */
cc0d019f
MS
827 const char *infile, /* I - File to filter */
828 const char *outfile, /* I - File to create */
bc44d920 829 const char *ppdfile, /* I - PPD file, if any */
db1f069b 830 const char *printer, /* I - Printer name */
cc0d019f 831 const char *user, /* I - Username */
bc44d920 832 const char *title, /* I - Job title */
833 int num_options, /* I - Number of filter options */
834 cups_option_t *options) /* I - Filter options */
835{
cc0d019f 836 int i; /* Looping var */
bc44d920 837 const char *argv[8], /* Command-line arguments */
e6013cfa 838 *envp[12], /* Environment variables */
bc44d920 839 *temp; /* Temporary string */
840 char *optstr, /* Filter options */
e6013cfa 841 content_type[1024], /* CONTENT_TYPE */
bc44d920 842 cups_datadir[1024], /* CUPS_DATADIR */
843 cups_fontpath[1024], /* CUPS_FONTPATH */
844 cups_serverbin[1024], /* CUPS_SERVERBIN */
845 cups_serverroot[1024], /* CUPS_SERVERROOT */
846 lang[1024], /* LANG */
847 path[1024], /* PATH */
848 ppd[1024], /* PPD */
849 rip_cache[1024], /* RIP_CACHE */
cc0d019f 850 userenv[1024], /* USER */
bc44d920 851 program[1024]; /* Program to run */
852 mime_filter_t *filter, /* Current filter */
853 *next; /* Next filter */
854 int current, /* Current filter */
855 filterfds[2][2], /* Pipes for filters */
856 pid, /* Process ID of filter */
857 status, /* Exit status */
858 retval; /* Return value */
859 cups_array_t *pids; /* Executed filters array */
860 mime_filter_t key; /* Search key for filters */
cc0d019f 861 cups_lang_t *language; /* Current language */
bc44d920 862
863
864 /*
865 * Setup the filter environment and command-line...
866 */
867
868 optstr = escape_options(num_options, options);
869
e6013cfa 870 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
1340db2d 871 srctype->super, srctype->type);
bc44d920 872 snprintf(cups_datadir, sizeof(cups_datadir), "CUPS_DATADIR=%s", DataDir);
873 snprintf(cups_fontpath, sizeof(cups_fontpath), "CUPS_FONTPATH=%s", FontPath);
874 snprintf(cups_serverbin, sizeof(cups_serverbin), "CUPS_SERVERBIN=%s",
875 ServerBin);
876 snprintf(cups_serverroot, sizeof(cups_serverroot), "CUPS_SERVERROOT=%s",
877 ServerRoot);
cc0d019f
MS
878 language = cupsLangDefault();
879 snprintf(lang, sizeof(lang), "LANG=%s.UTF8", language->language);
bc44d920 880 snprintf(path, sizeof(path), "PATH=%s", Path);
881 if (ppdfile)
882 snprintf(ppd, sizeof(ppd), "PPD=%s", ppdfile);
883 else if ((temp = getenv("PPD")) != NULL)
884 snprintf(ppd, sizeof(ppd), "PPD=%s", temp);
885 else
cc0d019f 886#ifdef __APPLE__
db1f069b
MS
887 if (!access("/System/Library/Frameworks/ApplicationServices.framework/"
888 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
889 "Resources/English.lproj/Generic.ppd", 0))
cc0d019f
MS
890 strlcpy(ppd, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
891 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
892 "Resources/English.lproj/Generic.ppd", sizeof(ppd));
db1f069b
MS
893 else
894 strlcpy(ppd, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
895 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
896 "Resources/Generic.ppd", sizeof(ppd));
cc0d019f 897#else
bc44d920 898 snprintf(ppd, sizeof(ppd), "PPD=%s/model/laserjet.ppd", DataDir);
cc0d019f 899#endif /* __APPLE__ */
bc44d920 900 snprintf(rip_cache, sizeof(rip_cache), "RIP_CACHE=%s", RIPCache);
cc0d019f 901 snprintf(userenv, sizeof(userenv), "USER=%s", user);
bc44d920 902
db1f069b
MS
903 argv[0] = (char *)printer;
904 argv[1] = "1";
cc0d019f 905 argv[2] = user;
bc44d920 906 argv[3] = title;
907 argv[4] = cupsGetOption("copies", num_options, options);
908 argv[5] = optstr;
cc0d019f 909 argv[6] = infile;
bc44d920 910 argv[7] = NULL;
911
912 if (!argv[4])
913 argv[4] = "1";
914
915 envp[0] = "<CFProcessPath>";
e6013cfa
MS
916 envp[1] = content_type;
917 envp[2] = cups_datadir;
918 envp[3] = cups_fontpath;
919 envp[4] = cups_serverbin;
920 envp[5] = cups_serverroot;
921 envp[6] = lang;
922 envp[7] = path;
923 envp[8] = ppd;
924 envp[9] = rip_cache;
925 envp[10] = userenv;
926 envp[11] = NULL;
bc44d920 927
cc0d019f
MS
928 for (i = 0; argv[i]; i ++)
929 fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
930
931 for (i = 0; envp[i]; i ++)
932 fprintf(stderr, "DEBUG: envp[%d]=\"%s\"\n", i, envp[i]);
933
bc44d920 934 /*
935 * Execute all of the filters...
936 */
937
938 pids = cupsArrayNew((cups_array_func_t)compare_pids, NULL);
939 current = 0;
940 filterfds[0][0] = -1;
941 filterfds[0][1] = -1;
942 filterfds[1][0] = -1;
943 filterfds[1][1] = -1;
944
cc0d019f
MS
945 if (!infile)
946 filterfds[0][0] = 0;
947
bc44d920 948 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
949 filter;
950 filter = next, current = 1 - current)
951 {
952 next = (mime_filter_t *)cupsArrayNext(filters);
953
954 if (filter->filter[0] == '/')
955 strlcpy(program, filter->filter, sizeof(program));
956 else
957 snprintf(program, sizeof(program), "%s/filter/%s", ServerBin,
958 filter->filter);
959
960 if (filterfds[!current][1] > 1)
961 {
962 close(filterfds[1 - current][0]);
963 close(filterfds[1 - current][1]);
964
965 filterfds[1 - current][0] = -1;
966 filterfds[1 - current][0] = -1;
967 }
968
969 if (next)
970 open_pipe(filterfds[1 - current]);
cc0d019f
MS
971 else if (outfile)
972 {
973 filterfds[1 - current][1] = open(outfile, O_CREAT | O_TRUNC | O_WRONLY,
974 0666);
975
976 if (filterfds[1 - current][1] < 0)
977 fprintf(stderr, "ERROR: Unable to create \"%s\" - %s\n", outfile,
978 strerror(errno));
979 }
bc44d920 980 else
981 filterfds[1 - current][1] = 1;
982
983 pid = exec_filter(program, (char **)argv, (char **)envp,
984 filterfds[current][0], filterfds[1 - current][1]);
985
986 if (pid > 0)
987 {
988 fprintf(stderr, "INFO: %s (PID %d) started.\n", filter->filter, pid);
989
990 filter->cost = pid;
991 cupsArrayAdd(pids, filter);
992 }
993 else
994 break;
995
996 argv[6] = NULL;
997 }
998
999 /*
1000 * Close remaining pipes...
1001 */
1002
1003 if (filterfds[0][1] > 1)
1004 {
1005 close(filterfds[0][0]);
1006 close(filterfds[0][1]);
1007 }
1008
1009 if (filterfds[1][1] > 1)
1010 {
1011 close(filterfds[1][0]);
1012 close(filterfds[1][1]);
1013 }
1014
1015 /*
1016 * Wait for the children to exit...
1017 */
1018
1019 retval = 0;
1020
1021 while (cupsArrayCount(pids) > 0)
1022 {
1023 if ((pid = wait(&status)) < 0)
1024 continue;
1025
1026 key.cost = pid;
1027 if ((filter = (mime_filter_t *)cupsArrayFind(pids, &key)) != NULL)
1028 {
1029 cupsArrayRemove(pids, filter);
1030
1031 if (status)
1032 {
1033 if (WIFEXITED(status))
1034 fprintf(stderr, "ERROR: %s (PID %d) stopped with status %d!\n",
1035 filter->filter, pid, WEXITSTATUS(status));
1036 else
1037 fprintf(stderr, "ERROR: %s (PID %d) crashed on signal %d!\n",
1038 filter->filter, pid, WTERMSIG(status));
1039
1040 retval = 1;
1041 }
1042 else
1043 fprintf(stderr, "INFO: %s (PID %d) exited with no errors.\n",
1044 filter->filter, pid);
1045 }
1046 }
1047
91c84a35
MS
1048 cupsArrayDelete(pids);
1049
bc44d920 1050 return (retval);
1051}
1052
1053
2e4ff8af
MS
1054/*
1055 * 'get_job_file()' - Get the specified job file.
1056 */
1057
1058static void
1059get_job_file(const char *job) /* I - Job ID */
1060{
1061 long jobid, /* Job ID */
1062 docnum; /* Document number */
1063 const char *jobptr; /* Pointer into job ID string */
1064 char uri[1024]; /* job-uri */
1065 http_t *http; /* Connection to server */
1066 ipp_t *request; /* Request data */
1067 int tempfd; /* Temporary file */
1068
1069
1070 /*
1071 * Get the job ID and document number, if any...
1072 */
1073
1074 if ((jobptr = strrchr(job, '-')) != NULL)
1075 jobptr ++;
1076 else
1077 jobptr = job;
1078
1079 jobid = strtol(jobptr, (char **)&jobptr, 10);
1080
1081 if (*jobptr == ',')
1082 docnum = strtol(jobptr + 1, NULL, 10);
1083 else
1084 docnum = 1;
1085
1086 if (jobid < 1 || jobid > INT_MAX)
1087 {
1088 _cupsLangPrintf(stderr, _("cupsfilter: Invalid job ID %d!\n"), (int)jobid);
1089 exit(1);
1090 }
1091
1092 if (docnum < 1 || docnum > INT_MAX)
1093 {
1094 _cupsLangPrintf(stderr, _("cupsfilter: Invalid document number %d!\n"),
1095 (int)docnum);
1096 exit(1);
1097 }
1098
1099 /*
1100 * Ask the server for the document file...
1101 */
1102
1103 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
1104 cupsEncryption())) == NULL)
1105 {
1106 _cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"),
1107 "cupsfilter");
1108 exit(1);
1109 }
1110
1111 request = ippNewRequest(CUPS_GET_DOCUMENT);
1112
1113 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", (int)jobid);
1114
1115 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
1116 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "document-number",
1117 (int)docnum);
1118
1119 if ((tempfd = cupsTempFd(TempFile, sizeof(TempFile))) == -1)
1120 {
f0ab5bff 1121 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
2e4ff8af
MS
1122 httpClose(http);
1123 exit(1);
1124 }
1125
1126 signal(SIGTERM, sighandler);
1127
1128 ippDelete(cupsDoIORequest(http, request, "/", -1, tempfd));
1129
1130 close(tempfd);
1131
1132 httpClose(http);
1133
1134 if (cupsLastError() != IPP_OK)
1135 {
1136 _cupsLangPrintf(stderr, _("cupsfilter: Unable to get job file - %s\n"),
1137 cupsLastErrorString());
1138 unlink(TempFile);
1139 exit(1);
1140 }
1141}
1142
1143
bc44d920 1144/*
1145 * 'open_pipe()' - Create a pipe which is closed on exec.
1146 */
1147
2e4ff8af 1148static int /* O - 0 on success, -1 on error */
bc44d920 1149open_pipe(int *fds) /* O - Pipe file descriptors (2) */
1150{
1151 /*
1152 * Create the pipe...
1153 */
1154
1155 if (pipe(fds))
1156 {
1157 fds[0] = -1;
1158 fds[1] = -1;
1159
1160 return (-1);
1161 }
1162
1163 /*
1164 * Set the "close on exec" flag on each end of the pipe...
1165 */
1166
1167 if (fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC))
1168 {
1169 close(fds[0]);
1170 close(fds[1]);
1171
1172 fds[0] = -1;
1173 fds[1] = -1;
1174
1175 return (-1);
1176 }
1177
1178 if (fcntl(fds[1], F_SETFD, fcntl(fds[1], F_GETFD) | FD_CLOEXEC))
1179 {
1180 close(fds[0]);
1181 close(fds[1]);
1182
1183 fds[0] = -1;
1184 fds[1] = -1;
1185
1186 return (-1);
1187 }
1188
1189 /*
1190 * Return 0 indicating success...
1191 */
1192
1193 return (0);
1194}
1195
1196
1197/*
1198 * 'read_cupsd_conf()' - Read the cupsd.conf file to get the filter settings.
1199 */
1200
1201static int /* O - 0 on success, 1 on error */
1202read_cupsd_conf(const char *filename) /* I - File to read */
1203{
cc0d019f 1204 cups_file_t *fp; /* cupsd.conf file */
bc44d920 1205 const char *temp; /* Temporary string */
1206 char line[1024], /* Line from file */
1207 *ptr; /* Pointer into line */
cc0d019f 1208 int linenum; /* Current line number */
bc44d920 1209
1210
1211 if ((temp = getenv("CUPS_DATADIR")) != NULL)
1212 set_string(&DataDir, temp);
1213 else
1214 set_string(&DataDir, CUPS_DATADIR);
1215
1216 if ((temp = getenv("CUPS_FONTPATH")) != NULL)
1217 set_string(&FontPath, temp);
1218 else
1219 set_string(&FontPath, CUPS_FONTPATH);
1220
cc0d019f
MS
1221 set_string(&RIPCache, "8m");
1222
bc44d920 1223 if ((temp = getenv("CUPS_SERVERBIN")) != NULL)
1224 set_string(&ServerBin, temp);
1225 else
1226 set_string(&ServerBin, CUPS_SERVERBIN);
1227
1228 strlcpy(line, filename, sizeof(line));
1229 if ((ptr = strrchr(line, '/')) != NULL)
1230 *ptr = '\0';
1231 else
1232 getcwd(line, sizeof(line));
1233
1234 set_string(&ServerRoot, line);
1235
cc0d019f
MS
1236 if ((fp = cupsFileOpen(filename, "r")) != NULL)
1237 {
1238 linenum = 0;
1239
1240 while (cupsFileGetConf(fp, line, sizeof(line), &ptr, &linenum))
1241 {
1242 if (!strcasecmp(line, "DataDir"))
1243 set_string(&DataDir, ptr);
1244 else if (!strcasecmp(line, "FontPath"))
1245 set_string(&FontPath, ptr);
1246 else if (!strcasecmp(line, "RIPCache"))
1247 set_string(&RIPCache, ptr);
1248 else if (!strcasecmp(line, "ServerBin"))
1249 set_string(&ServerBin, ptr);
1250 else if (!strcasecmp(line, "ServerRoot"))
1251 set_string(&ServerRoot, ptr);
1252 }
1253
1254 cupsFileClose(fp);
1255 }
1256
bc44d920 1257 snprintf(line, sizeof(line),
06d4e77b 1258 "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR ":/bin:/usr/bin",
bc44d920 1259 ServerBin);
1260 set_string(&Path, line);
1261
1262 return (0);
1263}
1264
1265
1266/*
1267 * 'set_string()' - Copy and set a string.
1268 */
1269
1270static void
1271set_string(char **s, /* O - Copy of string */
1272 const char *val) /* I - String to copy */
1273{
1274 if (*s)
1275 free(*s);
1276
1277 *s = strdup(val);
1278}
1279
1280
2e4ff8af
MS
1281/*
1282 * 'sighandler()' - Signal catcher for when we print from stdin...
1283 */
1284
1285static void
1286sighandler(int s) /* I - Signal number */
1287{
1288 /*
1289 * Remove the temporary file we're using to print a job file...
1290 */
1291
1292 if (TempFile[0])
1293 unlink(TempFile);
1294
1295 /*
1296 * Exit...
1297 */
1298
1299 exit(s);
1300}
1301
1302
bc44d920 1303/*
1304 * 'usage()' - Show program usage...
1305 */
1306
1307static void
cc0d019f
MS
1308usage(const char *command, /* I - Command name */
1309 const char *opt) /* I - Incorrect option, if any */
bc44d920 1310{
1311 if (opt)
cc0d019f
MS
1312 _cupsLangPrintf(stderr, _("%s: Unknown option '%c'!\n"), command, *opt);
1313
1314 if (!strcmp(command, "cupsfilter"))
1315 _cupsLangPuts(stdout,
1316 _("Usage: cupsfilter -m mime/type [ options ] filename\n"
1317 "\n"
1318 "Options:\n"
1319 "\n"
1320 " -c cupsd.conf Set cupsd.conf file to use\n"
1340db2d 1321 " -e Use every filter from the PPD file\n"
2e4ff8af 1322 " -j job-id[,N] Filter file N from the specified job (default is file 1)\n"
cc0d019f
MS
1323 " -n copies Set number of copies\n"
1324 " -o name=value Set option(s)\n"
1325 " -p filename.ppd Set PPD file\n"
1326 " -t title Set title\n"));
1327 else
1328 _cupsLangPuts(stdout,
1329 _("Usage: convert [ options ]\n"
1330 "\n"
1331 "Options:\n"
1332 "\n"
1340db2d 1333 " -e Use every filter from the PPD file\n"
cc0d019f
MS
1334 " -f filename Set file to be converted (otherwise stdin)\n"
1335 " -o filename Set file to be generated (otherwise stdout)\n"
1336 " -i mime/type Set input MIME type (otherwise auto-typed)\n"
1337 " -j mime/type Set output MIME type (otherwise application/pdf)\n"
1338 " -P filename.ppd Set PPD file\n"
1339 " -a 'name=value ...' Set option(s)\n"
1340 " -U username Set username for job\n"
1341 " -J title Set title\n"
1342 " -c copies Set number of copies\n"
1343 " -u Remove the PPD file when finished\n"
1344 " -D Remove the input file when finished\n"));
bc44d920 1345
1346 exit(1);
1347}
1348
1349
1350/*
b19ccc9e 1351 * End of "$Id: cupsfilter.c 7952 2008-09-17 00:56:20Z mike $".
bc44d920 1352 */