]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cupsfilter.c
2 * "$Id: cupsfilter.c 11772 2014-03-28 15:08:30Z msweet $"
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 const char *command
, /* Command name */
100 *opt
, /* Current option */
101 *printer
; /* Printer name */
102 mime_type_t
*printer_type
, /* Printer MIME type */
103 *prefilter_type
; /* Printer prefilter MIME type */
104 char *srctype
, /* Source type */
105 *dsttype
, /* Destination type */
106 super
[MIME_MAX_SUPER
], /* Super-type name */
107 type
[MIME_MAX_TYPE
]; /* Type name */
108 int compression
; /* Compression of file */
109 int cost
; /* Cost of filters */
110 mime_t
*mime
; /* MIME database */
111 char mimedir
[1024]; /* MIME directory */
112 char *infile
, /* File to filter */
113 *outfile
; /* File to create */
114 char cupsfilesconf
[1024]; /* cups-files.conf file */
115 const char *server_root
; /* CUPS_SERVERROOT environment variable */
116 mime_type_t
*src
, /* Source type */
117 *dst
; /* Destination type */
118 cups_array_t
*filters
; /* Filters for the file */
119 int num_options
; /* Number of options */
120 cups_option_t
*options
; /* Options */
121 const char *ppdfile
; /* PPD file */
122 const char *title
, /* Title string */
123 *user
; /* Username */
124 int all_filters
, /* Use all filters */
125 removeppd
, /* Remove PPD file */
126 removeinfile
; /* Remove input file */
127 int status
; /* Execution status */
134 if ((command
= strrchr(argv
[0], '/')) != NULL
)
139 printer
= !strcmp(command
, "convert") ? "tofile" : "cupsfilter";
143 dsttype
= "application/pdf";
155 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
156 server_root
= CUPS_SERVERROOT
;
158 snprintf(cupsfilesconf
, sizeof(cupsfilesconf
), "%s/cups-files.conf", server_root
);
161 * Process command-line arguments...
164 _cupsSetLocale(argv
);
166 for (i
= 1; i
< argc
; i
++)
167 if (argv
[i
][0] == '-')
169 for (opt
= argv
[i
] + 1; *opt
; opt
++)
172 case '-' : /* Next argument is a filename... */
174 if (i
< argc
&& !infile
)
180 case 'a' : /* Specify option... */
183 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
188 case 'c' : /* Specify cups-files.conf file location... */
192 if (!strcmp(command
, "convert"))
193 num_options
= cupsAddOption("copies", argv
[i
], num_options
,
196 strlcpy(cupsfilesconf
, argv
[i
], sizeof(cupsfilesconf
));
202 case 'd' : /* Specify the real printer name */
210 case 'D' : /* Delete input file after conversion */
214 case 'e' : /* Use every filter from the PPD file */
218 case 'f' : /* Specify input file... */
220 if (i
< argc
&& !infile
)
226 case 'i' : /* Specify source MIME type... */
230 if (sscanf(argv
[i
], "%15[^/]/%255s", super
, type
) != 2)
239 case 'j' : /* Get job file or specify destination MIME type... */
240 if (strcmp(command
, "convert"))
245 get_job_file(argv
[i
]);
254 case 'm' : /* Specify destination MIME type... */
258 if (sscanf(argv
[i
], "%15[^/]/%255s", super
, type
) != 2)
267 case 'n' : /* Specify number of copies... */
270 num_options
= cupsAddOption("copies", argv
[i
], num_options
,
276 case 'o' : /* Specify option(s) or output filename */
280 if (!strcmp(command
, "convert"))
288 num_options
= cupsParseOptions(argv
[i
], num_options
,
295 case 'p' : /* Specify PPD file... */
296 case 'P' : /* Specify PPD file... */
304 case 't' : /* Specify title... */
305 case 'J' : /* Specify title... */
313 case 'u' : /* Delete PPD file after conversion */
317 case 'U' : /* Specify username... */
325 default : /* Something we don't understand... */
332 if (strcmp(command
, "convert"))
339 _cupsLangPuts(stderr
,
340 _("cupsfilter: Only one filename can be specified."));
344 if (!infile
&& !srctype
)
351 else if ((title
= strrchr(infile
, '/')) != NULL
)
358 * Load the cups-files.conf file and create the MIME database...
361 if (read_cups_files_conf(cupsfilesconf
))
364 snprintf(mimedir
, sizeof(mimedir
), "%s/mime", DataDir
);
366 mime
= mimeLoadTypes(NULL
, mimedir
);
367 mime
= mimeLoadTypes(mime
, ServerRoot
);
368 mime
= mimeLoadFilters(mime
, mimedir
, Path
);
369 mime
= mimeLoadFilters(mime
, ServerRoot
, Path
);
373 _cupsLangPrintf(stderr
,
374 _("%s: Unable to read MIME database from \"%s\" or "
376 command
, mimedir
, ServerRoot
);
380 prefilter_type
= NULL
;
383 printer_type
= add_printer_filters(command
, mime
, printer
, ppdfile
,
386 printer_type
= mimeType(mime
, "application", "vnd.cups-postscript");
389 * Get the source and destination types...
394 /* sscanf return value already checked above */
395 sscanf(srctype
, "%15[^/]/%255s", super
, type
);
396 if ((src
= mimeType(mime
, super
, type
)) == NULL
)
398 _cupsLangPrintf(stderr
,
399 _("%s: Unknown source MIME type %s/%s."),
400 command
, super
, type
);
404 else if ((src
= mimeFileType(mime
, infile
, infile
, &compression
)) == NULL
)
406 _cupsLangPrintf(stderr
,
407 _("%s: Unable to determine MIME type of \"%s\"."),
412 /* sscanf return value already checked above */
413 sscanf(dsttype
, "%15[^/]/%255s", super
, type
);
414 if (!_cups_strcasecmp(super
, "printer"))
416 else if ((dst
= mimeType(mime
, super
, type
)) == NULL
)
418 _cupsLangPrintf(stderr
,
419 _("%s: Unknown destination MIME type %s/%s."),
420 command
, super
, type
);
425 * Figure out how to filter the file...
431 * Special case - no filtering needed...
434 filters
= cupsArrayNew(NULL
, NULL
);
435 cupsArrayAdd(filters
, &GZIPFilter
);
436 GZIPFilter
.src
= src
;
437 GZIPFilter
.dst
= dst
;
439 else if ((filters
= mimeFilter(mime
, src
, dst
, &cost
)) == NULL
)
441 _cupsLangPrintf(stderr
,
442 _("%s: No filter to convert from %s/%s to %s/%s."),
443 command
, src
->super
, src
->type
, dst
->super
, dst
->type
);
446 else if (compression
)
447 cupsArrayInsert(filters
, &GZIPFilter
);
455 mime_filter_t
*filter
, /* Current filter */
456 *prefilter
; /* Current pre-filter */
457 cups_array_t
*prefilters
= cupsArrayNew(NULL
, NULL
);
458 /* New filters array */
461 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
463 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
465 if ((prefilter
= mimeFilterLookup(mime
, filter
->src
,
466 prefilter_type
)) != NULL
)
467 cupsArrayAdd(prefilters
, prefilter
);
469 cupsArrayAdd(prefilters
, filter
);
472 cupsArrayDelete(filters
);
473 filters
= prefilters
;
480 status
= exec_filters(src
, filters
, infile
, outfile
, ppdfile
, printer
, user
,
481 title
, num_options
, options
);
484 * Remove files as needed, then exit...
490 if (removeppd
&& ppdfile
)
493 if (removeinfile
&& infile
)
501 * 'add_printer_filter()' - Add a single filters from a PPD file.
506 const char *command
, /* I - Command name */
507 mime_t
*mime
, /* I - MIME database */
508 mime_type_t
*filtertype
, /* I - Printer or prefilter MIME type */
509 const char *filter
) /* I - Filter to add */
511 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
512 type
[MIME_MAX_TYPE
], /* Type for filter */
513 dsuper
[MIME_MAX_SUPER
], /* Destination super-type for filter */
514 dtype
[MIME_MAX_TYPE
], /* Destination type for filter */
515 dest
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2],
516 /* Destination super/type */
517 program
[1024]; /* Program/filter name */
518 int cost
; /* Cost of filter */
519 size_t maxsize
= 0; /* Maximum supported file size */
520 mime_type_t
*temptype
, /* MIME type looping var */
521 *desttype
; /* Destination MIME type */
522 mime_filter_t
*filterptr
; /* MIME filter */
526 * Parse the filter string; it should be in one of the following formats:
528 * source/type cost program
529 * source/type cost maxsize(nnnn) program
530 * source/type dest/type cost program
531 * source/type dest/type cost maxsize(nnnn) program
534 if (sscanf(filter
, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
535 super
, type
, dsuper
, dtype
, &cost
, program
) == 6)
537 snprintf(dest
, sizeof(dest
), "%s/%s/%s", filtertype
->type
, dsuper
, dtype
);
539 if ((desttype
= mimeType(mime
, "printer", dest
)) == NULL
)
540 desttype
= mimeAddType(mime
, "printer", dest
);
544 if (sscanf(filter
, "%15[^/]/%255s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
547 desttype
= filtertype
;
551 _cupsLangPrintf(stderr
, _("%s: Invalid filter string \"%s\"."), command
,
557 if (!strncmp(program
, "maxsize(", 8))
559 char *ptr
; /* Pointer into maxsize(nnnn) program */
561 maxsize
= strtoll(program
+ 8, &ptr
, 10);
565 printf("testmime: Invalid filter string \"%s\".\n", filter
);
570 while (_cups_isspace(*ptr
))
573 _cups_strcpy(program
, ptr
);
577 * See if the filter program exists; if not, stop the printer and flag
581 if (strcmp(program
, "-"))
583 char filename
[1024]; /* Full path to program */
585 if (program
[0] == '/')
586 strlcpy(filename
, program
, sizeof(filename
));
588 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
590 if (_cupsFileCheck(filename
, _CUPS_FILE_CHECK_PROGRAM
, !geteuid(), check_cb
,
596 * Add the filter to the MIME database, supporting wildcards as needed...
599 for (temptype
= mimeFirstType(mime
);
601 temptype
= mimeNextType(mime
))
602 if (((super
[0] == '*' && _cups_strcasecmp(temptype
->super
, "printer")) ||
603 !_cups_strcasecmp(temptype
->super
, super
)) &&
604 (type
[0] == '*' || !_cups_strcasecmp(temptype
->type
, type
)))
606 if (desttype
!= filtertype
)
608 filterptr
= mimeAddFilter(mime
, temptype
, desttype
, cost
, program
);
610 if (!mimeFilterLookup(mime
, desttype
, filtertype
))
611 mimeAddFilter(mime
, desttype
, filtertype
, 0, "-");
614 filterptr
= mimeAddFilter(mime
, temptype
, filtertype
, cost
, program
);
617 filterptr
->maxsize
= maxsize
;
623 * 'add_printer_filters()' - Add filters from a PPD file.
626 static mime_type_t
* /* O - Printer type or NULL on error */
628 const char *command
, /* I - Command name */
629 mime_t
*mime
, /* I - MIME database */
630 const char *printer
, /* I - Printer name */
631 const char *ppdfile
, /* I - PPD file */
632 mime_type_t
**prefilter_type
) /* O - Prefilter type */
634 ppd_file_t
*ppd
; /* PPD file data */
635 _ppd_cache_t
*pc
; /* Cache data for PPD */
636 const char *value
; /* Filter definition value */
637 mime_type_t
*printer_type
; /* Printer filter type */
640 if ((ppd
= _ppdOpenFile(ppdfile
, _PPD_LOCALIZATION_NONE
)) == NULL
)
642 ppd_status_t status
; /* PPD load status */
643 int linenum
; /* Line number */
645 status
= ppdLastError(&linenum
);
646 _cupsLangPrintf(stderr
, _("%s: Unable to open PPD file: %s on line %d."),
647 command
, ppdErrorString(status
), linenum
);
651 pc
= _ppdCacheCreateWithPPD(ppd
);
655 printer_type
= mimeAddType(mime
, "printer", printer
);
656 *prefilter_type
= NULL
;
660 for (value
= (const char *)cupsArrayFirst(pc
->filters
);
662 value
= (const char *)cupsArrayNext(pc
->filters
))
663 add_printer_filter(command
, mime
, printer_type
, value
);
667 add_printer_filter(command
, mime
, printer_type
,
668 "application/vnd.cups-raw 0 -");
669 add_printer_filter(command
, mime
, printer_type
,
670 "application/vnd.cups-postscript 0 -");
675 *prefilter_type
= mimeAddType(mime
, "prefilter", printer
);
677 for (value
= (const char *)cupsArrayFirst(pc
->prefilters
);
679 value
= (const char *)cupsArrayNext(pc
->prefilters
))
680 add_printer_filter(command
, mime
, *prefilter_type
, value
);
683 return (printer_type
);
688 * 'check_cb()' - Callback function for _cupsFileCheck.
692 check_cb(void *context
, /* I - Context (command name) */
693 _cups_fc_result_t result
, /* I - Result of check */
694 const char *message
) /* I - Localized message */
698 _cupsLangPrintf(stderr
, _("%s: %s"), (char *)context
, message
);
703 * 'compare_pids()' - Compare two filter PIDs...
706 static int /* O - Result of comparison */
707 compare_pids(mime_filter_t
*a
, /* I - First filter */
708 mime_filter_t
*b
) /* I - Second filter */
711 * Because we're particularly lazy, we store the process ID in the "cost"
715 return (a
->cost
- b
->cost
);
720 * 'escape_options()' - Convert an options array to a string.
723 static char * /* O - Option string */
725 int num_options
, /* I - Number of options */
726 cups_option_t
*options
) /* I - Options */
728 int i
; /* Looping var */
729 cups_option_t
*option
; /* Current option */
730 int bytes
; /* Number of bytes needed */
731 char *s
, /* Option string */
732 *sptr
, /* Pointer into string */
733 *vptr
; /* Pointer into value */
737 * Figure out the worst-case number of bytes we need for the option string.
740 for (i
= num_options
, option
= options
, bytes
= 1; i
> 0; i
--, option
++)
741 bytes
+= 2 * (strlen(option
->name
) + strlen(option
->value
)) + 2;
743 if ((s
= malloc(bytes
)) == NULL
)
747 * Copy the options to the string...
750 for (i
= num_options
, option
= options
, sptr
= s
; i
> 0; i
--, option
++)
752 if (!strcmp(option
->name
, "copies"))
758 strlcpy(sptr
, option
->name
, bytes
- (sptr
- s
));
759 sptr
+= strlen(sptr
);
762 for (vptr
= option
->value
; *vptr
;)
764 if (strchr("\\ \t\n", *vptr
))
778 * 'exec_filter()' - Execute a single filter.
781 static int /* O - Process ID or -1 on error */
782 exec_filter(const char *filter
, /* I - Filter to execute */
783 char **argv
, /* I - Argument list */
784 char **envp
, /* I - Environment list */
785 int infd
, /* I - Stdin file descriptor */
786 int outfd
) /* I - Stdout file descriptor */
788 int pid
, /* Process ID */
789 fd
; /* Temporary file descriptor */
790 #if defined(__APPLE__)
791 char processPath
[1024], /* CFProcessPath environment variable */
792 linkpath
[1024]; /* Link path for symlinks... */
793 int linkbytes
; /* Bytes for link path */
797 * Add special voodoo magic for MacOS X - this allows MacOS X
798 * programs to access their bundle resources properly...
801 if ((linkbytes
= readlink(filter
, linkpath
, sizeof(linkpath
) - 1)) > 0)
804 * Yes, this is a symlink to the actual program, nul-terminate and
808 linkpath
[linkbytes
] = '\0';
810 if (linkpath
[0] == '/')
811 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s",
814 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s/%s",
815 dirname((char *)filter
), linkpath
);
818 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s", filter
);
820 envp
[0] = processPath
; /* Replace <CFProcessPath> string */
821 #endif /* __APPLE__ */
823 if ((pid
= fork()) == 0)
826 * Child process goes here...
828 * Update stdin/stdout/stderr as needed...
834 infd
= open("/dev/null", O_RDONLY
);
846 outfd
= open("/dev/null", O_WRONLY
);
855 if ((fd
= open("/dev/null", O_RDWR
)) > 3)
860 fcntl(3, F_SETFL
, O_NDELAY
);
862 if ((fd
= open("/dev/null", O_RDWR
)) > 4)
867 fcntl(4, F_SETFL
, O_NDELAY
);
873 execve(filter
, argv
, envp
);
885 * 'exec_filters()' - Execute filters for the given file and options.
888 static int /* O - 0 on success, 1 on error */
889 exec_filters(mime_type_t
*srctype
, /* I - Source type */
890 cups_array_t
*filters
, /* I - Array of filters to run */
891 const char *infile
, /* I - File to filter */
892 const char *outfile
, /* I - File to create */
893 const char *ppdfile
, /* I - PPD file, if any */
894 const char *printer
, /* I - Printer name */
895 const char *user
, /* I - Username */
896 const char *title
, /* I - Job title */
897 int num_options
, /* I - Number of filter options */
898 cups_option_t
*options
) /* I - Filter options */
900 int i
; /* Looping var */
901 const char *argv
[8], /* Command-line arguments */
902 *envp
[17], /* Environment variables */
903 *temp
; /* Temporary string */
904 char *optstr
, /* Filter options */
905 content_type
[1024], /* CONTENT_TYPE */
906 cups_datadir
[1024], /* CUPS_DATADIR */
907 cups_fontpath
[1024], /* CUPS_FONTPATH */
908 cups_serverbin
[1024], /* CUPS_SERVERBIN */
909 cups_serverroot
[1024], /* CUPS_SERVERROOT */
910 final_content_type
[1024] = "",
911 /* FINAL_CONTENT_TYPE */
912 lang
[1024], /* LANG */
913 path
[1024], /* PATH */
915 printer_info
[255], /* PRINTER_INFO env variable */
916 printer_location
[255], /* PRINTER_LOCATION env variable */
917 printer_name
[255], /* PRINTER env variable */
918 rip_max_cache
[1024], /* RIP_MAX_CACHE */
919 userenv
[1024], /* USER */
920 program
[1024]; /* Program to run */
921 mime_filter_t
*filter
, /* Current filter */
922 *next
; /* Next filter */
923 int current
, /* Current filter */
924 filterfds
[2][2], /* Pipes for filters */
925 pid
, /* Process ID of filter */
926 status
, /* Exit status */
927 retval
; /* Return value */
928 cups_array_t
*pids
; /* Executed filters array */
929 mime_filter_t key
; /* Search key for filters */
930 cups_lang_t
*language
; /* Current language */
931 cups_dest_t
*dest
; /* Destination information */
935 * Figure out the final content type...
938 for (filter
= (mime_filter_t
*)cupsArrayLast(filters
);
939 filter
&& filter
->dst
;
940 filter
= (mime_filter_t
*)cupsArrayPrev(filters
))
941 if (strcmp(filter
->dst
->super
, "printer"))
944 if (filter
&& filter
->dst
)
946 const char *ptr
; /* Pointer in type name */
948 if ((ptr
= strchr(filter
->dst
->type
, '/')) != NULL
)
949 snprintf(final_content_type
, sizeof(final_content_type
),
950 "FINAL_CONTENT_TYPE=%s", ptr
+ 1);
952 snprintf(final_content_type
, sizeof(final_content_type
),
953 "FINAL_CONTENT_TYPE=%s/%s", filter
->dst
->super
,
958 * Remove NULL ("-") filters...
961 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
963 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
964 if (!strcmp(filter
->filter
, "-"))
965 cupsArrayRemove(filters
, filter
);
968 * Setup the filter environment and command-line...
971 optstr
= escape_options(num_options
, options
);
973 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
974 srctype
->super
, srctype
->type
);
975 snprintf(cups_datadir
, sizeof(cups_datadir
), "CUPS_DATADIR=%s", DataDir
);
976 snprintf(cups_fontpath
, sizeof(cups_fontpath
), "CUPS_FONTPATH=%s", FontPath
);
977 snprintf(cups_serverbin
, sizeof(cups_serverbin
), "CUPS_SERVERBIN=%s",
979 snprintf(cups_serverroot
, sizeof(cups_serverroot
), "CUPS_SERVERROOT=%s",
981 language
= cupsLangDefault();
982 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", language
->language
);
983 snprintf(path
, sizeof(path
), "PATH=%s", Path
);
985 snprintf(ppd
, sizeof(ppd
), "PPD=%s", ppdfile
);
986 else if ((temp
= getenv("PPD")) != NULL
)
987 snprintf(ppd
, sizeof(ppd
), "PPD=%s", temp
);
990 if (!access("/System/Library/Frameworks/ApplicationServices.framework/"
991 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
992 "Resources/English.lproj/Generic.ppd", 0))
993 strlcpy(ppd
, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
994 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
995 "Resources/English.lproj/Generic.ppd", sizeof(ppd
));
997 strlcpy(ppd
, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
998 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
999 "Resources/Generic.ppd", sizeof(ppd
));
1001 snprintf(ppd
, sizeof(ppd
), "PPD=%s/model/laserjet.ppd", DataDir
);
1002 #endif /* __APPLE__ */
1003 snprintf(rip_max_cache
, sizeof(rip_max_cache
), "RIP_MAX_CACHE=%s", RIPCache
);
1004 snprintf(userenv
, sizeof(userenv
), "USER=%s", user
);
1007 (dest
= cupsGetNamedDest(CUPS_HTTP_DEFAULT
, printer
, NULL
)) != NULL
)
1009 if ((temp
= cupsGetOption("printer-info", dest
->num_options
,
1010 dest
->options
)) != NULL
)
1011 snprintf(printer_info
, sizeof(printer_info
), "PRINTER_INFO=%s", temp
);
1013 snprintf(printer_info
, sizeof(printer_info
), "PRINTER_INFO=%s", printer
);
1015 if ((temp
= cupsGetOption("printer-location", dest
->num_options
,
1016 dest
->options
)) != NULL
)
1017 snprintf(printer_location
, sizeof(printer_location
),
1018 "PRINTER_LOCATION=%s", temp
);
1020 strlcpy(printer_location
, "PRINTER_LOCATION=Unknown",
1021 sizeof(printer_location
));
1025 snprintf(printer_info
, sizeof(printer_info
), "PRINTER_INFO=%s",
1026 printer
? printer
: "Unknown");
1027 strlcpy(printer_location
, "PRINTER_LOCATION=Unknown",
1028 sizeof(printer_location
));
1031 snprintf(printer_name
, sizeof(printer_name
), "PRINTER=%s",
1032 printer
? printer
: "Unknown");
1034 argv
[0] = (char *)printer
;
1038 argv
[4] = cupsGetOption("copies", num_options
, options
);
1046 envp
[0] = "<CFProcessPath>";
1047 envp
[1] = content_type
;
1048 envp
[2] = cups_datadir
;
1049 envp
[3] = cups_fontpath
;
1050 envp
[4] = cups_serverbin
;
1051 envp
[5] = cups_serverroot
;
1055 envp
[9] = printer_info
;
1056 envp
[10] = printer_location
;
1057 envp
[11] = printer_name
;
1058 envp
[12] = rip_max_cache
;
1060 envp
[14] = "CHARSET=utf-8";
1061 if (final_content_type
[0])
1063 envp
[15] = final_content_type
;
1069 for (i
= 0; argv
[i
]; i
++)
1070 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
1072 for (i
= 0; envp
[i
]; i
++)
1073 fprintf(stderr
, "DEBUG: envp[%d]=\"%s\"\n", i
, envp
[i
]);
1076 * Execute all of the filters...
1079 pids
= cupsArrayNew((cups_array_func_t
)compare_pids
, NULL
);
1081 filterfds
[0][0] = -1;
1082 filterfds
[0][1] = -1;
1083 filterfds
[1][0] = -1;
1084 filterfds
[1][1] = -1;
1087 filterfds
[0][0] = 0;
1089 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
1091 filter
= next
, current
= 1 - current
)
1093 next
= (mime_filter_t
*)cupsArrayNext(filters
);
1095 if (filter
->filter
[0] == '/')
1096 strlcpy(program
, filter
->filter
, sizeof(program
));
1098 snprintf(program
, sizeof(program
), "%s/filter/%s", ServerBin
,
1101 if (filterfds
[!current
][1] > 1)
1103 close(filterfds
[1 - current
][0]);
1104 close(filterfds
[1 - current
][1]);
1106 filterfds
[1 - current
][0] = -1;
1107 filterfds
[1 - current
][0] = -1;
1111 open_pipe(filterfds
[1 - current
]);
1114 filterfds
[1 - current
][1] = open(outfile
, O_CREAT
| O_TRUNC
| O_WRONLY
,
1117 if (filterfds
[1 - current
][1] < 0)
1118 fprintf(stderr
, "ERROR: Unable to create \"%s\" - %s\n", outfile
,
1122 filterfds
[1 - current
][1] = 1;
1124 pid
= exec_filter(program
, (char **)argv
, (char **)envp
,
1125 filterfds
[current
][0], filterfds
[1 - current
][1]);
1129 fprintf(stderr
, "INFO: %s (PID %d) started.\n", filter
->filter
, pid
);
1132 cupsArrayAdd(pids
, filter
);
1141 * Close remaining pipes...
1144 if (filterfds
[0][1] > 1)
1146 close(filterfds
[0][0]);
1147 close(filterfds
[0][1]);
1150 if (filterfds
[1][1] > 1)
1152 close(filterfds
[1][0]);
1153 close(filterfds
[1][1]);
1157 * Wait for the children to exit...
1162 while (cupsArrayCount(pids
) > 0)
1164 if ((pid
= wait(&status
)) < 0)
1168 if ((filter
= (mime_filter_t
*)cupsArrayFind(pids
, &key
)) != NULL
)
1170 cupsArrayRemove(pids
, filter
);
1174 if (WIFEXITED(status
))
1175 fprintf(stderr
, "ERROR: %s (PID %d) stopped with status %d\n",
1176 filter
->filter
, pid
, WEXITSTATUS(status
));
1178 fprintf(stderr
, "ERROR: %s (PID %d) crashed on signal %d\n",
1179 filter
->filter
, pid
, WTERMSIG(status
));
1184 fprintf(stderr
, "INFO: %s (PID %d) exited with no errors.\n",
1185 filter
->filter
, pid
);
1189 cupsArrayDelete(pids
);
1196 * 'get_job_file()' - Get the specified job file.
1200 get_job_file(const char *job
) /* I - Job ID */
1202 long jobid
, /* Job ID */
1203 docnum
; /* Document number */
1204 const char *jobptr
; /* Pointer into job ID string */
1205 char uri
[1024]; /* job-uri */
1206 http_t
*http
; /* Connection to server */
1207 ipp_t
*request
; /* Request data */
1208 int tempfd
; /* Temporary file */
1212 * Get the job ID and document number, if any...
1215 if ((jobptr
= strrchr(job
, '-')) != NULL
)
1220 jobid
= strtol(jobptr
, (char **)&jobptr
, 10);
1223 docnum
= strtol(jobptr
+ 1, NULL
, 10);
1227 if (jobid
< 1 || jobid
> INT_MAX
)
1229 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid job ID %d."), (int)jobid
);
1233 if (docnum
< 1 || docnum
> INT_MAX
)
1235 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid document number %d."),
1241 * Ask the server for the document file...
1244 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1245 cupsEncryption())) == NULL
)
1247 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server."),
1252 request
= ippNewRequest(CUPS_GET_DOCUMENT
);
1254 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", (int)jobid
);
1256 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1257 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "document-number",
1260 if ((tempfd
= cupsTempFd(TempFile
, sizeof(TempFile
))) == -1)
1262 _cupsLangPrintError("ERROR", _("Unable to create temporary file"));
1267 signal(SIGTERM
, sighandler
);
1269 ippDelete(cupsDoIORequest(http
, request
, "/", -1, tempfd
));
1275 if (cupsLastError() != IPP_OK
)
1277 _cupsLangPrintf(stderr
, _("cupsfilter: Unable to get job file - %s"),
1278 cupsLastErrorString());
1286 * 'open_pipe()' - Create a pipe which is closed on exec.
1289 static int /* O - 0 on success, -1 on error */
1290 open_pipe(int *fds
) /* O - Pipe file descriptors (2) */
1293 * Create the pipe...
1305 * Set the "close on exec" flag on each end of the pipe...
1308 if (fcntl(fds
[0], F_SETFD
, fcntl(fds
[0], F_GETFD
) | FD_CLOEXEC
))
1319 if (fcntl(fds
[1], F_SETFD
, fcntl(fds
[1], F_GETFD
) | FD_CLOEXEC
))
1331 * Return 0 indicating success...
1339 * 'read_cups_files_conf()' - Read the cups-files.conf file to get the filter settings.
1342 static int /* O - 0 on success, 1 on error */
1343 read_cups_files_conf(
1344 const char *filename
) /* I - File to read */
1346 cups_file_t
*fp
; /* cups-files.conf file */
1347 const char *temp
; /* Temporary string */
1348 char line
[1024], /* Line from file */
1349 *ptr
; /* Pointer into line */
1350 int linenum
; /* Current line number */
1353 if ((temp
= getenv("CUPS_DATADIR")) != NULL
)
1354 set_string(&DataDir
, temp
);
1356 set_string(&DataDir
, CUPS_DATADIR
);
1358 if ((temp
= getenv("CUPS_FONTPATH")) != NULL
)
1359 set_string(&FontPath
, temp
);
1361 set_string(&FontPath
, CUPS_FONTPATH
);
1363 set_string(&RIPCache
, "128m");
1365 if ((temp
= getenv("CUPS_SERVERBIN")) != NULL
)
1366 set_string(&ServerBin
, temp
);
1368 set_string(&ServerBin
, CUPS_SERVERBIN
);
1370 strlcpy(line
, filename
, sizeof(line
));
1371 if ((ptr
= strrchr(line
, '/')) != NULL
)
1374 getcwd(line
, sizeof(line
));
1376 set_string(&ServerRoot
, line
);
1378 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1382 while (cupsFileGetConf(fp
, line
, sizeof(line
), &ptr
, &linenum
))
1384 if (!_cups_strcasecmp(line
, "DataDir"))
1385 set_string(&DataDir
, ptr
);
1386 else if (!_cups_strcasecmp(line
, "FontPath"))
1387 set_string(&FontPath
, ptr
);
1388 else if (!_cups_strcasecmp(line
, "RIPCache"))
1389 set_string(&RIPCache
, ptr
);
1390 else if (!_cups_strcasecmp(line
, "ServerBin"))
1391 set_string(&ServerBin
, ptr
);
1392 else if (!_cups_strcasecmp(line
, "ServerRoot"))
1393 set_string(&ServerRoot
, ptr
);
1399 snprintf(line
, sizeof(line
),
1400 "%s/filter:" CUPS_BINDIR
":" CUPS_SBINDIR
":/bin:/usr/bin",
1402 set_string(&Path
, line
);
1409 * 'set_string()' - Copy and set a string.
1413 set_string(char **s
, /* O - Copy of string */
1414 const char *val
) /* I - String to copy */
1424 * 'sighandler()' - Signal catcher for when we print from stdin...
1428 sighandler(int s
) /* I - Signal number */
1431 * Remove the temporary file we're using to print a job file...
1446 * 'usage()' - Show program usage...
1450 usage(const char *opt
) /* I - Incorrect option, if any */
1453 _cupsLangPrintf(stderr
, _("%s: Unknown option \"%c\"."), "cupsfilter",
1456 _cupsLangPuts(stdout
, _("Usage: cupsfilter [ options ] filename"));
1457 _cupsLangPuts(stdout
, _("Options:"));
1458 _cupsLangPuts(stdout
, _(" -D Remove the input file "
1460 _cupsLangPuts(stdout
, _(" -P filename.ppd Set PPD file."));
1461 _cupsLangPuts(stdout
, _(" -U username Specify username."));
1462 _cupsLangPuts(stdout
, _(" -c cups-files.conf Set cups-files.conf file to "
1464 _cupsLangPuts(stdout
, _(" -d printer Use the named "
1466 _cupsLangPuts(stdout
, _(" -e Use every filter from "
1468 _cupsLangPuts(stdout
, _(" -i mime/type Set input MIME type "
1469 "(otherwise auto-typed)."));
1470 _cupsLangPuts(stdout
, _(" -j job-id[,N] Filter file N from the "
1471 "specified job (default is file 1)."));
1472 _cupsLangPuts(stdout
, _(" -m mime/type Set output MIME type "
1473 "(otherwise application/pdf)."));
1474 _cupsLangPuts(stdout
, _(" -n copies Set number of copies."));
1475 _cupsLangPuts(stdout
, _(" -o name=value Set option(s)."));
1476 _cupsLangPuts(stdout
, _(" -p filename.ppd Set PPD file."));
1477 _cupsLangPuts(stdout
, _(" -t title Set title."));
1478 _cupsLangPuts(stdout
, _(" -u Remove the PPD file "
1486 * End of "$Id: cupsfilter.c 11772 2014-03-28 15:08:30Z msweet $".