]>
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-2008 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 if ((ppd
= ppdOpenFile(ppdfile
)) == NULL
)
580 ppd_status_t status
; /* PPD load status */
582 status
= ppdLastError(&i
);
583 _cupsLangPrintf(stderr
, _("%s: Unable to open PPD file: %s on line %d\n"),
584 command
, ppdErrorString(status
), i
);
588 printer_type
= mimeAddType(mime
, "printer", printer
);
590 if (ppd
->num_filters
> 0)
592 for (i
= 0; i
< ppd
->num_filters
; i
++)
593 add_printer_filter(command
, mime
, printer_type
, ppd
->filters
[i
]);
597 add_printer_filter(command
, mime
, printer_type
,
598 "application/vnd.cups-command 0 commandtops");
599 add_printer_filter(command
, mime
, printer_type
,
600 "application/vnd.cups-postscript 0 -");
603 if ((ppdattr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
605 *prefilter_type
= mimeAddType(mime
, "prefilter", printer
);
607 for (; ppdattr
; ppdattr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
609 add_printer_filter(command
, mime
, *prefilter_type
, ppdattr
->value
);
612 *prefilter_type
= NULL
;
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
[11], /* Environment variables */
805 *temp
; /* Temporary string */
806 char *optstr
, /* Filter options */
807 cups_datadir
[1024], /* CUPS_DATADIR */
808 cups_fontpath
[1024], /* CUPS_FONTPATH */
809 cups_serverbin
[1024], /* CUPS_SERVERBIN */
810 cups_serverroot
[1024], /* CUPS_SERVERROOT */
811 lang
[1024], /* LANG */
812 path
[1024], /* PATH */
814 rip_cache
[1024], /* RIP_CACHE */
815 userenv
[1024], /* USER */
816 program
[1024]; /* Program to run */
817 mime_filter_t
*filter
, /* Current filter */
818 *next
; /* Next filter */
819 int current
, /* Current filter */
820 filterfds
[2][2], /* Pipes for filters */
821 pid
, /* Process ID of filter */
822 status
, /* Exit status */
823 retval
; /* Return value */
824 cups_array_t
*pids
; /* Executed filters array */
825 mime_filter_t key
; /* Search key for filters */
826 cups_lang_t
*language
; /* Current language */
830 * Setup the filter environment and command-line...
833 optstr
= escape_options(num_options
, options
);
835 snprintf(cups_datadir
, sizeof(cups_datadir
), "CUPS_DATADIR=%s", DataDir
);
836 snprintf(cups_fontpath
, sizeof(cups_fontpath
), "CUPS_FONTPATH=%s", FontPath
);
837 snprintf(cups_serverbin
, sizeof(cups_serverbin
), "CUPS_SERVERBIN=%s",
839 snprintf(cups_serverroot
, sizeof(cups_serverroot
), "CUPS_SERVERROOT=%s",
841 language
= cupsLangDefault();
842 snprintf(lang
, sizeof(lang
), "LANG=%s.UTF8", language
->language
);
843 snprintf(path
, sizeof(path
), "PATH=%s", Path
);
845 snprintf(ppd
, sizeof(ppd
), "PPD=%s", ppdfile
);
846 else if ((temp
= getenv("PPD")) != NULL
)
847 snprintf(ppd
, sizeof(ppd
), "PPD=%s", temp
);
850 if (!access("/System/Library/Frameworks/ApplicationServices.framework/"
851 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
852 "Resources/English.lproj/Generic.ppd", 0))
853 strlcpy(ppd
, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
854 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
855 "Resources/English.lproj/Generic.ppd", sizeof(ppd
));
857 strlcpy(ppd
, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
858 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
859 "Resources/Generic.ppd", sizeof(ppd
));
861 snprintf(ppd
, sizeof(ppd
), "PPD=%s/model/laserjet.ppd", DataDir
);
862 #endif /* __APPLE__ */
863 snprintf(rip_cache
, sizeof(rip_cache
), "RIP_CACHE=%s", RIPCache
);
864 snprintf(userenv
, sizeof(userenv
), "USER=%s", user
);
866 argv
[0] = (char *)printer
;
870 argv
[4] = cupsGetOption("copies", num_options
, options
);
878 envp
[0] = "<CFProcessPath>";
879 envp
[1] = cups_datadir
;
880 envp
[2] = cups_fontpath
;
881 envp
[3] = cups_serverbin
;
882 envp
[4] = cups_serverroot
;
890 for (i
= 0; argv
[i
]; i
++)
891 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
893 for (i
= 0; envp
[i
]; i
++)
894 fprintf(stderr
, "DEBUG: envp[%d]=\"%s\"\n", i
, envp
[i
]);
897 * Execute all of the filters...
900 pids
= cupsArrayNew((cups_array_func_t
)compare_pids
, NULL
);
902 filterfds
[0][0] = -1;
903 filterfds
[0][1] = -1;
904 filterfds
[1][0] = -1;
905 filterfds
[1][1] = -1;
910 for (filter
= (mime_filter_t
*)cupsArrayFirst(filters
);
912 filter
= next
, current
= 1 - current
)
914 next
= (mime_filter_t
*)cupsArrayNext(filters
);
916 if (filter
->filter
[0] == '/')
917 strlcpy(program
, filter
->filter
, sizeof(program
));
919 snprintf(program
, sizeof(program
), "%s/filter/%s", ServerBin
,
922 if (filterfds
[!current
][1] > 1)
924 close(filterfds
[1 - current
][0]);
925 close(filterfds
[1 - current
][1]);
927 filterfds
[1 - current
][0] = -1;
928 filterfds
[1 - current
][0] = -1;
932 open_pipe(filterfds
[1 - current
]);
935 filterfds
[1 - current
][1] = open(outfile
, O_CREAT
| O_TRUNC
| O_WRONLY
,
938 if (filterfds
[1 - current
][1] < 0)
939 fprintf(stderr
, "ERROR: Unable to create \"%s\" - %s\n", outfile
,
943 filterfds
[1 - current
][1] = 1;
945 pid
= exec_filter(program
, (char **)argv
, (char **)envp
,
946 filterfds
[current
][0], filterfds
[1 - current
][1]);
950 fprintf(stderr
, "INFO: %s (PID %d) started.\n", filter
->filter
, pid
);
953 cupsArrayAdd(pids
, filter
);
962 * Close remaining pipes...
965 if (filterfds
[0][1] > 1)
967 close(filterfds
[0][0]);
968 close(filterfds
[0][1]);
971 if (filterfds
[1][1] > 1)
973 close(filterfds
[1][0]);
974 close(filterfds
[1][1]);
978 * Wait for the children to exit...
983 while (cupsArrayCount(pids
) > 0)
985 if ((pid
= wait(&status
)) < 0)
989 if ((filter
= (mime_filter_t
*)cupsArrayFind(pids
, &key
)) != NULL
)
991 cupsArrayRemove(pids
, filter
);
995 if (WIFEXITED(status
))
996 fprintf(stderr
, "ERROR: %s (PID %d) stopped with status %d!\n",
997 filter
->filter
, pid
, WEXITSTATUS(status
));
999 fprintf(stderr
, "ERROR: %s (PID %d) crashed on signal %d!\n",
1000 filter
->filter
, pid
, WTERMSIG(status
));
1005 fprintf(stderr
, "INFO: %s (PID %d) exited with no errors.\n",
1006 filter
->filter
, pid
);
1010 cupsArrayDelete(pids
);
1017 * 'get_job_file()' - Get the specified job file.
1021 get_job_file(const char *job
) /* I - Job ID */
1023 long jobid
, /* Job ID */
1024 docnum
; /* Document number */
1025 const char *jobptr
; /* Pointer into job ID string */
1026 char uri
[1024]; /* job-uri */
1027 http_t
*http
; /* Connection to server */
1028 ipp_t
*request
; /* Request data */
1029 int tempfd
; /* Temporary file */
1033 * Get the job ID and document number, if any...
1036 if ((jobptr
= strrchr(job
, '-')) != NULL
)
1041 jobid
= strtol(jobptr
, (char **)&jobptr
, 10);
1044 docnum
= strtol(jobptr
+ 1, NULL
, 10);
1048 if (jobid
< 1 || jobid
> INT_MAX
)
1050 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid job ID %d!\n"), (int)jobid
);
1054 if (docnum
< 1 || docnum
> INT_MAX
)
1056 _cupsLangPrintf(stderr
, _("cupsfilter: Invalid document number %d!\n"),
1062 * Ask the server for the document file...
1065 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1066 cupsEncryption())) == NULL
)
1068 _cupsLangPrintf(stderr
, _("%s: Unable to connect to server\n"),
1073 request
= ippNewRequest(CUPS_GET_DOCUMENT
);
1075 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", (int)jobid
);
1077 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1078 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "document-number",
1081 if ((tempfd
= cupsTempFd(TempFile
, sizeof(TempFile
))) == -1)
1083 _cupsLangPrintf(stderr
,
1084 _("cupsfilter: Unable to create temporary file: %s\n"),
1090 signal(SIGTERM
, sighandler
);
1092 ippDelete(cupsDoIORequest(http
, request
, "/", -1, tempfd
));
1098 if (cupsLastError() != IPP_OK
)
1100 _cupsLangPrintf(stderr
, _("cupsfilter: Unable to get job file - %s\n"),
1101 cupsLastErrorString());
1109 * 'open_pipe()' - Create a pipe which is closed on exec.
1112 static int /* O - 0 on success, -1 on error */
1113 open_pipe(int *fds
) /* O - Pipe file descriptors (2) */
1116 * Create the pipe...
1128 * Set the "close on exec" flag on each end of the pipe...
1131 if (fcntl(fds
[0], F_SETFD
, fcntl(fds
[0], F_GETFD
) | FD_CLOEXEC
))
1142 if (fcntl(fds
[1], F_SETFD
, fcntl(fds
[1], F_GETFD
) | FD_CLOEXEC
))
1154 * Return 0 indicating success...
1162 * 'read_cupsd_conf()' - Read the cupsd.conf file to get the filter settings.
1165 static int /* O - 0 on success, 1 on error */
1166 read_cupsd_conf(const char *filename
) /* I - File to read */
1168 cups_file_t
*fp
; /* cupsd.conf file */
1169 const char *temp
; /* Temporary string */
1170 char line
[1024], /* Line from file */
1171 *ptr
; /* Pointer into line */
1172 int linenum
; /* Current line number */
1175 if ((temp
= getenv("CUPS_DATADIR")) != NULL
)
1176 set_string(&DataDir
, temp
);
1178 set_string(&DataDir
, CUPS_DATADIR
);
1180 if ((temp
= getenv("CUPS_FONTPATH")) != NULL
)
1181 set_string(&FontPath
, temp
);
1183 set_string(&FontPath
, CUPS_FONTPATH
);
1185 set_string(&RIPCache
, "8m");
1187 if ((temp
= getenv("CUPS_SERVERBIN")) != NULL
)
1188 set_string(&ServerBin
, temp
);
1190 set_string(&ServerBin
, CUPS_SERVERBIN
);
1192 strlcpy(line
, filename
, sizeof(line
));
1193 if ((ptr
= strrchr(line
, '/')) != NULL
)
1196 getcwd(line
, sizeof(line
));
1198 set_string(&ServerRoot
, line
);
1200 if ((fp
= cupsFileOpen(filename
, "r")) != NULL
)
1204 while (cupsFileGetConf(fp
, line
, sizeof(line
), &ptr
, &linenum
))
1206 if (!strcasecmp(line
, "DataDir"))
1207 set_string(&DataDir
, ptr
);
1208 else if (!strcasecmp(line
, "FontPath"))
1209 set_string(&FontPath
, ptr
);
1210 else if (!strcasecmp(line
, "RIPCache"))
1211 set_string(&RIPCache
, ptr
);
1212 else if (!strcasecmp(line
, "ServerBin"))
1213 set_string(&ServerBin
, ptr
);
1214 else if (!strcasecmp(line
, "ServerRoot"))
1215 set_string(&ServerRoot
, ptr
);
1221 snprintf(line
, sizeof(line
),
1222 "%s/filter:" CUPS_BINDIR
":" CUPS_SBINDIR
":/bin:/usr/bin",
1224 set_string(&Path
, line
);
1231 * 'set_string()' - Copy and set a string.
1235 set_string(char **s
, /* O - Copy of string */
1236 const char *val
) /* I - String to copy */
1246 * 'sighandler()' - Signal catcher for when we print from stdin...
1250 sighandler(int s
) /* I - Signal number */
1253 * Remove the temporary file we're using to print a job file...
1268 * 'usage()' - Show program usage...
1272 usage(const char *command
, /* I - Command name */
1273 const char *opt
) /* I - Incorrect option, if any */
1276 _cupsLangPrintf(stderr
, _("%s: Unknown option '%c'!\n"), command
, *opt
);
1278 if (!strcmp(command
, "cupsfilter"))
1279 _cupsLangPuts(stdout
,
1280 _("Usage: cupsfilter -m mime/type [ options ] filename\n"
1284 " -c cupsd.conf Set cupsd.conf file to use\n"
1285 " -j job-id[,N] Filter file N from the specified job (default is file 1)\n"
1286 " -n copies Set number of copies\n"
1287 " -o name=value Set option(s)\n"
1288 " -p filename.ppd Set PPD file\n"
1289 " -t title Set title\n"));
1291 _cupsLangPuts(stdout
,
1292 _("Usage: convert [ options ]\n"
1296 " -f filename Set file to be converted (otherwise stdin)\n"
1297 " -o filename Set file to be generated (otherwise stdout)\n"
1298 " -i mime/type Set input MIME type (otherwise auto-typed)\n"
1299 " -j mime/type Set output MIME type (otherwise application/pdf)\n"
1300 " -P filename.ppd Set PPD file\n"
1301 " -a 'name=value ...' Set option(s)\n"
1302 " -U username Set username for job\n"
1303 " -J title Set title\n"
1304 " -c copies Set number of copies\n"
1305 " -u Remove the PPD file when finished\n"
1306 " -D Remove the input file when finished\n"));
1313 * End of "$Id: cupsfilter.c 7952 2008-09-17 00:56:20Z mike $".