]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cupsfilter.c
2 * "$Id: cupsfilter.c 7952 2008-09-17 00:56:20Z mike $"
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(cups_array_t
*filters
, const char *infile
,
90 const char *outfile
, const char *ppdfile
,
91 const char *printer
, const char *user
,
92 const char *title
, int num_options
,
93 cups_option_t
*options
);
94 static void get_job_file(const char *job
);
95 static int open_pipe(int *fds
);
96 static int read_cupsd_conf(const char *filename
);
97 static void set_string(char **s
, const char *val
);
98 static void sighandler(int sig
);
99 static void usage(const char *command
, const char *opt
);
103 * 'main()' - Main entry for the test program.
106 int /* O - Exit status */
107 main(int argc
, /* I - Number of command-line args */
108 char *argv
[]) /* I - Command-line arguments */
110 int i
; /* Looping vars */
111 const char *command
, /* Command name */
112 *opt
, /* Current option */
113 *printer
; /* Printer name */
114 mime_type_t
*printer_type
, /* Printer MIME type */
115 *prefilter_type
; /* Printer prefilter MIME type */
116 char *srctype
, /* Source type */
117 *dsttype
, /* Destination type */
118 super
[MIME_MAX_SUPER
], /* Super-type name */
119 type
[MIME_MAX_TYPE
]; /* Type name */
120 int compression
; /* Compression of file */
121 int cost
; /* Cost of filters */
122 mime_t
*mime
; /* MIME database */
123 char mimedir
[1024]; /* MIME directory */
124 char *infile
, /* File to filter */
125 *outfile
; /* File to create */
126 char cupsdconf
[1024]; /* cupsd.conf file */
127 const char *server_root
; /* CUPS_SERVERROOT environment variable */
128 mime_type_t
*src
, /* Source type */
129 *dst
; /* Destination type */
130 cups_array_t
*filters
; /* Filters for the file */
131 int num_options
; /* Number of options */
132 cups_option_t
*options
; /* Options */
133 const char *ppdfile
; /* PPD file */
134 const char *title
, /* Title string */
135 *user
; /* Username */
136 int removeppd
, /* Remove PPD file */
137 removeinfile
; /* Remove input file */
138 int status
; /* Execution status */
145 if ((command
= strrchr(argv
[0], '/')) != NULL
)
150 printer
= !strcmp(command
, "convert") ? "tofile" : "cupsfilter";
154 dsttype
= "application/pdf";
165 if ((server_root
= getenv("CUPS_SERVERROOT")) == NULL
)
166 server_root
= CUPS_SERVERROOT
;
168 snprintf(cupsdconf
, sizeof(cupsdconf
), "%s/cupsd.conf", server_root
);
171 * Process command-line arguments...
174 _cupsSetLocale(argv
);
176 for (i
= 1; i
< argc
; i
++)
177 if (argv
[i
][0] == '-')
179 for (opt
= argv
[i
] + 1; *opt
; opt
++)
182 case '-' : /* Next argument is a filename... */
184 if (i
< argc
&& !infile
)
190 case 'a' : /* Specify option... */
193 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
198 case 'c' : /* Specify cupsd.conf file location... */
202 if (!strcmp(command
, "convert"))
203 num_options
= cupsAddOption("copies", argv
[i
], num_options
,
206 strlcpy(cupsdconf
, argv
[i
], sizeof(cupsdconf
));
212 case 'D' : /* Delete input file after conversion */
216 case 'f' : /* Specify input file... */
218 if (i
< argc
&& !infile
)
224 case 'i' : /* Specify source MIME type... */
228 if (sscanf(argv
[i
], "%15[^/]/%255s", super
, type
) != 2)
237 case 'j' : /* Get job file or specify destination MIME type... */
238 if (strcmp(command
, "convert"))
243 get_job_file(argv
[i
]);
252 case 'm' : /* Specify destination MIME type... */
256 if (sscanf(argv
[i
], "%15[^/]/%255s", super
, type
) != 2)
265 case 'n' : /* Specify number of copies... */
268 num_options
= cupsAddOption("copies", argv
[i
], num_options
,
274 case 'o' : /* Specify option(s) or output filename */
278 if (!strcmp(command
, "convert"))
281 usage(command
, NULL
);
286 num_options
= cupsParseOptions(argv
[i
], num_options
,
293 case 'p' : /* Specify PPD file... */
294 case 'P' : /* Specify PPD file... */
302 case 't' : /* Specify title... */
303 case 'J' : /* Specify title... */
311 case 'u' : /* Delete PPD file after conversion */
315 case 'U' : /* Specify username... */
323 default : /* Something we don't understand... */
330 if (strcmp(command
, "convert"))
334 _cupsLangPuts(stderr
,
335 _("convert: Use the -f option to specify a file to "
337 usage(command
, NULL
);
342 _cupsLangPuts(stderr
,
343 _("cupsfilter: Only one filename can be specified!\n"));
344 usage(command
, NULL
);
347 if (!infile
&& !srctype
)
348 usage(command
, NULL
);
354 else if ((title
= strrchr(infile
, '/')) != NULL
)
361 * Load the cupsd.conf file and create the MIME database...
364 if (read_cupsd_conf(cupsdconf
))
367 snprintf(mimedir
, sizeof(mimedir
), "%s/mime", DataDir
);
369 mime
= mimeLoadTypes(NULL
, mimedir
);
370 mime
= mimeLoadTypes(mime
, ServerRoot
);
371 mime
= mimeLoadFilters(mime
, mimedir
, Path
);
372 mime
= mimeLoadFilters(mime
, ServerRoot
, Path
);
376 _cupsLangPrintf(stderr
,
377 _("%s: Unable to read MIME database from \"%s\" or "
379 command
, mimedir
, ServerRoot
);
383 printer_type
= add_printer_filters(command
, mime
, printer
, ppdfile
,
387 * Get the source and destination types...
392 sscanf(srctype
, "%15[^/]/%255s", super
, type
);
393 if ((src
= mimeType(mime
, super
, type
)) == NULL
)
395 _cupsLangPrintf(stderr
,
396 _("%s: Unknown source MIME type %s/%s!\n"),
397 command
, super
, type
);
401 else if ((src
= mimeFileType(mime
, infile
, infile
, &compression
)) == NULL
)
403 _cupsLangPrintf(stderr
,
404 _("%s: Unable to determine MIME type of \"%s\"!\n"),
409 sscanf(dsttype
, "%15[^/]/%255s", super
, type
);
410 if (!strcasecmp(super
, "printer"))
412 else if ((dst
= mimeType(mime
, super
, type
)) == NULL
)
414 _cupsLangPrintf(stderr
,
415 _("%s: Unknown destination MIME type %s/%s!\n"),
416 command
, super
, type
);
421 * Figure out how to filter the file...
427 * Special case - no filtering needed...
430 filters
= cupsArrayNew(NULL
, NULL
);
431 cupsArrayAdd(filters
, &GZIPFilter
);
433 else if ((filters
= mimeFilter(mime
, src
, dst
, &cost
)) == NULL
)
435 _cupsLangPrintf(stderr
,
436 _("%s: No filter to convert from %s/%s to %s/%s!\n"),
437 command
, src
->super
, src
->type
, dst
->super
, dst
->type
);
440 else if (compression
)
441 cupsArrayInsert(filters
, &GZIPFilter
);
449 mime_filter_t
*filter
, /* Current filter */
450 *prefilter
; /* Current pre-filter */
451 cups_array_t
*prefilters
= cupsArrayNew(NULL
, NULL
);
452 /* New filters array */
455 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
457 filter
= (mime_filter_t
*)cupsArrayNext(filters
))
459 if ((prefilter
= mimeFilterLookup(mime
, filter
->src
, prefilter_type
)))
460 cupsArrayAdd(prefilters
, prefilter
);
462 cupsArrayAdd(prefilters
, filter
);
465 cupsArrayDelete(filters
);
466 filters
= prefilters
;
473 status
= exec_filters(filters
, infile
, outfile
, ppdfile
, printer
, user
,
474 title
, num_options
, options
);
477 * Remove files as needed, then exit...
483 if (removeppd
&& ppdfile
)
486 if (removeinfile
&& infile
)
494 * 'add_printer_filter()' - Add a single filters from a PPD file.
499 const char *command
, /* I - Command name */
500 mime_t
*mime
, /* I - MIME database */
501 mime_type_t
*filtertype
, /* I - Printer or prefilter MIME type */
502 const char *filter
) /* I - Filter to add */
504 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
505 type
[MIME_MAX_TYPE
], /* Type for filter */
506 program
[1024]; /* Program/filter name */
507 int cost
; /* Cost of filter */
508 mime_type_t
*temptype
; /* MIME type looping var */
509 char filename
[1024]; /* Full filter filename */
513 * Parse the filter string; it should be in the following format:
515 * super/type cost program
518 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
521 _cupsLangPrintf(stderr
, _("%s: Invalid filter string \"%s\"\n"), command
,
527 * See if the filter program exists; if not, stop the printer and flag
531 if (strcmp(program
, "-"))
533 if (program
[0] == '/')
534 strlcpy(filename
, program
, sizeof(filename
));
536 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
538 if (access(filename
, X_OK
))
540 _cupsLangPrintf(stderr
, _("%s: Filter \"%s\" not available: %s\n"),
541 command
, program
, strerror(errno
));
547 * Add the filter to the MIME database, supporting wildcards as needed...
550 for (temptype
= mimeFirstType(mime
);
552 temptype
= mimeNextType(mime
))
553 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
554 !strcasecmp(temptype
->super
, super
)) &&
555 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
556 mimeAddFilter(mime
, temptype
, filtertype
, cost
, program
);
561 * 'add_printer_filters()' - Add filters from a PPD file.
564 static mime_type_t
* /* O - Printer type or NULL on error */
566 const char *command
, /* I - Command name */
567 mime_t
*mime
, /* I - MIME database */
568 const char *printer
, /* I - Printer name */
569 const char *ppdfile
, /* I - PPD file */
570 mime_type_t
**prefilter_type
) /* O - Prefilter type */
572 int i
; /* Looping var */
573 mime_type_t
*printer_type
; /* Printer MIME type */
574 ppd_file_t
*ppd
; /* PPD file data */
575 ppd_attr_t
*ppdattr
; /* Current prefilter */
578 *prefilter_type
= NULL
;
580 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
582 ppd_status_t status
; /* PPD load status */
584 status
= ppdLastError(&i
);
585 _cupsLangPrintf(stderr
, _("%s: Unable to open PPD file: %s on line %d\n"),
586 command
, ppdErrorString(status
), i
);
590 printer_type
= mimeAddType(mime
, "printer", printer
);
592 if (ppd
->num_filters
> 0)
594 for (i
= 0; i
< ppd
->num_filters
; i
++)
595 add_printer_filter(command
, mime
, printer_type
, ppd
->filters
[i
]);
599 add_printer_filter(command
, mime
, printer_type
,
600 "application/vnd.cups-command 0 commandtops");
601 add_printer_filter(command
, mime
, printer_type
,
602 "application/vnd.cups-postscript 0 -");
605 if ((ppdattr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
607 *prefilter_type
= mimeAddType(mime
, "prefilter", printer
);
609 for (; ppdattr
; ppdattr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
611 add_printer_filter(command
, mime
, *prefilter_type
, ppdattr
->value
);
614 return (printer_type
);
619 * 'compare_pids()' - Compare two filter PIDs...
622 static int /* O - Result of comparison */
623 compare_pids(mime_filter_t
*a
, /* I - First filter */
624 mime_filter_t
*b
) /* I - Second filter */
627 * Because we're particularly lazy, we store the process ID in the "cost"
631 return (a
->cost
- b
->cost
);
636 * 'escape_options()' - Convert an options array to a string.
639 static char * /* O - Option string */
641 int num_options
, /* I - Number of options */
642 cups_option_t
*options
) /* I - Options */
644 int i
; /* Looping var */
645 cups_option_t
*option
; /* Current option */
646 int bytes
; /* Number of bytes needed */
647 char *s
, /* Option string */
648 *sptr
, /* Pointer into string */
649 *vptr
; /* Pointer into value */
653 * Figure out the worst-case number of bytes we need for the option string.
656 for (i
= num_options
, option
= options
, bytes
= 1; i
> 0; i
--, option
++)
657 bytes
+= 2 * (strlen(option
->name
) + strlen(option
->value
)) + 2;
659 if ((s
= malloc(bytes
)) == NULL
)
663 * Copy the options to the string...
666 for (i
= num_options
, option
= options
, sptr
= s
; i
> 0; i
--, option
++)
668 if (!strcmp(option
->name
, "copies"))
674 strcpy(sptr
, option
->name
);
675 sptr
+= strlen(sptr
);
678 for (vptr
= option
->value
; *vptr
;)
680 if (strchr("\\ \t\n", *vptr
))
694 * 'exec_filter()' - Execute a single filter.
697 static int /* O - Process ID or -1 on error */
698 exec_filter(const char *filter
, /* I - Filter to execute */
699 char **argv
, /* I - Argument list */
700 char **envp
, /* I - Environment list */
701 int infd
, /* I - Stdin file descriptor */
702 int outfd
) /* I - Stdout file descriptor */
704 int pid
; /* Process ID */
705 #if defined(__APPLE__)
706 char processPath
[1024], /* CFProcessPath environment variable */
707 linkpath
[1024]; /* Link path for symlinks... */
708 int linkbytes
; /* Bytes for link path */
712 * Add special voodoo magic for MacOS X - this allows MacOS X
713 * programs to access their bundle resources properly...
716 if ((linkbytes
= readlink(filter
, linkpath
, sizeof(linkpath
) - 1)) > 0)
719 * Yes, this is a symlink to the actual program, nul-terminate and
723 linkpath
[linkbytes
] = '\0';
725 if (linkpath
[0] == '/')
726 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s",
729 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s/%s",
730 dirname((char *)filter
), linkpath
);
733 snprintf(processPath
, sizeof(processPath
), "CFProcessPath=%s", filter
);
735 envp
[0] = processPath
; /* Replace <CFProcessPath> string */
736 #endif /* __APPLE__ */
738 if ((pid
= fork()) == 0)
741 * Child process goes here...
743 * Update stdin/stdout/stderr as needed...
752 open("/dev/null", O_RDONLY
);
761 open("/dev/null", O_WRONLY
);
765 open("/dev/null", O_RDWR
);
766 fcntl(3, F_SETFL
, O_NDELAY
);
769 open("/dev/null", O_RDWR
);
770 fcntl(4, F_SETFL
, O_NDELAY
);
776 execve(filter
, argv
, envp
);
788 * 'exec_filters()' - Execute filters for the given file and options.
791 static int /* O - 0 on success, 1 on error */
792 exec_filters(cups_array_t
*filters
, /* I - Array of filters to run */
793 const char *infile
, /* I - File to filter */
794 const char *outfile
, /* I - File to create */
795 const char *ppdfile
, /* I - PPD file, if any */
796 const char *printer
, /* I - Printer name */
797 const char *user
, /* I - Username */
798 const char *title
, /* I - Job title */
799 int num_options
, /* I - Number of filter options */
800 cups_option_t
*options
) /* I - Filter options */
802 int i
; /* Looping var */
803 const char *argv
[8], /* Command-line arguments */
804 *envp
[12], /* Environment variables */
805 *temp
; /* Temporary string */
806 char *optstr
, /* Filter options */
807 content_type
[1024], /* CONTENT_TYPE */
808 cups_datadir
[1024], /* CUPS_DATADIR */
809 cups_fontpath
[1024], /* CUPS_FONTPATH */
810 cups_serverbin
[1024], /* CUPS_SERVERBIN */
811 cups_serverroot
[1024], /* CUPS_SERVERROOT */
812 lang
[1024], /* LANG */
813 path
[1024], /* PATH */
815 rip_cache
[1024], /* RIP_CACHE */
816 userenv
[1024], /* USER */
817 program
[1024]; /* Program to run */
818 mime_filter_t
*filter
, /* Current filter */
819 *next
; /* Next filter */
820 int current
, /* Current filter */
821 filterfds
[2][2], /* Pipes for filters */
822 pid
, /* Process ID of filter */
823 status
, /* Exit status */
824 retval
; /* Return value */
825 cups_array_t
*pids
; /* Executed filters array */
826 mime_filter_t key
; /* Search key for filters */
827 cups_lang_t
*language
; /* Current language */
831 * Setup the filter environment and command-line...
834 optstr
= escape_options(num_options
, options
);
836 filter
= cupsArrayFirst(filters
);
837 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s/%s",
838 filter
->src
->super
, filter
->src
->type
);
839 snprintf(cups_datadir
, sizeof(cups_datadir
), "CUPS_DATADIR=%s", DataDir
);
840 snprintf(cups_fontpath
, sizeof(cups_fontpath
), "CUPS_FONTPATH=%s", FontPath
);
841 snprintf(cups_serverbin
, sizeof(cups_serverbin
), "CUPS_SERVERBIN=%s",
843 snprintf(cups_serverroot
, sizeof(cups_serverroot
), "CUPS_SERVERROOT=%s",
845 language
= cupsLangDefault();
846 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", language
->language
);
847 snprintf(path
, sizeof(path
), "PATH=%s", Path
);
849 snprintf(ppd
, sizeof(ppd
), "PPD=%s", ppdfile
);
850 else if ((temp
= getenv("PPD")) != NULL
)
851 snprintf(ppd
, sizeof(ppd
), "PPD=%s", temp
);
854 if (!access("/System/Library/Frameworks/ApplicationServices.framework/"
855 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
856 "Resources/English.lproj/Generic.ppd", 0))
857 strlcpy(ppd
, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
858 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
859 "Resources/English.lproj/Generic.ppd", sizeof(ppd
));
861 strlcpy(ppd
, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
862 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
863 "Resources/Generic.ppd", sizeof(ppd
));
865 snprintf(ppd
, sizeof(ppd
), "PPD=%s/model/laserjet.ppd", DataDir
);
866 #endif /* __APPLE__ */
867 snprintf(rip_cache
, sizeof(rip_cache
), "RIP_CACHE=%s", RIPCache
);
868 snprintf(userenv
, sizeof(userenv
), "USER=%s", user
);
870 argv
[0] = (char *)printer
;
874 argv
[4] = cupsGetOption("copies", num_options
, options
);
882 envp
[0] = "<CFProcessPath>";
883 envp
[1] = content_type
;
884 envp
[2] = cups_datadir
;
885 envp
[3] = cups_fontpath
;
886 envp
[4] = cups_serverbin
;
887 envp
[5] = cups_serverroot
;
895 for (i
= 0; argv
[i
]; i
++)
896 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
898 for (i
= 0; envp
[i
]; i
++)
899 fprintf(stderr
, "DEBUG: envp[%d]=\"%s\"\n", i
, envp
[i
]);
902 * Execute all of the filters...
905 pids
= cupsArrayNew((cups_array_func_t
)compare_pids
, NULL
);
907 filterfds
[0][0] = -1;
908 filterfds
[0][1] = -1;
909 filterfds
[1][0] = -1;
910 filterfds
[1][1] = -1;
915 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
917 filter
= next
, current
= 1 - current
)
919 next
= (mime_filter_t
*)cupsArrayNext(filters
);
921 if (filter
->filter
[0] == '/')
922 strlcpy(program
, filter
->filter
, sizeof(program
));
924 snprintf(program
, sizeof(program
), "%s/filter/%s", ServerBin
,
927 if (filterfds
[!current
][1] > 1)
929 close(filterfds
[1 - current
][0]);
930 close(filterfds
[1 - current
][1]);
932 filterfds
[1 - current
][0] = -1;
933 filterfds
[1 - current
][0] = -1;
937 open_pipe(filterfds
[1 - current
]);
940 filterfds
[1 - current
][1] = open(outfile
, O_CREAT
| O_TRUNC
| O_WRONLY
,
943 if (filterfds
[1 - current
][1] < 0)
944 fprintf(stderr
, "ERROR: Unable to create \"%s\" - %s\n", outfile
,
948 filterfds
[1 - current
][1] = 1;
950 pid
= exec_filter(program
, (char **)argv
, (char **)envp
,
951 filterfds
[current
][0], filterfds
[1 - current
][1]);
955 fprintf(stderr
, "INFO: %s (PID %d) started.\n", filter
->filter
, pid
);
958 cupsArrayAdd(pids
, filter
);
967 * Close remaining pipes...
970 if (filterfds
[0][1] > 1)
972 close(filterfds
[0][0]);
973 close(filterfds
[0][1]);
976 if (filterfds
[1][1] > 1)
978 close(filterfds
[1][0]);
979 close(filterfds
[1][1]);
983 * Wait for the children to exit...
988 while (cupsArrayCount(pids
) > 0)
990 if ((pid
= wait(&status
)) < 0)
994 if ((filter
= (mime_filter_t
*)cupsArrayFind(pids
, &key
)) != NULL
)
996 cupsArrayRemove(pids
, filter
);
1000 if (WIFEXITED(status
))
1001 fprintf(stderr
, "ERROR: %s (PID %d) stopped with status %d!\n",
1002 filter
->filter
, pid
, WEXITSTATUS(status
));
1004 fprintf(stderr
, "ERROR: %s (PID %d) crashed on signal %d!\n",
1005 filter
->filter
, pid
, WTERMSIG(status
));
1010 fprintf(stderr
, "INFO: %s (PID %d) exited with no errors.\n",
1011 filter
->filter
, pid
);
1015 cupsArrayDelete(pids
);
1022 * 'get_job_file()' - Get the specified job file.
1026 get_job_file(const char *job
) /* I - Job ID */
1028 long jobid
, /* Job ID */
1029 docnum
; /* Document number */
1030 const char *jobptr
; /* Pointer into job ID string */
1031 char uri
[1024]; /* job-uri */
1032 http_t
*http
; /* Connection to server */
1033 ipp_t
*request
; /* Request data */
1034 int tempfd
; /* Temporary file */
1038 * Get the job ID and document number, if any...
1041 if ((jobptr
= strrchr(job
, '-')) != NULL
)
1046 jobid
= strtol(jobptr
, (char **)&jobptr
, 10);
1049 docnum
= strtol(jobptr
+ 1, NULL
, 10);
1053 if (jobid
< 1 || jobid
> INT_MAX
)
1055 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid job ID %d!\n"), (int)jobid
);
1059 if (docnum
< 1 || docnum
> INT_MAX
)
1061 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid document number %d!\n"),
1067 * Ask the server for the document file...
1070 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1071 cupsEncryption())) == NULL
)
1073 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server\n"),
1078 request
= ippNewRequest(CUPS_GET_DOCUMENT
);
1080 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", (int)jobid
);
1082 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1083 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "document-number",
1086 if ((tempfd
= cupsTempFd(TempFile
, sizeof(TempFile
))) == -1)
1088 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
1093 signal(SIGTERM
, sighandler
);
1095 ippDelete(cupsDoIORequest(http
, request
, "/", -1, tempfd
));
1101 if (cupsLastError() != IPP_OK
)
1103 _cupsLangPrintf(stderr
, _("cupsfilter: Unable to get job file - %s\n"),
1104 cupsLastErrorString());
1112 * 'open_pipe()' - Create a pipe which is closed on exec.
1115 static int /* O - 0 on success, -1 on error */
1116 open_pipe(int *fds
) /* O - Pipe file descriptors (2) */
1119 * Create the pipe...
1131 * Set the "close on exec" flag on each end of the pipe...
1134 if (fcntl(fds
[0], F_SETFD
, fcntl(fds
[0], F_GETFD
) | FD_CLOEXEC
))
1145 if (fcntl(fds
[1], F_SETFD
, fcntl(fds
[1], F_GETFD
) | FD_CLOEXEC
))
1157 * Return 0 indicating success...
1165 * 'read_cupsd_conf()' - Read the cupsd.conf file to get the filter settings.
1168 static int /* O - 0 on success, 1 on error */
1169 read_cupsd_conf(const char *filename
) /* I - File to read */
1171 cups_file_t
*fp
; /* cupsd.conf file */
1172 const char *temp
; /* Temporary string */
1173 char line
[1024], /* Line from file */
1174 *ptr
; /* Pointer into line */
1175 int linenum
; /* Current line number */
1178 if ((temp
= getenv("CUPS_DATADIR")) != NULL
)
1179 set_string(&DataDir
, temp
);
1181 set_string(&DataDir
, CUPS_DATADIR
);
1183 if ((temp
= getenv("CUPS_FONTPATH")) != NULL
)
1184 set_string(&FontPath
, temp
);
1186 set_string(&FontPath
, CUPS_FONTPATH
);
1188 set_string(&RIPCache
, "8m");
1190 if ((temp
= getenv("CUPS_SERVERBIN")) != NULL
)
1191 set_string(&ServerBin
, temp
);
1193 set_string(&ServerBin
, CUPS_SERVERBIN
);
1195 strlcpy(line
, filename
, sizeof(line
));
1196 if ((ptr
= strrchr(line
, '/')) != NULL
)
1199 getcwd(line
, sizeof(line
));
1201 set_string(&ServerRoot
, line
);
1203 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1207 while (cupsFileGetConf(fp
, line
, sizeof(line
), &ptr
, &linenum
))
1209 if (!strcasecmp(line
, "DataDir"))
1210 set_string(&DataDir
, ptr
);
1211 else if (!strcasecmp(line
, "FontPath"))
1212 set_string(&FontPath
, ptr
);
1213 else if (!strcasecmp(line
, "RIPCache"))
1214 set_string(&RIPCache
, ptr
);
1215 else if (!strcasecmp(line
, "ServerBin"))
1216 set_string(&ServerBin
, ptr
);
1217 else if (!strcasecmp(line
, "ServerRoot"))
1218 set_string(&ServerRoot
, ptr
);
1224 snprintf(line
, sizeof(line
),
1225 "%s/filter:" CUPS_BINDIR
":" CUPS_SBINDIR
":/bin:/usr/bin",
1227 set_string(&Path
, line
);
1234 * 'set_string()' - Copy and set a string.
1238 set_string(char **s
, /* O - Copy of string */
1239 const char *val
) /* I - String to copy */
1249 * 'sighandler()' - Signal catcher for when we print from stdin...
1253 sighandler(int s
) /* I - Signal number */
1256 * Remove the temporary file we're using to print a job file...
1271 * 'usage()' - Show program usage...
1275 usage(const char *command
, /* I - Command name */
1276 const char *opt
) /* I - Incorrect option, if any */
1279 _cupsLangPrintf(stderr
, _("%s: Unknown option '%c'!\n"), command
, *opt
);
1281 if (!strcmp(command
, "cupsfilter"))
1282 _cupsLangPuts(stdout
,
1283 _("Usage: cupsfilter -m mime/type [ options ] filename\n"
1287 " -c cupsd.conf Set cupsd.conf file to use\n"
1288 " -j job-id[,N] Filter file N from the specified job (default is file 1)\n"
1289 " -n copies Set number of copies\n"
1290 " -o name=value Set option(s)\n"
1291 " -p filename.ppd Set PPD file\n"
1292 " -t title Set title\n"));
1294 _cupsLangPuts(stdout
,
1295 _("Usage: convert [ options ]\n"
1299 " -f filename Set file to be converted (otherwise stdin)\n"
1300 " -o filename Set file to be generated (otherwise stdout)\n"
1301 " -i mime/type Set input MIME type (otherwise auto-typed)\n"
1302 " -j mime/type Set output MIME type (otherwise application/pdf)\n"
1303 " -P filename.ppd Set PPD file\n"
1304 " -a 'name=value ...' Set option(s)\n"
1305 " -U username Set username for job\n"
1306 " -J title Set title\n"
1307 " -c copies Set number of copies\n"
1308 " -u Remove the PPD file when finished\n"
1309 " -D Remove the input file when finished\n"));
1316 * End of "$Id: cupsfilter.c 7952 2008-09-17 00:56:20Z mike $".