]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cupsfilter.c
4 * Filtering program for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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/".
17 * Include necessary headers...
20 #include <cups/cups-private.h>
21 #include <cups/file-private.h>
22 #include <cups/ppd-private.h>
29 #if defined(__APPLE__)
31 #endif /* __APPLE__ */
38 static char *DataDir
= NULL
;/* CUPS_DATADIR environment variable */
39 static char *FontPath
= NULL
;
40 /* CUPS_FONTPATH environment variable */
41 static mime_filter_t GZIPFilter
= /* gziptoany filter */
43 NULL
, /* Source type */
44 NULL
, /* Destination type */
46 "gziptoany" /* Filter program to run */
48 static char *Path
= NULL
; /* PATH environment variable */
49 static char *ServerBin
= NULL
;
50 /* CUPS_SERVERBIN environment variable */
51 static char *ServerRoot
= NULL
;
52 /* CUPS_SERVERROOT environment variable */
53 static char *RIPCache
= NULL
;
54 /* RIP_MAX_CACHE environment variable */
55 static char TempFile
[1024] = "";
63 static void add_printer_filter(const char *command
, mime_t
*mime
,
64 mime_type_t
*printer_type
,
66 static mime_type_t
*add_printer_filters(const char *command
,
67 mime_t
*mime
, const char *printer
,
69 mime_type_t
**prefilter_type
);
70 static void check_cb(void *context
, _cups_fc_result_t result
,
72 static int compare_pids(mime_filter_t
*a
, mime_filter_t
*b
);
73 static char *escape_options(int num_options
, cups_option_t
*options
);
74 static int exec_filter(const char *filter
, char **argv
,
75 char **envp
, int infd
, int outfd
);
76 static int exec_filters(mime_type_t
*srctype
,
77 cups_array_t
*filters
, const char *infile
,
78 const char *outfile
, const char *ppdfile
,
79 const char *printer
, const char *user
,
80 const char *title
, int num_options
,
81 cups_option_t
*options
);
82 static void get_job_file(const char *job
);
83 static int open_pipe(int *fds
);
84 static int read_cups_files_conf(const char *filename
);
85 static void set_string(char **s
, const char *val
);
86 static void sighandler(int sig
);
87 static void usage(const char *opt
) __attribute__((noreturn
));
91 * 'main()' - Main entry for the test program.
94 int /* O - Exit status */
95 main(int argc
, /* I - Number of command-line args */
96 char *argv
[]) /* I - Command-line arguments */
98 int i
, /* Looping vars */
99 list_filters
= 0; /* Just list the filters? */
100 const char *command
, /* Command name */
101 *opt
, /* Current option */
102 *printer
; /* Printer name */
103 mime_type_t
*printer_type
, /* Printer MIME type */
104 *prefilter_type
; /* Printer prefilter MIME type */
105 char *srctype
, /* Source type */
106 *dsttype
, /* Destination type */
107 super
[MIME_MAX_SUPER
], /* Super-type name */
108 type
[MIME_MAX_TYPE
]; /* Type name */
109 int compression
; /* Compression of file */
110 int cost
; /* Cost of filters */
111 mime_t
*mime
; /* MIME database */
112 char mimedir
[1024]; /* MIME directory */
113 char *infile
, /* File to filter */
114 *outfile
; /* File to create */
115 char cupsfilesconf
[1024]; /* cups-files.conf file */
116 const char *server_root
; /* CUPS_SERVERROOT environment variable */
117 mime_type_t
*src
, /* Source type */
118 *dst
; /* Destination type */
119 cups_array_t
*filters
; /* Filters for the file */
120 int num_options
; /* Number of options */
121 cups_option_t
*options
; /* Options */
122 const char *ppdfile
; /* PPD file */
123 const char *title
, /* Title string */
124 *user
; /* Username */
125 int all_filters
, /* Use all filters */
126 removeppd
, /* Remove PPD file */
127 removeinfile
; /* Remove input file */
128 int status
; /* Execution status */
135 if ((command
= strrchr(argv
[0], '/')) != NULL
)
140 printer
= !strcmp(command
, "convert") ? "tofile" : "cupsfilter";
144 dsttype
= "application/pdf";
156 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
157 server_root
= CUPS_SERVERROOT
;
159 snprintf(cupsfilesconf
, sizeof(cupsfilesconf
), "%s/cups-files.conf", server_root
);
162 * Process command-line arguments...
165 _cupsSetLocale(argv
);
167 for (i
= 1; i
< argc
; i
++)
169 if (argv
[i
][0] == '-')
171 if (!strcmp(argv
[i
], "--list-filters"))
175 else if (!strcmp(argv
[i
], "--"))
178 if (i
< argc
&& !infile
)
185 for (opt
= argv
[i
] + 1; *opt
; opt
++)
189 case 'a' : /* Specify option... */
192 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
197 case 'c' : /* Specify cups-files.conf file location... */
201 if (!strcmp(command
, "convert"))
202 num_options
= cupsAddOption("copies", argv
[i
], num_options
, &options
);
204 strlcpy(cupsfilesconf
, argv
[i
], sizeof(cupsfilesconf
));
210 case 'd' : /* Specify the real printer name */
218 case 'D' : /* Delete input file after conversion */
222 case 'e' : /* Use every filter from the PPD file */
226 case 'f' : /* Specify input file... */
228 if (i
< argc
&& !infile
)
234 case 'i' : /* Specify source MIME type... */
238 if (sscanf(argv
[i
], "%15[^/]/%255s", super
, type
) != 2)
247 case 'j' : /* Get job file or specify destination MIME type... */
248 if (strcmp(command
, "convert"))
253 get_job_file(argv
[i
]);
262 case 'm' : /* Specify destination MIME type... */
266 if (sscanf(argv
[i
], "%15[^/]/%255s", super
, type
) != 2)
275 case 'n' : /* Specify number of copies... */
278 num_options
= cupsAddOption("copies", argv
[i
], num_options
, &options
);
283 case 'o' : /* Specify option(s) or output filename */
287 if (!strcmp(command
, "convert"))
295 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
301 case 'p' : /* Specify PPD file... */
302 case 'P' : /* Specify PPD file... */
310 case 't' : /* Specify title... */
311 case 'J' : /* Specify title... */
319 case 'u' : /* Delete PPD file after conversion */
323 case 'U' : /* Specify username... */
331 default : /* Something we don't understand... */
340 if (strcmp(command
, "convert"))
347 _cupsLangPuts(stderr
,
348 _("cupsfilter: Only one filename can be specified."));
353 if (!infile
&& !srctype
)
360 else if ((title
= strrchr(infile
, '/')) != NULL
)
367 * Load the cups-files.conf file and create the MIME database...
370 if (read_cups_files_conf(cupsfilesconf
))
373 snprintf(mimedir
, sizeof(mimedir
), "%s/mime", DataDir
);
375 mime
= mimeLoadTypes(NULL
, mimedir
);
376 mime
= mimeLoadTypes(mime
, ServerRoot
);
377 mime
= mimeLoadFilters(mime
, mimedir
, Path
);
378 mime
= mimeLoadFilters(mime
, ServerRoot
, Path
);
382 _cupsLangPrintf(stderr
,
383 _("%s: Unable to read MIME database from \"%s\" or "
385 command
, mimedir
, ServerRoot
);
389 prefilter_type
= NULL
;
392 printer_type
= add_printer_filters(command
, mime
, printer
, ppdfile
,
395 printer_type
= mimeType(mime
, "application", "vnd.cups-postscript");
398 * Get the source and destination types...
403 /* sscanf return value already checked above */
404 sscanf(srctype
, "%15[^/]/%255s", super
, type
);
405 if ((src
= mimeType(mime
, super
, type
)) == NULL
)
407 _cupsLangPrintf(stderr
,
408 _("%s: Unknown source MIME type %s/%s."),
409 command
, super
, type
);
413 else if ((src
= mimeFileType(mime
, infile
, infile
, &compression
)) == NULL
)
415 _cupsLangPrintf(stderr
,
416 _("%s: Unable to determine MIME type of \"%s\"."),
421 /* sscanf return value already checked above */
422 sscanf(dsttype
, "%15[^/]/%255s", super
, type
);
423 if (!_cups_strcasecmp(super
, "printer"))
425 else if ((dst
= mimeType(mime
, super
, type
)) == NULL
)
427 _cupsLangPrintf(stderr
,
428 _("%s: Unknown destination MIME type %s/%s."),
429 command
, super
, type
);
434 * Figure out how to filter the file...
440 * Special case - no filtering needed...
443 filters
= cupsArrayNew(NULL
, NULL
);
444 cupsArrayAdd(filters
, &GZIPFilter
);
445 GZIPFilter
.src
= src
;
446 GZIPFilter
.dst
= dst
;
448 else if ((filters
= mimeFilter(mime
, src
, dst
, &cost
)) == NULL
)
450 _cupsLangPrintf(stderr
,
451 _("%s: No filter to convert from %s/%s to %s/%s."),
452 command
, src
->super
, src
->type
, dst
->super
, dst
->type
);
455 else if (compression
)
456 cupsArrayInsert(filters
, &GZIPFilter
);
464 mime_filter_t
*filter
, /* Current filter */
465 *prefilter
; /* Current pre-filter */
466 cups_array_t
*prefilters
= cupsArrayNew(NULL
, NULL
);
467 /* New filters array */
470 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
472 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
474 if ((prefilter
= mimeFilterLookup(mime
, filter
->src
,
475 prefilter_type
)) != NULL
)
476 cupsArrayAdd(prefilters
, prefilter
);
478 cupsArrayAdd(prefilters
, filter
);
481 cupsArrayDelete(filters
);
482 filters
= prefilters
;
491 mime_filter_t
*filter
; /* Current filter */
493 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
495 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
496 if (strcmp(filter
->filter
, "-"))
497 _cupsLangPuts(stdout
, filter
->filter
);
507 status
= exec_filters(src
, filters
, infile
, outfile
, ppdfile
, printer
, user
,
508 title
, num_options
, options
);
512 * Remove files as needed, then exit...
518 if (removeppd
&& ppdfile
)
521 if (removeinfile
&& infile
)
529 * 'add_printer_filter()' - Add a single filters from a PPD file.
534 const char *command
, /* I - Command name */
535 mime_t
*mime
, /* I - MIME database */
536 mime_type_t
*filtertype
, /* I - Printer or prefilter MIME type */
537 const char *filter
) /* I - Filter to add */
539 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
540 type
[MIME_MAX_TYPE
], /* Type for filter */
541 dsuper
[MIME_MAX_SUPER
], /* Destination super-type for filter */
542 dtype
[MIME_MAX_TYPE
], /* Destination type for filter */
543 dest
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2],
544 /* Destination super/type */
545 program
[1024]; /* Program/filter name */
546 int cost
; /* Cost of filter */
547 size_t maxsize
= 0; /* Maximum supported file size */
548 mime_type_t
*temptype
, /* MIME type looping var */
549 *desttype
; /* Destination MIME type */
550 mime_filter_t
*filterptr
; /* MIME filter */
554 * Parse the filter string; it should be in one of the following formats:
556 * source/type cost program
557 * source/type cost maxsize(nnnn) program
558 * source/type dest/type cost program
559 * source/type dest/type cost maxsize(nnnn) program
562 if (sscanf(filter
, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
563 super
, type
, dsuper
, dtype
, &cost
, program
) == 6)
565 snprintf(dest
, sizeof(dest
), "%s/%s/%s", filtertype
->type
, dsuper
, dtype
);
567 if ((desttype
= mimeType(mime
, "printer", dest
)) == NULL
)
568 desttype
= mimeAddType(mime
, "printer", dest
);
572 if (sscanf(filter
, "%15[^/]/%255s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
575 desttype
= filtertype
;
579 _cupsLangPrintf(stderr
, _("%s: Invalid filter string \"%s\"."), command
,
585 if (!strncmp(program
, "maxsize(", 8))
587 char *ptr
; /* Pointer into maxsize(nnnn) program */
589 maxsize
= (size_t)strtoll(program
+ 8, &ptr
, 10);
593 printf("testmime: Invalid filter string \"%s\".\n", filter
);
598 while (_cups_isspace(*ptr
))
601 _cups_strcpy(program
, ptr
);
605 * See if the filter program exists; if not, stop the printer and flag
609 if (strcmp(program
, "-"))
611 char filename
[1024]; /* Full path to program */
613 if (program
[0] == '/')
614 strlcpy(filename
, program
, sizeof(filename
));
616 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
618 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_PROGRAM
, !geteuid(), check_cb
,
624 * Add the filter to the MIME database, supporting wildcards as needed...
627 for (temptype
= mimeFirstType(mime
);
629 temptype
= mimeNextType(mime
))
630 if (((super
[0] == '*' && _cups_strcasecmp(temptype
->super
, "printer")) ||
631 !_cups_strcasecmp(temptype
->super
, super
)) &&
632 (type
[0] == '*' || !_cups_strcasecmp(temptype
->type
, type
)))
634 if (desttype
!= filtertype
)
636 filterptr
= mimeAddFilter(mime
, temptype
, desttype
, cost
, program
);
638 if (!mimeFilterLookup(mime
, desttype
, filtertype
))
639 mimeAddFilter(mime
, desttype
, filtertype
, 0, "-");
642 filterptr
= mimeAddFilter(mime
, temptype
, filtertype
, cost
, program
);
645 filterptr
->maxsize
= maxsize
;
651 * 'add_printer_filters()' - Add filters from a PPD file.
654 static mime_type_t
* /* O - Printer type or NULL on error */
656 const char *command
, /* I - Command name */
657 mime_t
*mime
, /* I - MIME database */
658 const char *printer
, /* I - Printer name */
659 const char *ppdfile
, /* I - PPD file */
660 mime_type_t
**prefilter_type
) /* O - Prefilter type */
662 ppd_file_t
*ppd
; /* PPD file data */
663 _ppd_cache_t
*pc
; /* Cache data for PPD */
664 const char *value
; /* Filter definition value */
665 mime_type_t
*printer_type
; /* Printer filter type */
668 if ((ppd
= _ppdOpenFile(ppdfile
, _PPD_LOCALIZATION_NONE
)) == NULL
)
670 ppd_status_t status
; /* PPD load status */
671 int linenum
; /* Line number */
673 status
= ppdLastError(&linenum
);
674 _cupsLangPrintf(stderr
, _("%s: Unable to open PPD file: %s on line %d."),
675 command
, ppdErrorString(status
), linenum
);
679 pc
= _ppdCacheCreateWithPPD(ppd
);
683 printer_type
= mimeAddType(mime
, "printer", printer
);
684 *prefilter_type
= NULL
;
688 for (value
= (const char *)cupsArrayFirst(pc
->filters
);
690 value
= (const char *)cupsArrayNext(pc
->filters
))
691 add_printer_filter(command
, mime
, printer_type
, value
);
695 add_printer_filter(command
, mime
, printer_type
,
696 "application/vnd.cups-raw 0 -");
697 add_printer_filter(command
, mime
, printer_type
,
698 "application/vnd.cups-postscript 0 -");
703 *prefilter_type
= mimeAddType(mime
, "prefilter", printer
);
705 for (value
= (const char *)cupsArrayFirst(pc
->prefilters
);
707 value
= (const char *)cupsArrayNext(pc
->prefilters
))
708 add_printer_filter(command
, mime
, *prefilter_type
, value
);
711 return (printer_type
);
716 * 'check_cb()' - Callback function for _cupsFileCheck.
720 check_cb(void *context
, /* I - Context (command name) */
721 _cups_fc_result_t result
, /* I - Result of check */
722 const char *message
) /* I - Localized message */
726 _cupsLangPrintf(stderr
, _("%s: %s"), (char *)context
, message
);
731 * 'compare_pids()' - Compare two filter PIDs...
734 static int /* O - Result of comparison */
735 compare_pids(mime_filter_t
*a
, /* I - First filter */
736 mime_filter_t
*b
) /* I - Second filter */
739 * Because we're particularly lazy, we store the process ID in the "cost"
743 return (a
->cost
- b
->cost
);
748 * 'escape_options()' - Convert an options array to a string.
751 static char * /* O - Option string */
753 int num_options
, /* I - Number of options */
754 cups_option_t
*options
) /* I - Options */
756 int i
; /* Looping var */
757 cups_option_t
*option
; /* Current option */
758 size_t bytes
; /* Number of bytes needed */
759 char *s
, /* Option string */
760 *sptr
, /* Pointer into string */
761 *vptr
; /* Pointer into value */
765 * Figure out the worst-case number of bytes we need for the option string.
768 for (i
= num_options
, option
= options
, bytes
= 1; i
> 0; i
--, option
++)
769 bytes
+= 2 * (strlen(option
->name
) + strlen(option
->value
)) + 2;
771 if ((s
= malloc(bytes
)) == NULL
)
775 * Copy the options to the string...
778 for (i
= num_options
, option
= options
, sptr
= s
; i
> 0; i
--, option
++)
780 if (!strcmp(option
->name
, "copies"))
786 strlcpy(sptr
, option
->name
, bytes
- (size_t)(sptr
- s
));
787 sptr
+= strlen(sptr
);
790 for (vptr
= option
->value
; *vptr
;)
792 if (strchr("\\ \t\n", *vptr
))
806 * 'exec_filter()' - Execute a single filter.
809 static int /* O - Process ID or -1 on error */
810 exec_filter(const char *filter
, /* I - Filter to execute */
811 char **argv
, /* I - Argument list */
812 char **envp
, /* I - Environment list */
813 int infd
, /* I - Stdin file descriptor */
814 int outfd
) /* I - Stdout file descriptor */
816 int pid
, /* Process ID */
817 fd
; /* Temporary file descriptor */
818 #if defined(__APPLE__)
819 char processPath
[1024], /* CFProcessPath environment variable */
820 linkpath
[1024]; /* Link path for symlinks... */
821 int linkbytes
; /* Bytes for link path */
825 * Add special voodoo magic for MacOS X - this allows MacOS X
826 * programs to access their bundle resources properly...
829 if ((linkbytes
= readlink(filter
, linkpath
, sizeof(linkpath
) - 1)) > 0)
832 * Yes, this is a symlink to the actual program, nul-terminate and
836 linkpath
[linkbytes
] = '\0';
838 if (linkpath
[0] == '/')
839 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s",
842 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s/%s",
843 dirname((char *)filter
), linkpath
);
846 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s", filter
);
848 envp
[0] = processPath
; /* Replace <CFProcessPath> string */
849 #endif /* __APPLE__ */
851 if ((pid
= fork()) == 0)
854 * Child process goes here...
856 * Update stdin/stdout/stderr as needed...
862 infd
= open("/dev/null", O_RDONLY
);
874 outfd
= open("/dev/null", O_WRONLY
);
883 if ((fd
= open("/dev/null", O_RDWR
)) > 3)
888 fcntl(3, F_SETFL
, O_NDELAY
);
890 if ((fd
= open("/dev/null", O_RDWR
)) > 4)
895 fcntl(4, F_SETFL
, O_NDELAY
);
901 execve(filter
, argv
, envp
);
913 * 'exec_filters()' - Execute filters for the given file and options.
916 static int /* O - 0 on success, 1 on error */
917 exec_filters(mime_type_t
*srctype
, /* I - Source type */
918 cups_array_t
*filters
, /* I - Array of filters to run */
919 const char *infile
, /* I - File to filter */
920 const char *outfile
, /* I - File to create */
921 const char *ppdfile
, /* I - PPD file, if any */
922 const char *printer
, /* I - Printer name */
923 const char *user
, /* I - Username */
924 const char *title
, /* I - Job title */
925 int num_options
, /* I - Number of filter options */
926 cups_option_t
*options
) /* I - Filter options */
928 int i
; /* Looping var */
929 const char *argv
[8], /* Command-line arguments */
930 *envp
[17], /* Environment variables */
931 *temp
; /* Temporary string */
932 char *optstr
, /* Filter options */
933 content_type
[1024], /* CONTENT_TYPE */
934 cups_datadir
[1024], /* CUPS_DATADIR */
935 cups_fontpath
[1024], /* CUPS_FONTPATH */
936 cups_serverbin
[1024], /* CUPS_SERVERBIN */
937 cups_serverroot
[1024], /* CUPS_SERVERROOT */
938 final_content_type
[1024] = "",
939 /* FINAL_CONTENT_TYPE */
940 lang
[1024], /* LANG */
941 path
[1024], /* PATH */
943 printer_info
[255], /* PRINTER_INFO env variable */
944 printer_location
[255], /* PRINTER_LOCATION env variable */
945 printer_name
[255], /* PRINTER env variable */
946 rip_max_cache
[1024], /* RIP_MAX_CACHE */
947 userenv
[1024], /* USER */
948 program
[1024]; /* Program to run */
949 mime_filter_t
*filter
, /* Current filter */
950 *next
; /* Next filter */
951 int current
, /* Current filter */
952 filterfds
[2][2], /* Pipes for filters */
953 pid
, /* Process ID of filter */
954 status
, /* Exit status */
955 retval
; /* Return value */
956 cups_array_t
*pids
; /* Executed filters array */
957 mime_filter_t key
; /* Search key for filters */
958 cups_lang_t
*language
; /* Current language */
959 cups_dest_t
*dest
; /* Destination information */
963 * Figure out the final content type...
966 for (filter
= (mime_filter_t
*)cupsArrayLast(filters
);
967 filter
&& filter
->dst
;
968 filter
= (mime_filter_t
*)cupsArrayPrev(filters
))
969 if (strcmp(filter
->dst
->super
, "printer"))
972 if (filter
&& filter
->dst
)
974 const char *ptr
; /* Pointer in type name */
976 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
977 snprintf(final_content_type
, sizeof(final_content_type
),
978 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
980 snprintf(final_content_type
, sizeof(final_content_type
),
981 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
986 * Remove NULL ("-") filters...
989 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
991 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
992 if (!strcmp(filter
->filter
, "-"))
993 cupsArrayRemove(filters
, filter
);
996 * Setup the filter environment and command-line...
999 optstr
= escape_options(num_options
, options
);
1001 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
1002 srctype
->super
, srctype
->type
);
1003 snprintf(cups_datadir
, sizeof(cups_datadir
), "CUPS_DATADIR=%s", DataDir
);
1004 snprintf(cups_fontpath
, sizeof(cups_fontpath
), "CUPS_FONTPATH=%s", FontPath
);
1005 snprintf(cups_serverbin
, sizeof(cups_serverbin
), "CUPS_SERVERBIN=%s",
1007 snprintf(cups_serverroot
, sizeof(cups_serverroot
), "CUPS_SERVERROOT=%s",
1009 language
= cupsLangDefault();
1010 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", language
->language
);
1011 snprintf(path
, sizeof(path
), "PATH=%s", Path
);
1013 snprintf(ppd
, sizeof(ppd
), "PPD=%s", ppdfile
);
1014 else if ((temp
= getenv("PPD")) != NULL
)
1015 snprintf(ppd
, sizeof(ppd
), "PPD=%s", temp
);
1018 if (!access("/System/Library/Frameworks/ApplicationServices.framework/"
1019 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
1020 "Resources/English.lproj/Generic.ppd", 0))
1021 strlcpy(ppd
, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
1022 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
1023 "Resources/English.lproj/Generic.ppd", sizeof(ppd
));
1025 strlcpy(ppd
, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
1026 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
1027 "Resources/Generic.ppd", sizeof(ppd
));
1029 snprintf(ppd
, sizeof(ppd
), "PPD=%s/model/laserjet.ppd", DataDir
);
1030 #endif /* __APPLE__ */
1031 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
1032 snprintf(userenv
, sizeof(userenv
), "USER=%s", user
);
1035 (dest
= cupsGetNamedDest(CUPS_HTTP_DEFAULT
, printer
, NULL
)) != NULL
)
1037 if ((temp
= cupsGetOption("printer-info", dest
->num_options
,
1038 dest
->options
)) != NULL
)
1039 snprintf(printer_info
, sizeof(printer_info
), "PRINTER_INFO=%s", temp
);
1041 snprintf(printer_info
, sizeof(printer_info
), "PRINTER_INFO=%s", printer
);
1043 if ((temp
= cupsGetOption("printer-location", dest
->num_options
,
1044 dest
->options
)) != NULL
)
1045 snprintf(printer_location
, sizeof(printer_location
),
1046 "PRINTER_LOCATION=%s", temp
);
1048 strlcpy(printer_location
, "PRINTER_LOCATION=Unknown",
1049 sizeof(printer_location
));
1053 snprintf(printer_info
, sizeof(printer_info
), "PRINTER_INFO=%s",
1054 printer
? printer
: "Unknown");
1055 strlcpy(printer_location
, "PRINTER_LOCATION=Unknown",
1056 sizeof(printer_location
));
1059 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s",
1060 printer
? printer
: "Unknown");
1062 argv
[0] = (char *)printer
;
1066 argv
[4] = cupsGetOption("copies", num_options
, options
);
1074 envp
[0] = "<CFProcessPath>";
1075 envp
[1] = content_type
;
1076 envp
[2] = cups_datadir
;
1077 envp
[3] = cups_fontpath
;
1078 envp
[4] = cups_serverbin
;
1079 envp
[5] = cups_serverroot
;
1083 envp
[9] = printer_info
;
1084 envp
[10] = printer_location
;
1085 envp
[11] = printer_name
;
1086 envp
[12] = rip_max_cache
;
1088 envp
[14] = "CHARSET=utf-8";
1089 if (final_content_type
[0])
1091 envp
[15] = final_content_type
;
1097 for (i
= 0; argv
[i
]; i
++)
1098 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
1100 for (i
= 0; envp
[i
]; i
++)
1101 fprintf(stderr
, "DEBUG: envp[%d]=\"%s\"\n", i
, envp
[i
]);
1104 * Execute all of the filters...
1107 pids
= cupsArrayNew((cups_array_func_t
)compare_pids
, NULL
);
1109 filterfds
[0][0] = -1;
1110 filterfds
[0][1] = -1;
1111 filterfds
[1][0] = -1;
1112 filterfds
[1][1] = -1;
1115 filterfds
[0][0] = 0;
1117 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1119 filter
= next
, current
= 1 - current
)
1121 next
= (mime_filter_t
*)cupsArrayNext(filters
);
1123 if (filter
->filter
[0] == '/')
1124 strlcpy(program
, filter
->filter
, sizeof(program
));
1126 snprintf(program
, sizeof(program
), "%s/filter/%s", ServerBin
,
1129 if (filterfds
[!current
][1] > 1)
1131 close(filterfds
[1 - current
][0]);
1132 close(filterfds
[1 - current
][1]);
1134 filterfds
[1 - current
][0] = -1;
1135 filterfds
[1 - current
][0] = -1;
1139 open_pipe(filterfds
[1 - current
]);
1142 filterfds
[1 - current
][1] = open(outfile
, O_CREAT
| O_TRUNC
| O_WRONLY
,
1145 if (filterfds
[1 - current
][1] < 0)
1146 fprintf(stderr
, "ERROR: Unable to create \"%s\" - %s\n", outfile
,
1150 filterfds
[1 - current
][1] = 1;
1152 pid
= exec_filter(program
, (char **)argv
, (char **)envp
,
1153 filterfds
[current
][0], filterfds
[1 - current
][1]);
1157 fprintf(stderr
, "INFO: %s (PID %d) started.\n", filter
->filter
, pid
);
1160 cupsArrayAdd(pids
, filter
);
1169 * Close remaining pipes...
1172 if (filterfds
[0][1] > 1)
1174 close(filterfds
[0][0]);
1175 close(filterfds
[0][1]);
1178 if (filterfds
[1][1] > 1)
1180 close(filterfds
[1][0]);
1181 close(filterfds
[1][1]);
1185 * Wait for the children to exit...
1190 while (cupsArrayCount(pids
) > 0)
1192 if ((pid
= wait(&status
)) < 0)
1196 if ((filter
= (mime_filter_t
*)cupsArrayFind(pids
, &key
)) != NULL
)
1198 cupsArrayRemove(pids
, filter
);
1202 if (WIFEXITED(status
))
1203 fprintf(stderr
, "ERROR: %s (PID %d) stopped with status %d\n",
1204 filter
->filter
, pid
, WEXITSTATUS(status
));
1206 fprintf(stderr
, "ERROR: %s (PID %d) crashed on signal %d\n",
1207 filter
->filter
, pid
, WTERMSIG(status
));
1212 fprintf(stderr
, "INFO: %s (PID %d) exited with no errors.\n",
1213 filter
->filter
, pid
);
1217 cupsArrayDelete(pids
);
1224 * 'get_job_file()' - Get the specified job file.
1228 get_job_file(const char *job
) /* I - Job ID */
1230 long jobid
, /* Job ID */
1231 docnum
; /* Document number */
1232 const char *jobptr
; /* Pointer into job ID string */
1233 char uri
[1024]; /* job-uri */
1234 http_t
*http
; /* Connection to server */
1235 ipp_t
*request
; /* Request data */
1236 int tempfd
; /* Temporary file */
1240 * Get the job ID and document number, if any...
1243 if ((jobptr
= strrchr(job
, '-')) != NULL
)
1248 jobid
= strtol(jobptr
, (char **)&jobptr
, 10);
1251 docnum
= strtol(jobptr
+ 1, NULL
, 10);
1255 if (jobid
< 1 || jobid
> INT_MAX
)
1257 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid job ID %d."), (int)jobid
);
1261 if (docnum
< 1 || docnum
> INT_MAX
)
1263 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid document number %d."),
1269 * Ask the server for the document file...
1272 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1273 cupsEncryption())) == NULL
)
1275 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server."),
1280 request
= ippNewRequest(CUPS_GET_DOCUMENT
);
1282 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", (int)jobid
);
1284 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1285 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "document-number",
1288 if ((tempfd
= cupsTempFd(TempFile
, sizeof(TempFile
))) == -1)
1290 _cupsLangPrintError("ERROR", _("Unable to create temporary file"));
1295 signal(SIGTERM
, sighandler
);
1297 ippDelete(cupsDoIORequest(http
, request
, "/", -1, tempfd
));
1303 if (cupsLastError() != IPP_OK
)
1305 _cupsLangPrintf(stderr
, _("cupsfilter: Unable to get job file - %s"),
1306 cupsLastErrorString());
1314 * 'open_pipe()' - Create a pipe which is closed on exec.
1317 static int /* O - 0 on success, -1 on error */
1318 open_pipe(int *fds
) /* O - Pipe file descriptors (2) */
1321 * Create the pipe...
1333 * Set the "close on exec" flag on each end of the pipe...
1336 if (fcntl(fds
[0], F_SETFD
, fcntl(fds
[0], F_GETFD
) | FD_CLOEXEC
))
1347 if (fcntl(fds
[1], F_SETFD
, fcntl(fds
[1], F_GETFD
) | FD_CLOEXEC
))
1359 * Return 0 indicating success...
1367 * 'read_cups_files_conf()' - Read the cups-files.conf file to get the filter settings.
1370 static int /* O - 0 on success, 1 on error */
1371 read_cups_files_conf(
1372 const char *filename
) /* I - File to read */
1374 cups_file_t
*fp
; /* cups-files.conf file */
1375 const char *temp
; /* Temporary string */
1376 char line
[1024], /* Line from file */
1377 *ptr
; /* Pointer into line */
1378 int linenum
; /* Current line number */
1381 if ((temp
= getenv("CUPS_DATADIR")) != NULL
)
1382 set_string(&DataDir
, temp
);
1384 set_string(&DataDir
, CUPS_DATADIR
);
1386 if ((temp
= getenv("CUPS_FONTPATH")) != NULL
)
1387 set_string(&FontPath
, temp
);
1389 set_string(&FontPath
, CUPS_FONTPATH
);
1391 set_string(&RIPCache
, "128m");
1393 if ((temp
= getenv("CUPS_SERVERBIN")) != NULL
)
1394 set_string(&ServerBin
, temp
);
1396 set_string(&ServerBin
, CUPS_SERVERBIN
);
1398 strlcpy(line
, filename
, sizeof(line
));
1399 if ((ptr
= strrchr(line
, '/')) != NULL
)
1402 getcwd(line
, sizeof(line
));
1404 set_string(&ServerRoot
, line
);
1406 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1410 while (cupsFileGetConf(fp
, line
, sizeof(line
), &ptr
, &linenum
))
1412 if (!_cups_strcasecmp(line
, "DataDir"))
1413 set_string(&DataDir
, ptr
);
1414 else if (!_cups_strcasecmp(line
, "FontPath"))
1415 set_string(&FontPath
, ptr
);
1416 else if (!_cups_strcasecmp(line
, "RIPCache"))
1417 set_string(&RIPCache
, ptr
);
1418 else if (!_cups_strcasecmp(line
, "ServerBin"))
1419 set_string(&ServerBin
, ptr
);
1420 else if (!_cups_strcasecmp(line
, "ServerRoot"))
1421 set_string(&ServerRoot
, ptr
);
1427 snprintf(line
, sizeof(line
), "%s/filter:" CUPS_BINDIR
":" CUPS_SBINDIR
":/bin:/usr/bin", ServerBin
);
1428 set_string(&Path
, line
);
1435 * 'set_string()' - Copy and set a string.
1439 set_string(char **s
, /* O - Copy of string */
1440 const char *val
) /* I - String to copy */
1450 * 'sighandler()' - Signal catcher for when we print from stdin...
1454 sighandler(int s
) /* I - Signal number */
1457 * Remove the temporary file we're using to print a job file...
1472 * 'usage()' - Show program usage...
1476 usage(const char *opt
) /* I - Incorrect option, if any */
1479 _cupsLangPrintf(stderr
, _("%s: Unknown option \"%c\"."), "cupsfilter", *opt
);
1481 _cupsLangPuts(stdout
, _("Usage: cupsfilter [ options ] [ -- ] filename"));
1482 _cupsLangPuts(stdout
, _("Options:"));
1483 _cupsLangPuts(stdout
, _(" --list-filters List filters that will be used."));
1484 _cupsLangPuts(stdout
, _(" -D Remove the input file when finished."));
1485 _cupsLangPuts(stdout
, _(" -P filename.ppd Set PPD file."));
1486 _cupsLangPuts(stdout
, _(" -U username Specify username."));
1487 _cupsLangPuts(stdout
, _(" -c cups-files.conf Set cups-files.conf file to use."));
1488 _cupsLangPuts(stdout
, _(" -d printer Use the named printer."));
1489 _cupsLangPuts(stdout
, _(" -e Use every filter from the PPD file."));
1490 _cupsLangPuts(stdout
, _(" -i mime/type Set input MIME type (otherwise auto-typed)."));
1491 _cupsLangPuts(stdout
, _(" -j job-id[,N] Filter file N from the specified job (default is file 1)."));
1492 _cupsLangPuts(stdout
, _(" -m mime/type Set output MIME type (otherwise application/pdf)."));
1493 _cupsLangPuts(stdout
, _(" -n copies Set number of copies."));
1494 _cupsLangPuts(stdout
, _(" -o name=value Set option(s)."));
1495 _cupsLangPuts(stdout
, _(" -p filename.ppd Set PPD file."));
1496 _cupsLangPuts(stdout
, _(" -t title Set title."));
1497 _cupsLangPuts(stdout
, _(" -u Remove the PPD file when finished."));