]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cupsfilter.c
4 * CUPS filtering program for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 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 * 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.
22 * get_job_file() - Get the specified job file.
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...
30 * Include necessary headers...
33 #include <cups/cups.h>
34 #include <cups/i18n.h>
35 #include <cups/string.h>
44 #if defined(__APPLE__)
46 #endif /* __APPLE__ */
53 static char *DataDir
= NULL
;/* CUPS_DATADIR environment variable */
54 static char *FontPath
= NULL
;
55 /* CUPS_FONTPATH environment variable */
56 static mime_filter_t GZIPFilter
= /* gziptoany filter */
58 NULL
, /* Source type */
59 NULL
, /* Destination type */
61 "gziptoany" /* Filter program to run */
63 static char *Path
= NULL
; /* PATH environment variable */
64 static char *ServerBin
= NULL
;
65 /* CUPS_SERVERBIN environment variable */
66 static char *ServerRoot
= NULL
;
67 /* CUPS_SERVERROOT environment variable */
68 static char *RIPCache
= NULL
;
69 /* RIP_CACHE environment variable */
70 static char TempFile
[1024] = "";
78 static void add_printer_filter(const char *command
, mime_t
*mime
,
79 mime_type_t
*printer_type
,
81 static mime_type_t
*add_printer_filters(const char *command
,
82 mime_t
*mime
, const char *printer
,
84 mime_type_t
**prefilter_type
);
85 static int compare_pids(mime_filter_t
*a
, mime_filter_t
*b
);
86 static char *escape_options(int num_options
, cups_option_t
*options
);
87 static int exec_filter(const char *filter
, char **argv
,
88 char **envp
, int infd
, int outfd
);
89 static int exec_filters(mime_type_t
*srctype
,
90 cups_array_t
*filters
, const char *infile
,
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
);
95 static void get_job_file(const char *job
);
96 static int open_pipe(int *fds
);
97 static int read_cupsd_conf(const char *filename
);
98 static void set_string(char **s
, const char *val
);
99 static void sighandler(int sig
);
100 static void usage(const char *command
, const char *opt
);
104 * 'main()' - Main entry for the test program.
107 int /* O - Exit status */
108 main(int argc
, /* I - Number of command-line args */
109 char *argv
[]) /* I - Command-line arguments */
111 int i
; /* Looping vars */
112 const char *command
, /* Command name */
113 *opt
, /* Current option */
114 *printer
; /* Printer name */
115 mime_type_t
*printer_type
, /* Printer MIME type */
116 *prefilter_type
; /* Printer prefilter MIME type */
117 char *srctype
, /* Source type */
118 *dsttype
, /* Destination type */
119 super
[MIME_MAX_SUPER
], /* Super-type name */
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 */
124 char mimedir
[1024]; /* MIME directory */
125 char *infile
, /* File to filter */
126 *outfile
; /* File to create */
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 */
135 const char *title
, /* Title string */
136 *user
; /* Username */
137 int all_filters
, /* Use all filters */
138 removeppd
, /* Remove PPD file */
139 removeinfile
; /* Remove input file */
140 int status
; /* Execution status */
147 if ((command
= strrchr(argv
[0], '/')) != NULL
)
152 printer
= !strcmp(command
, "convert") ? "tofile" : "cupsfilter";
156 dsttype
= "application/pdf";
168 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
169 server_root
= CUPS_SERVERROOT
;
171 snprintf(cupsdconf
, sizeof(cupsdconf
), "%s/cupsd.conf", server_root
);
174 * Process command-line arguments...
177 _cupsSetLocale(argv
);
179 for (i
= 1; i
< argc
; i
++)
180 if (argv
[i
][0] == '-')
182 for (opt
= argv
[i
] + 1; *opt
; opt
++)
185 case '-' : /* Next argument is a filename... */
187 if (i
< argc
&& !infile
)
193 case 'a' : /* Specify option... */
196 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
201 case 'c' : /* Specify cupsd.conf file location... */
205 if (!strcmp(command
, "convert"))
206 num_options
= cupsAddOption("copies", argv
[i
], num_options
,
209 strlcpy(cupsdconf
, argv
[i
], sizeof(cupsdconf
));
215 case 'D' : /* Delete input file after conversion */
219 case 'e' : /* Use every filter from the PPD file */
223 case 'f' : /* Specify input file... */
225 if (i
< argc
&& !infile
)
231 case 'i' : /* Specify source MIME type... */
235 if (sscanf(argv
[i
], "%15[^/]/%255s", super
, type
) != 2)
244 case 'j' : /* Get job file or specify destination MIME type... */
245 if (strcmp(command
, "convert"))
250 get_job_file(argv
[i
]);
259 case 'm' : /* Specify destination MIME type... */
263 if (sscanf(argv
[i
], "%15[^/]/%255s", super
, type
) != 2)
272 case 'n' : /* Specify number of copies... */
275 num_options
= cupsAddOption("copies", argv
[i
], num_options
,
281 case 'o' : /* Specify option(s) or output filename */
285 if (!strcmp(command
, "convert"))
288 usage(command
, NULL
);
293 num_options
= cupsParseOptions(argv
[i
], num_options
,
300 case 'p' : /* Specify PPD file... */
301 case 'P' : /* Specify PPD file... */
309 case 't' : /* Specify title... */
310 case 'J' : /* Specify title... */
318 case 'u' : /* Delete PPD file after conversion */
322 case 'U' : /* Specify username... */
330 default : /* Something we don't understand... */
337 if (strcmp(command
, "convert"))
341 _cupsLangPuts(stderr
,
342 _("convert: Use the -f option to specify a file to "
344 usage(command
, NULL
);
349 _cupsLangPuts(stderr
,
350 _("cupsfilter: Only one filename can be specified\n"));
351 usage(command
, NULL
);
354 if (!infile
&& !srctype
)
355 usage(command
, NULL
);
361 else if ((title
= strrchr(infile
, '/')) != NULL
)
368 * Load the cupsd.conf file and create the MIME database...
371 if (read_cupsd_conf(cupsdconf
))
374 snprintf(mimedir
, sizeof(mimedir
), "%s/mime", DataDir
);
376 mime
= mimeLoadTypes(NULL
, mimedir
);
377 mime
= mimeLoadTypes(mime
, ServerRoot
);
378 mime
= mimeLoadFilters(mime
, mimedir
, Path
);
379 mime
= mimeLoadFilters(mime
, ServerRoot
, Path
);
383 _cupsLangPrintf(stderr
,
384 _("%s: Unable to read MIME database from \"%s\" or "
386 command
, mimedir
, ServerRoot
);
392 printer_type
= add_printer_filters(command
, mime
, printer
, ppdfile
,
397 printer_type
= mimeType(mime
, "application", "vnd.cups-postscript");
398 prefilter_type
= NULL
;
402 * Get the source and destination types...
407 sscanf(srctype
, "%15[^/]/%255s", super
, type
);
408 if ((src
= mimeType(mime
, super
, type
)) == NULL
)
410 _cupsLangPrintf(stderr
,
411 _("%s: Unknown source MIME type %s/%s\n"),
412 command
, super
, type
);
416 else if ((src
= mimeFileType(mime
, infile
, infile
, &compression
)) == NULL
)
418 _cupsLangPrintf(stderr
,
419 _("%s: Unable to determine MIME type of \"%s\"\n"),
424 sscanf(dsttype
, "%15[^/]/%255s", super
, type
);
425 if (!strcasecmp(super
, "printer"))
427 else if ((dst
= mimeType(mime
, super
, type
)) == NULL
)
429 _cupsLangPrintf(stderr
,
430 _("%s: Unknown destination MIME type %s/%s\n"),
431 command
, super
, type
);
436 * Figure out how to filter the file...
442 * Special case - no filtering needed...
445 filters
= cupsArrayNew(NULL
, NULL
);
446 cupsArrayAdd(filters
, &GZIPFilter
);
447 GZIPFilter
.src
= src
;
448 GZIPFilter
.dst
= dst
;
450 else if ((filters
= mimeFilter(mime
, src
, dst
, &cost
)) == NULL
)
452 _cupsLangPrintf(stderr
,
453 _("%s: No filter to convert from %s/%s to %s/%s\n"),
454 command
, src
->super
, src
->type
, dst
->super
, dst
->type
);
457 else if (compression
)
458 cupsArrayInsert(filters
, &GZIPFilter
);
466 mime_filter_t
*filter
, /* Current filter */
467 *prefilter
; /* Current pre-filter */
468 cups_array_t
*prefilters
= cupsArrayNew(NULL
, NULL
);
469 /* New filters array */
472 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
474 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
476 if ((prefilter
= mimeFilterLookup(mime
, filter
->src
,
477 prefilter_type
)) != NULL
)
478 cupsArrayAdd(prefilters
, prefilter
);
480 cupsArrayAdd(prefilters
, filter
);
483 cupsArrayDelete(filters
);
484 filters
= prefilters
;
491 status
= exec_filters(src
, filters
, infile
, outfile
, ppdfile
, printer
, user
,
492 title
, num_options
, options
);
495 * Remove files as needed, then exit...
501 if (removeppd
&& ppdfile
)
504 if (removeinfile
&& infile
)
512 * 'add_printer_filter()' - Add a single filters from a PPD file.
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 */
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 */
531 * Parse the filter string; it should be in the following format:
533 * super/type cost program
536 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
539 _cupsLangPrintf(stderr
, _("%s: Invalid filter string \"%s\"\n"), command
,
545 * See if the filter program exists; if not, stop the printer and flag
549 if (strcmp(program
, "-"))
551 if (program
[0] == '/')
552 strlcpy(filename
, program
, sizeof(filename
));
554 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
556 if (access(filename
, X_OK
))
558 _cupsLangPrintf(stderr
, _("%s: Filter \"%s\" not available: %s\n"),
559 command
, program
, strerror(errno
));
565 * Add the filter to the MIME database, supporting wildcards as needed...
568 for (temptype
= mimeFirstType(mime
);
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
);
579 * 'add_printer_filters()' - Add filters from a PPD file.
582 static mime_type_t
* /* O - Printer type or NULL on error */
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 */
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 */
596 *prefilter_type
= NULL
;
598 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
600 ppd_status_t status
; /* PPD load status */
602 status
= ppdLastError(&i
);
603 _cupsLangPrintf(stderr
, _("%s: Unable to open PPD file: %s on line %d\n"),
604 command
, ppdErrorString(status
), i
);
608 printer_type
= mimeAddType(mime
, "printer", printer
);
610 if (ppd
->num_filters
> 0)
612 for (i
= 0; i
< ppd
->num_filters
; i
++)
613 add_printer_filter(command
, mime
, printer_type
, ppd
->filters
[i
]);
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 -");
623 if ((ppdattr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
625 *prefilter_type
= mimeAddType(mime
, "prefilter", printer
);
627 for (; ppdattr
; ppdattr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
629 add_printer_filter(command
, mime
, *prefilter_type
, ppdattr
->value
);
632 *prefilter_type
= NULL
;
634 return (printer_type
);
639 * 'compare_pids()' - Compare two filter PIDs...
642 static int /* O - Result of comparison */
643 compare_pids(mime_filter_t
*a
, /* I - First filter */
644 mime_filter_t
*b
) /* I - Second filter */
647 * Because we're particularly lazy, we store the process ID in the "cost"
651 return (a
->cost
- b
->cost
);
656 * 'escape_options()' - Convert an options array to a string.
659 static char * /* O - Option string */
661 int num_options
, /* I - Number of options */
662 cups_option_t
*options
) /* I - Options */
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 */
673 * Figure out the worst-case number of bytes we need for the option string.
676 for (i
= num_options
, option
= options
, bytes
= 1; i
> 0; i
--, option
++)
677 bytes
+= 2 * (strlen(option
->name
) + strlen(option
->value
)) + 2;
679 if ((s
= malloc(bytes
)) == NULL
)
683 * Copy the options to the string...
686 for (i
= num_options
, option
= options
, sptr
= s
; i
> 0; i
--, option
++)
688 if (!strcmp(option
->name
, "copies"))
694 strcpy(sptr
, option
->name
);
695 sptr
+= strlen(sptr
);
698 for (vptr
= option
->value
; *vptr
;)
700 if (strchr("\\ \t\n", *vptr
))
714 * 'exec_filter()' - Execute a single filter.
717 static int /* O - Process ID or -1 on error */
718 exec_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 */
724 int pid
, /* Process ID */
725 fd
; /* Temporary file descriptor */
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 */
733 * Add special voodoo magic for MacOS X - this allows MacOS X
734 * programs to access their bundle resources properly...
737 if ((linkbytes
= readlink(filter
, linkpath
, sizeof(linkpath
) - 1)) > 0)
740 * Yes, this is a symlink to the actual program, nul-terminate and
744 linkpath
[linkbytes
] = '\0';
746 if (linkpath
[0] == '/')
747 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s",
750 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s/%s",
751 dirname((char *)filter
), linkpath
);
754 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s", filter
);
756 envp
[0] = processPath
; /* Replace <CFProcessPath> string */
757 #endif /* __APPLE__ */
759 if ((pid
= fork()) == 0)
762 * Child process goes here...
764 * Update stdin/stdout/stderr as needed...
770 infd
= open("/dev/null", O_RDONLY
);
782 outfd
= open("/dev/null", O_WRONLY
);
791 if ((fd
= open("/dev/null", O_RDWR
)) > 3)
796 fcntl(3, F_SETFL
, O_NDELAY
);
798 if ((fd
= open("/dev/null", O_RDWR
)) > 4)
803 fcntl(4, F_SETFL
, O_NDELAY
);
809 execve(filter
, argv
, envp
);
821 * 'exec_filters()' - Execute filters for the given file and options.
824 static int /* O - 0 on success, 1 on error */
825 exec_filters(mime_type_t
*srctype
, /* I - Source type */
826 cups_array_t
*filters
, /* I - Array of filters to run */
827 const char *infile
, /* I - File to filter */
828 const char *outfile
, /* I - File to create */
829 const char *ppdfile
, /* I - PPD file, if any */
830 const char *printer
, /* I - Printer name */
831 const char *user
, /* I - Username */
832 const char *title
, /* I - Job title */
833 int num_options
, /* I - Number of filter options */
834 cups_option_t
*options
) /* I - Filter options */
836 int i
; /* Looping var */
837 const char *argv
[8], /* Command-line arguments */
838 *envp
[12], /* Environment variables */
839 *temp
; /* Temporary string */
840 char *optstr
, /* Filter options */
841 content_type
[1024], /* CONTENT_TYPE */
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 */
849 rip_cache
[1024], /* RIP_CACHE */
850 userenv
[1024], /* USER */
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 */
861 cups_lang_t
*language
; /* Current language */
865 * Setup the filter environment and command-line...
868 optstr
= escape_options(num_options
, options
);
870 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
871 srctype
->super
, srctype
->type
);
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",
876 snprintf(cups_serverroot
, sizeof(cups_serverroot
), "CUPS_SERVERROOT=%s",
878 language
= cupsLangDefault();
879 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", language
->language
);
880 snprintf(path
, sizeof(path
), "PATH=%s", Path
);
882 snprintf(ppd
, sizeof(ppd
), "PPD=%s", ppdfile
);
883 else if ((temp
= getenv("PPD")) != NULL
)
884 snprintf(ppd
, sizeof(ppd
), "PPD=%s", temp
);
887 if (!access("/System/Library/Frameworks/ApplicationServices.framework/"
888 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
889 "Resources/English.lproj/Generic.ppd", 0))
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
));
894 strlcpy(ppd
, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
895 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
896 "Resources/Generic.ppd", sizeof(ppd
));
898 snprintf(ppd
, sizeof(ppd
), "PPD=%s/model/laserjet.ppd", DataDir
);
899 #endif /* __APPLE__ */
900 snprintf(rip_cache
, sizeof(rip_cache
), "RIP_CACHE=%s", RIPCache
);
901 snprintf(userenv
, sizeof(userenv
), "USER=%s", user
);
903 argv
[0] = (char *)printer
;
907 argv
[4] = cupsGetOption("copies", num_options
, options
);
915 envp
[0] = "<CFProcessPath>";
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
;
928 for (i
= 0; argv
[i
]; i
++)
929 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
931 for (i
= 0; envp
[i
]; i
++)
932 fprintf(stderr
, "DEBUG: envp[%d]=\"%s\"\n", i
, envp
[i
]);
935 * Execute all of the filters...
938 pids
= cupsArrayNew((cups_array_func_t
)compare_pids
, NULL
);
940 filterfds
[0][0] = -1;
941 filterfds
[0][1] = -1;
942 filterfds
[1][0] = -1;
943 filterfds
[1][1] = -1;
948 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
950 filter
= next
, current
= 1 - current
)
952 next
= (mime_filter_t
*)cupsArrayNext(filters
);
954 if (filter
->filter
[0] == '/')
955 strlcpy(program
, filter
->filter
, sizeof(program
));
957 snprintf(program
, sizeof(program
), "%s/filter/%s", ServerBin
,
960 if (filterfds
[!current
][1] > 1)
962 close(filterfds
[1 - current
][0]);
963 close(filterfds
[1 - current
][1]);
965 filterfds
[1 - current
][0] = -1;
966 filterfds
[1 - current
][0] = -1;
970 open_pipe(filterfds
[1 - current
]);
973 filterfds
[1 - current
][1] = open(outfile
, O_CREAT
| O_TRUNC
| O_WRONLY
,
976 if (filterfds
[1 - current
][1] < 0)
977 fprintf(stderr
, "ERROR: Unable to create \"%s\" - %s\n", outfile
,
981 filterfds
[1 - current
][1] = 1;
983 pid
= exec_filter(program
, (char **)argv
, (char **)envp
,
984 filterfds
[current
][0], filterfds
[1 - current
][1]);
988 fprintf(stderr
, "INFO: %s (PID %d) started.\n", filter
->filter
, pid
);
991 cupsArrayAdd(pids
, filter
);
1000 * Close remaining pipes...
1003 if (filterfds
[0][1] > 1)
1005 close(filterfds
[0][0]);
1006 close(filterfds
[0][1]);
1009 if (filterfds
[1][1] > 1)
1011 close(filterfds
[1][0]);
1012 close(filterfds
[1][1]);
1016 * Wait for the children to exit...
1021 while (cupsArrayCount(pids
) > 0)
1023 if ((pid
= wait(&status
)) < 0)
1027 if ((filter
= (mime_filter_t
*)cupsArrayFind(pids
, &key
)) != NULL
)
1029 cupsArrayRemove(pids
, filter
);
1033 if (WIFEXITED(status
))
1034 fprintf(stderr
, "ERROR: %s (PID %d) stopped with status %d\n",
1035 filter
->filter
, pid
, WEXITSTATUS(status
));
1037 fprintf(stderr
, "ERROR: %s (PID %d) crashed on signal %d\n",
1038 filter
->filter
, pid
, WTERMSIG(status
));
1043 fprintf(stderr
, "INFO: %s (PID %d) exited with no errors.\n",
1044 filter
->filter
, pid
);
1048 cupsArrayDelete(pids
);
1055 * 'get_job_file()' - Get the specified job file.
1059 get_job_file(const char *job
) /* I - Job ID */
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 */
1071 * Get the job ID and document number, if any...
1074 if ((jobptr
= strrchr(job
, '-')) != NULL
)
1079 jobid
= strtol(jobptr
, (char **)&jobptr
, 10);
1082 docnum
= strtol(jobptr
+ 1, NULL
, 10);
1086 if (jobid
< 1 || jobid
> INT_MAX
)
1088 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid job ID %d\n"), (int)jobid
);
1092 if (docnum
< 1 || docnum
> INT_MAX
)
1094 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid document number %d\n"),
1100 * Ask the server for the document file...
1103 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1104 cupsEncryption())) == NULL
)
1106 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server\n"),
1111 request
= ippNewRequest(CUPS_GET_DOCUMENT
);
1113 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", (int)jobid
);
1115 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1116 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "document-number",
1119 if ((tempfd
= cupsTempFd(TempFile
, sizeof(TempFile
))) == -1)
1121 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
1126 signal(SIGTERM
, sighandler
);
1128 ippDelete(cupsDoIORequest(http
, request
, "/", -1, tempfd
));
1134 if (cupsLastError() != IPP_OK
)
1136 _cupsLangPrintf(stderr
, _("cupsfilter: Unable to get job file - %s\n"),
1137 cupsLastErrorString());
1145 * 'open_pipe()' - Create a pipe which is closed on exec.
1148 static int /* O - 0 on success, -1 on error */
1149 open_pipe(int *fds
) /* O - Pipe file descriptors (2) */
1152 * Create the pipe...
1164 * Set the "close on exec" flag on each end of the pipe...
1167 if (fcntl(fds
[0], F_SETFD
, fcntl(fds
[0], F_GETFD
) | FD_CLOEXEC
))
1178 if (fcntl(fds
[1], F_SETFD
, fcntl(fds
[1], F_GETFD
) | FD_CLOEXEC
))
1190 * Return 0 indicating success...
1198 * 'read_cupsd_conf()' - Read the cupsd.conf file to get the filter settings.
1201 static int /* O - 0 on success, 1 on error */
1202 read_cupsd_conf(const char *filename
) /* I - File to read */
1204 cups_file_t
*fp
; /* cupsd.conf file */
1205 const char *temp
; /* Temporary string */
1206 char line
[1024], /* Line from file */
1207 *ptr
; /* Pointer into line */
1208 int linenum
; /* Current line number */
1211 if ((temp
= getenv("CUPS_DATADIR")) != NULL
)
1212 set_string(&DataDir
, temp
);
1214 set_string(&DataDir
, CUPS_DATADIR
);
1216 if ((temp
= getenv("CUPS_FONTPATH")) != NULL
)
1217 set_string(&FontPath
, temp
);
1219 set_string(&FontPath
, CUPS_FONTPATH
);
1221 set_string(&RIPCache
, "8m");
1223 if ((temp
= getenv("CUPS_SERVERBIN")) != NULL
)
1224 set_string(&ServerBin
, temp
);
1226 set_string(&ServerBin
, CUPS_SERVERBIN
);
1228 strlcpy(line
, filename
, sizeof(line
));
1229 if ((ptr
= strrchr(line
, '/')) != NULL
)
1232 getcwd(line
, sizeof(line
));
1234 set_string(&ServerRoot
, line
);
1236 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1240 while (cupsFileGetConf(fp
, line
, sizeof(line
), &ptr
, &linenum
))
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
);
1257 snprintf(line
, sizeof(line
),
1258 "%s/filter:" CUPS_BINDIR
":" CUPS_SBINDIR
":/bin:/usr/bin",
1260 set_string(&Path
, line
);
1267 * 'set_string()' - Copy and set a string.
1271 set_string(char **s
, /* O - Copy of string */
1272 const char *val
) /* I - String to copy */
1282 * 'sighandler()' - Signal catcher for when we print from stdin...
1286 sighandler(int s
) /* I - Signal number */
1289 * Remove the temporary file we're using to print a job file...
1304 * 'usage()' - Show program usage...
1308 usage(const char *command
, /* I - Command name */
1309 const char *opt
) /* I - Incorrect option, if any */
1312 _cupsLangPrintf(stderr
, _("%s: Unknown option '%c'\n"), command
, *opt
);
1314 if (!strcmp(command
, "cupsfilter"))
1315 _cupsLangPuts(stdout
,
1316 _("Usage: cupsfilter -m mime/type [ options ] filename\n"
1320 " -c cupsd.conf Set cupsd.conf file to use\n"
1321 " -e Use every filter from the PPD file\n"
1322 " -j job-id[,N] Filter file N from the specified job (default is file 1)\n"
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"));
1328 _cupsLangPuts(stdout
,
1329 _("Usage: convert [ options ]\n"
1333 " -e Use every filter from the PPD file\n"
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"));