]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cupsfilter.c
Add --list-filters options to cupsfilter (STR #4325)
[thirdparty/cups.git] / scheduler / cupsfilter.c
1 /*
2 * "$Id$"
3 *
4 * Filtering program for CUPS.
5 *
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 *
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/".
14 */
15
16 /*
17 * Include necessary headers...
18 */
19
20 #include <cups/cups-private.h>
21 #include <cups/file-private.h>
22 #include <cups/ppd-private.h>
23 #include "mime.h"
24 #include <limits.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #if defined(__APPLE__)
30 # include <libgen.h>
31 #endif /* __APPLE__ */
32
33
34 /*
35 * Local globals...
36 */
37
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 */
42 {
43 NULL, /* Source type */
44 NULL, /* Destination type */
45 0, /* Cost */
46 "gziptoany" /* Filter program to run */
47 };
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] = "";
56 /* Temporary file */
57
58
59 /*
60 * Local functions...
61 */
62
63 static void add_printer_filter(const char *command, mime_t *mime,
64 mime_type_t *printer_type,
65 const char *filter);
66 static mime_type_t *add_printer_filters(const char *command,
67 mime_t *mime, const char *printer,
68 const char *ppdfile,
69 mime_type_t **prefilter_type);
70 static void check_cb(void *context, _cups_fc_result_t result,
71 const char *message);
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));
88
89
90 /*
91 * 'main()' - Main entry for the test program.
92 */
93
94 int /* O - Exit status */
95 main(int argc, /* I - Number of command-line args */
96 char *argv[]) /* I - Command-line arguments */
97 {
98 int i, /* Looping vars */
99 list_filters = 0; /* Just list the filters? */
100 const char *command, /* Command name */
101 *opt, /* Current option */
102 *printer; /* Printer name */
103 mime_type_t *printer_type, /* Printer MIME type */
104 *prefilter_type; /* Printer prefilter MIME type */
105 char *srctype, /* Source type */
106 *dsttype, /* Destination type */
107 super[MIME_MAX_SUPER], /* Super-type name */
108 type[MIME_MAX_TYPE]; /* Type name */
109 int compression; /* Compression of file */
110 int cost; /* Cost of filters */
111 mime_t *mime; /* MIME database */
112 char mimedir[1024]; /* MIME directory */
113 char *infile, /* File to filter */
114 *outfile; /* File to create */
115 char cupsfilesconf[1024]; /* cups-files.conf file */
116 const char *server_root; /* CUPS_SERVERROOT environment variable */
117 mime_type_t *src, /* Source type */
118 *dst; /* Destination type */
119 cups_array_t *filters; /* Filters for the file */
120 int num_options; /* Number of options */
121 cups_option_t *options; /* Options */
122 const char *ppdfile; /* PPD file */
123 const char *title, /* Title string */
124 *user; /* Username */
125 int all_filters, /* Use all filters */
126 removeppd, /* Remove PPD file */
127 removeinfile; /* Remove input file */
128 int status; /* Execution status */
129
130
131 /*
132 * Setup defaults...
133 */
134
135 if ((command = strrchr(argv[0], '/')) != NULL)
136 command ++;
137 else
138 command = argv[0];
139
140 printer = !strcmp(command, "convert") ? "tofile" : "cupsfilter";
141 mime = NULL;
142 srctype = NULL;
143 compression = 0;
144 dsttype = "application/pdf";
145 infile = NULL;
146 outfile = NULL;
147 num_options = 0;
148 options = NULL;
149 ppdfile = NULL;
150 title = NULL;
151 user = cupsUser();
152 all_filters = 0;
153 removeppd = 0;
154 removeinfile = 0;
155
156 if ((server_root = getenv("CUPS_SERVERROOT")) == NULL)
157 server_root = CUPS_SERVERROOT;
158
159 snprintf(cupsfilesconf, sizeof(cupsfilesconf), "%s/cups-files.conf", server_root);
160
161 /*
162 * Process command-line arguments...
163 */
164
165 _cupsSetLocale(argv);
166
167 for (i = 1; i < argc; i ++)
168 {
169 if (argv[i][0] == '-')
170 {
171 if (!strcmp(argv[i], "--list-filters"))
172 {
173 list_filters = 1;
174 }
175 else if (!strcmp(argv[i], "--"))
176 {
177 i ++;
178 if (i < argc && !infile)
179 infile = argv[i];
180 else
181 usage(opt);
182 }
183 else
184 {
185 for (opt = argv[i] + 1; *opt; opt ++)
186 {
187 switch (*opt)
188 {
189 case 'a' : /* Specify option... */
190 i ++;
191 if (i < argc)
192 num_options = cupsParseOptions(argv[i], num_options, &options);
193 else
194 usage(opt);
195 break;
196
197 case 'c' : /* Specify cups-files.conf file location... */
198 i ++;
199 if (i < argc)
200 {
201 if (!strcmp(command, "convert"))
202 num_options = cupsAddOption("copies", argv[i], num_options, &options);
203 else
204 strlcpy(cupsfilesconf, argv[i], sizeof(cupsfilesconf));
205 }
206 else
207 usage(opt);
208 break;
209
210 case 'd' : /* Specify the real printer name */
211 i ++;
212 if (i < argc)
213 printer = argv[i];
214 else
215 usage(opt);
216 break;
217
218 case 'D' : /* Delete input file after conversion */
219 removeinfile = 1;
220 break;
221
222 case 'e' : /* Use every filter from the PPD file */
223 all_filters = 1;
224 break;
225
226 case 'f' : /* Specify input file... */
227 i ++;
228 if (i < argc && !infile)
229 infile = argv[i];
230 else
231 usage(opt);
232 break;
233
234 case 'i' : /* Specify source MIME type... */
235 i ++;
236 if (i < argc)
237 {
238 if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2)
239 usage(opt);
240
241 srctype = argv[i];
242 }
243 else
244 usage(opt);
245 break;
246
247 case 'j' : /* Get job file or specify destination MIME type... */
248 if (strcmp(command, "convert"))
249 {
250 i ++;
251 if (i < argc)
252 {
253 get_job_file(argv[i]);
254 infile = TempFile;
255 }
256 else
257 usage(opt);
258
259 break;
260 }
261
262 case 'm' : /* Specify destination MIME type... */
263 i ++;
264 if (i < argc)
265 {
266 if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2)
267 usage(opt);
268
269 dsttype = argv[i];
270 }
271 else
272 usage(opt);
273 break;
274
275 case 'n' : /* Specify number of copies... */
276 i ++;
277 if (i < argc)
278 num_options = cupsAddOption("copies", argv[i], num_options, &options);
279 else
280 usage(opt);
281 break;
282
283 case 'o' : /* Specify option(s) or output filename */
284 i ++;
285 if (i < argc)
286 {
287 if (!strcmp(command, "convert"))
288 {
289 if (outfile)
290 usage(NULL);
291 else
292 outfile = argv[i];
293 }
294 else
295 num_options = cupsParseOptions(argv[i], num_options, &options);
296 }
297 else
298 usage(opt);
299 break;
300
301 case 'p' : /* Specify PPD file... */
302 case 'P' : /* Specify PPD file... */
303 i ++;
304 if (i < argc)
305 ppdfile = argv[i];
306 else
307 usage(opt);
308 break;
309
310 case 't' : /* Specify title... */
311 case 'J' : /* Specify title... */
312 i ++;
313 if (i < argc)
314 title = argv[i];
315 else
316 usage(opt);
317 break;
318
319 case 'u' : /* Delete PPD file after conversion */
320 removeppd = 1;
321 break;
322
323 case 'U' : /* Specify username... */
324 i ++;
325 if (i < argc)
326 user = argv[i];
327 else
328 usage(opt);
329 break;
330
331 default : /* Something we don't understand... */
332 usage(opt);
333 break;
334 }
335 }
336 }
337 }
338 else if (!infile)
339 {
340 if (strcmp(command, "convert"))
341 infile = argv[i];
342 else
343 usage(NULL);
344 }
345 else
346 {
347 _cupsLangPuts(stderr,
348 _("cupsfilter: Only one filename can be specified."));
349 usage(NULL);
350 }
351 }
352
353 if (!infile && !srctype)
354 usage(NULL);
355
356 if (!title)
357 {
358 if (!infile)
359 title = "(stdin)";
360 else if ((title = strrchr(infile, '/')) != NULL)
361 title ++;
362 else
363 title = infile;
364 }
365
366 /*
367 * Load the cups-files.conf file and create the MIME database...
368 */
369
370 if (read_cups_files_conf(cupsfilesconf))
371 return (1);
372
373 snprintf(mimedir, sizeof(mimedir), "%s/mime", DataDir);
374
375 mime = mimeLoadTypes(NULL, mimedir);
376 mime = mimeLoadTypes(mime, ServerRoot);
377 mime = mimeLoadFilters(mime, mimedir, Path);
378 mime = mimeLoadFilters(mime, ServerRoot, Path);
379
380 if (!mime)
381 {
382 _cupsLangPrintf(stderr,
383 _("%s: Unable to read MIME database from \"%s\" or "
384 "\"%s\"."),
385 command, mimedir, ServerRoot);
386 return (1);
387 }
388
389 prefilter_type = NULL;
390
391 if (all_filters)
392 printer_type = add_printer_filters(command, mime, printer, ppdfile,
393 &prefilter_type);
394 else
395 printer_type = mimeType(mime, "application", "vnd.cups-postscript");
396
397 /*
398 * Get the source and destination types...
399 */
400
401 if (srctype)
402 {
403 /* sscanf return value already checked above */
404 sscanf(srctype, "%15[^/]/%255s", super, type);
405 if ((src = mimeType(mime, super, type)) == NULL)
406 {
407 _cupsLangPrintf(stderr,
408 _("%s: Unknown source MIME type %s/%s."),
409 command, super, type);
410 return (1);
411 }
412 }
413 else if ((src = mimeFileType(mime, infile, infile, &compression)) == NULL)
414 {
415 _cupsLangPrintf(stderr,
416 _("%s: Unable to determine MIME type of \"%s\"."),
417 command, infile);
418 return (1);
419 }
420
421 /* sscanf return value already checked above */
422 sscanf(dsttype, "%15[^/]/%255s", super, type);
423 if (!_cups_strcasecmp(super, "printer"))
424 dst = printer_type;
425 else if ((dst = mimeType(mime, super, type)) == NULL)
426 {
427 _cupsLangPrintf(stderr,
428 _("%s: Unknown destination MIME type %s/%s."),
429 command, super, type);
430 return (1);
431 }
432
433 /*
434 * Figure out how to filter the file...
435 */
436
437 if (src == dst)
438 {
439 /*
440 * Special case - no filtering needed...
441 */
442
443 filters = cupsArrayNew(NULL, NULL);
444 cupsArrayAdd(filters, &GZIPFilter);
445 GZIPFilter.src = src;
446 GZIPFilter.dst = dst;
447 }
448 else if ((filters = mimeFilter(mime, src, dst, &cost)) == NULL)
449 {
450 _cupsLangPrintf(stderr,
451 _("%s: No filter to convert from %s/%s to %s/%s."),
452 command, src->super, src->type, dst->super, dst->type);
453 return (1);
454 }
455 else if (compression)
456 cupsArrayInsert(filters, &GZIPFilter);
457
458 if (prefilter_type)
459 {
460 /*
461 * Add pre-filters...
462 */
463
464 mime_filter_t *filter, /* Current filter */
465 *prefilter; /* Current pre-filter */
466 cups_array_t *prefilters = cupsArrayNew(NULL, NULL);
467 /* New filters array */
468
469
470 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
471 filter;
472 filter = (mime_filter_t *)cupsArrayNext(filters))
473 {
474 if ((prefilter = mimeFilterLookup(mime, filter->src,
475 prefilter_type)) != NULL)
476 cupsArrayAdd(prefilters, prefilter);
477
478 cupsArrayAdd(prefilters, filter);
479 }
480
481 cupsArrayDelete(filters);
482 filters = prefilters;
483 }
484
485 if (list_filters)
486 {
487 /*
488 * List filters...
489 */
490
491 mime_filter_t *filter; /* Current filter */
492
493 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
494 filter;
495 filter = (mime_filter_t *)cupsArrayNext(filters))
496 if (strcmp(filter->filter, "-"))
497 _cupsLangPuts(stdout, filter->filter);
498
499 status = 0;
500 }
501 else
502 {
503 /*
504 * Run filters...
505 */
506
507 status = exec_filters(src, filters, infile, outfile, ppdfile, printer, user,
508 title, num_options, options);
509 }
510
511 /*
512 * Remove files as needed, then exit...
513 */
514
515 if (TempFile[0])
516 unlink(TempFile);
517
518 if (removeppd && ppdfile)
519 unlink(ppdfile);
520
521 if (removeinfile && infile)
522 unlink(infile);
523
524 return (status);
525 }
526
527
528 /*
529 * 'add_printer_filter()' - Add a single filters from a PPD file.
530 */
531
532 static void
533 add_printer_filter(
534 const char *command, /* I - Command name */
535 mime_t *mime, /* I - MIME database */
536 mime_type_t *filtertype, /* I - Printer or prefilter MIME type */
537 const char *filter) /* I - Filter to add */
538 {
539 char super[MIME_MAX_SUPER], /* Super-type for filter */
540 type[MIME_MAX_TYPE], /* Type for filter */
541 dsuper[MIME_MAX_SUPER], /* Destination super-type for filter */
542 dtype[MIME_MAX_TYPE], /* Destination type for filter */
543 dest[MIME_MAX_SUPER + MIME_MAX_TYPE + 2],
544 /* Destination super/type */
545 program[1024]; /* Program/filter name */
546 int cost; /* Cost of filter */
547 size_t maxsize = 0; /* Maximum supported file size */
548 mime_type_t *temptype, /* MIME type looping var */
549 *desttype; /* Destination MIME type */
550 mime_filter_t *filterptr; /* MIME filter */
551
552
553 /*
554 * Parse the filter string; it should be in one of the following formats:
555 *
556 * source/type cost program
557 * source/type cost maxsize(nnnn) program
558 * source/type dest/type cost program
559 * source/type dest/type cost maxsize(nnnn) program
560 */
561
562 if (sscanf(filter, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
563 super, type, dsuper, dtype, &cost, program) == 6)
564 {
565 snprintf(dest, sizeof(dest), "%s/%s/%s", filtertype->type, dsuper, dtype);
566
567 if ((desttype = mimeType(mime, "printer", dest)) == NULL)
568 desttype = mimeAddType(mime, "printer", dest);
569 }
570 else
571 {
572 if (sscanf(filter, "%15[^/]/%255s%d%*[ \t]%1023[^\n]", super, type, &cost,
573 program) == 4)
574 {
575 desttype = filtertype;
576 }
577 else
578 {
579 _cupsLangPrintf(stderr, _("%s: Invalid filter string \"%s\"."), command,
580 filter);
581 return;
582 }
583 }
584
585 if (!strncmp(program, "maxsize(", 8))
586 {
587 char *ptr; /* Pointer into maxsize(nnnn) program */
588
589 maxsize = (size_t)strtoll(program + 8, &ptr, 10);
590
591 if (*ptr != ')')
592 {
593 printf("testmime: Invalid filter string \"%s\".\n", filter);
594 return;
595 }
596
597 ptr ++;
598 while (_cups_isspace(*ptr))
599 ptr ++;
600
601 _cups_strcpy(program, ptr);
602 }
603
604 /*
605 * See if the filter program exists; if not, stop the printer and flag
606 * the error!
607 */
608
609 if (strcmp(program, "-"))
610 {
611 char filename[1024]; /* Full path to program */
612
613 if (program[0] == '/')
614 strlcpy(filename, program, sizeof(filename));
615 else
616 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
617
618 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_PROGRAM, !geteuid(), check_cb,
619 (void *)command))
620 return;
621 }
622
623 /*
624 * Add the filter to the MIME database, supporting wildcards as needed...
625 */
626
627 for (temptype = mimeFirstType(mime);
628 temptype;
629 temptype = mimeNextType(mime))
630 if (((super[0] == '*' && _cups_strcasecmp(temptype->super, "printer")) ||
631 !_cups_strcasecmp(temptype->super, super)) &&
632 (type[0] == '*' || !_cups_strcasecmp(temptype->type, type)))
633 {
634 if (desttype != filtertype)
635 {
636 filterptr = mimeAddFilter(mime, temptype, desttype, cost, program);
637
638 if (!mimeFilterLookup(mime, desttype, filtertype))
639 mimeAddFilter(mime, desttype, filtertype, 0, "-");
640 }
641 else
642 filterptr = mimeAddFilter(mime, temptype, filtertype, cost, program);
643
644 if (filterptr)
645 filterptr->maxsize = maxsize;
646 }
647 }
648
649
650 /*
651 * 'add_printer_filters()' - Add filters from a PPD file.
652 */
653
654 static mime_type_t * /* O - Printer type or NULL on error */
655 add_printer_filters(
656 const char *command, /* I - Command name */
657 mime_t *mime, /* I - MIME database */
658 const char *printer, /* I - Printer name */
659 const char *ppdfile, /* I - PPD file */
660 mime_type_t **prefilter_type) /* O - Prefilter type */
661 {
662 ppd_file_t *ppd; /* PPD file data */
663 _ppd_cache_t *pc; /* Cache data for PPD */
664 const char *value; /* Filter definition value */
665 mime_type_t *printer_type; /* Printer filter type */
666
667
668 if ((ppd = _ppdOpenFile(ppdfile, _PPD_LOCALIZATION_NONE)) == NULL)
669 {
670 ppd_status_t status; /* PPD load status */
671 int linenum; /* Line number */
672
673 status = ppdLastError(&linenum);
674 _cupsLangPrintf(stderr, _("%s: Unable to open PPD file: %s on line %d."),
675 command, ppdErrorString(status), linenum);
676 return (NULL);
677 }
678
679 pc = _ppdCacheCreateWithPPD(ppd);
680 if (!pc)
681 return (NULL);
682
683 printer_type = mimeAddType(mime, "printer", printer);
684 *prefilter_type = NULL;
685
686 if (pc->filters)
687 {
688 for (value = (const char *)cupsArrayFirst(pc->filters);
689 value;
690 value = (const char *)cupsArrayNext(pc->filters))
691 add_printer_filter(command, mime, printer_type, value);
692 }
693 else
694 {
695 add_printer_filter(command, mime, printer_type,
696 "application/vnd.cups-raw 0 -");
697 add_printer_filter(command, mime, printer_type,
698 "application/vnd.cups-postscript 0 -");
699 }
700
701 if (pc->prefilters)
702 {
703 *prefilter_type = mimeAddType(mime, "prefilter", printer);
704
705 for (value = (const char *)cupsArrayFirst(pc->prefilters);
706 value;
707 value = (const char *)cupsArrayNext(pc->prefilters))
708 add_printer_filter(command, mime, *prefilter_type, value);
709 }
710
711 return (printer_type);
712 }
713
714
715 /*
716 * 'check_cb()' - Callback function for _cupsFileCheck.
717 */
718
719 static void
720 check_cb(void *context, /* I - Context (command name) */
721 _cups_fc_result_t result, /* I - Result of check */
722 const char *message) /* I - Localized message */
723 {
724 (void)result;
725
726 _cupsLangPrintf(stderr, _("%s: %s"), (char *)context, message);
727 }
728
729
730 /*
731 * 'compare_pids()' - Compare two filter PIDs...
732 */
733
734 static int /* O - Result of comparison */
735 compare_pids(mime_filter_t *a, /* I - First filter */
736 mime_filter_t *b) /* I - Second filter */
737 {
738 /*
739 * Because we're particularly lazy, we store the process ID in the "cost"
740 * variable...
741 */
742
743 return (a->cost - b->cost);
744 }
745
746
747 /*
748 * 'escape_options()' - Convert an options array to a string.
749 */
750
751 static char * /* O - Option string */
752 escape_options(
753 int num_options, /* I - Number of options */
754 cups_option_t *options) /* I - Options */
755 {
756 int i; /* Looping var */
757 cups_option_t *option; /* Current option */
758 size_t bytes; /* Number of bytes needed */
759 char *s, /* Option string */
760 *sptr, /* Pointer into string */
761 *vptr; /* Pointer into value */
762
763
764 /*
765 * Figure out the worst-case number of bytes we need for the option string.
766 */
767
768 for (i = num_options, option = options, bytes = 1; i > 0; i --, option ++)
769 bytes += 2 * (strlen(option->name) + strlen(option->value)) + 2;
770
771 if ((s = malloc(bytes)) == NULL)
772 return (NULL);
773
774 /*
775 * Copy the options to the string...
776 */
777
778 for (i = num_options, option = options, sptr = s; i > 0; i --, option ++)
779 {
780 if (!strcmp(option->name, "copies"))
781 continue;
782
783 if (sptr > s)
784 *sptr++ = ' ';
785
786 strlcpy(sptr, option->name, bytes - (size_t)(sptr - s));
787 sptr += strlen(sptr);
788 *sptr++ = '=';
789
790 for (vptr = option->value; *vptr;)
791 {
792 if (strchr("\\ \t\n", *vptr))
793 *sptr++ = '\\';
794
795 *sptr++ = *vptr++;
796 }
797 }
798
799 *sptr = '\0';
800
801 return (s);
802 }
803
804
805 /*
806 * 'exec_filter()' - Execute a single filter.
807 */
808
809 static int /* O - Process ID or -1 on error */
810 exec_filter(const char *filter, /* I - Filter to execute */
811 char **argv, /* I - Argument list */
812 char **envp, /* I - Environment list */
813 int infd, /* I - Stdin file descriptor */
814 int outfd) /* I - Stdout file descriptor */
815 {
816 int pid, /* Process ID */
817 fd; /* Temporary file descriptor */
818 #if defined(__APPLE__)
819 char processPath[1024], /* CFProcessPath environment variable */
820 linkpath[1024]; /* Link path for symlinks... */
821 int linkbytes; /* Bytes for link path */
822
823
824 /*
825 * Add special voodoo magic for MacOS X - this allows MacOS X
826 * programs to access their bundle resources properly...
827 */
828
829 if ((linkbytes = readlink(filter, linkpath, sizeof(linkpath) - 1)) > 0)
830 {
831 /*
832 * Yes, this is a symlink to the actual program, nul-terminate and
833 * use it...
834 */
835
836 linkpath[linkbytes] = '\0';
837
838 if (linkpath[0] == '/')
839 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
840 linkpath);
841 else
842 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
843 dirname((char *)filter), linkpath);
844 }
845 else
846 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", filter);
847
848 envp[0] = processPath; /* Replace <CFProcessPath> string */
849 #endif /* __APPLE__ */
850
851 if ((pid = fork()) == 0)
852 {
853 /*
854 * Child process goes here...
855 *
856 * Update stdin/stdout/stderr as needed...
857 */
858
859 if (infd != 0)
860 {
861 if (infd < 0)
862 infd = open("/dev/null", O_RDONLY);
863
864 if (infd > 0)
865 {
866 dup2(infd, 0);
867 close(infd);
868 }
869 }
870
871 if (outfd != 1)
872 {
873 if (outfd < 0)
874 outfd = open("/dev/null", O_WRONLY);
875
876 if (outfd > 1)
877 {
878 dup2(outfd, 1);
879 close(outfd);
880 }
881 }
882
883 if ((fd = open("/dev/null", O_RDWR)) > 3)
884 {
885 dup2(fd, 3);
886 close(fd);
887 }
888 fcntl(3, F_SETFL, O_NDELAY);
889
890 if ((fd = open("/dev/null", O_RDWR)) > 4)
891 {
892 dup2(fd, 4);
893 close(fd);
894 }
895 fcntl(4, F_SETFL, O_NDELAY);
896
897 /*
898 * Execute command...
899 */
900
901 execve(filter, argv, envp);
902
903 perror(filter);
904
905 exit(errno);
906 }
907
908 return (pid);
909 }
910
911
912 /*
913 * 'exec_filters()' - Execute filters for the given file and options.
914 */
915
916 static int /* O - 0 on success, 1 on error */
917 exec_filters(mime_type_t *srctype, /* I - Source type */
918 cups_array_t *filters, /* I - Array of filters to run */
919 const char *infile, /* I - File to filter */
920 const char *outfile, /* I - File to create */
921 const char *ppdfile, /* I - PPD file, if any */
922 const char *printer, /* I - Printer name */
923 const char *user, /* I - Username */
924 const char *title, /* I - Job title */
925 int num_options, /* I - Number of filter options */
926 cups_option_t *options) /* I - Filter options */
927 {
928 int i; /* Looping var */
929 const char *argv[8], /* Command-line arguments */
930 *envp[17], /* Environment variables */
931 *temp; /* Temporary string */
932 char *optstr, /* Filter options */
933 content_type[1024], /* CONTENT_TYPE */
934 cups_datadir[1024], /* CUPS_DATADIR */
935 cups_fontpath[1024], /* CUPS_FONTPATH */
936 cups_serverbin[1024], /* CUPS_SERVERBIN */
937 cups_serverroot[1024], /* CUPS_SERVERROOT */
938 final_content_type[1024] = "",
939 /* FINAL_CONTENT_TYPE */
940 lang[1024], /* LANG */
941 path[1024], /* PATH */
942 ppd[1024], /* PPD */
943 printer_info[255], /* PRINTER_INFO env variable */
944 printer_location[255], /* PRINTER_LOCATION env variable */
945 printer_name[255], /* PRINTER env variable */
946 rip_max_cache[1024], /* RIP_MAX_CACHE */
947 userenv[1024], /* USER */
948 program[1024]; /* Program to run */
949 mime_filter_t *filter, /* Current filter */
950 *next; /* Next filter */
951 int current, /* Current filter */
952 filterfds[2][2], /* Pipes for filters */
953 pid, /* Process ID of filter */
954 status, /* Exit status */
955 retval; /* Return value */
956 cups_array_t *pids; /* Executed filters array */
957 mime_filter_t key; /* Search key for filters */
958 cups_lang_t *language; /* Current language */
959 cups_dest_t *dest; /* Destination information */
960
961
962 /*
963 * Figure out the final content type...
964 */
965
966 for (filter = (mime_filter_t *)cupsArrayLast(filters);
967 filter && filter->dst;
968 filter = (mime_filter_t *)cupsArrayPrev(filters))
969 if (strcmp(filter->dst->super, "printer"))
970 break;
971
972 if (filter && filter->dst)
973 {
974 const char *ptr; /* Pointer in type name */
975
976 if ((ptr = strchr(filter->dst->type, '/')) != NULL)
977 snprintf(final_content_type, sizeof(final_content_type),
978 "FINAL_CONTENT_TYPE=%s", ptr + 1);
979 else
980 snprintf(final_content_type, sizeof(final_content_type),
981 "FINAL_CONTENT_TYPE=%s/%s", filter->dst->super,
982 filter->dst->type);
983 }
984
985 /*
986 * Remove NULL ("-") filters...
987 */
988
989 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
990 filter;
991 filter = (mime_filter_t *)cupsArrayNext(filters))
992 if (!strcmp(filter->filter, "-"))
993 cupsArrayRemove(filters, filter);
994
995 /*
996 * Setup the filter environment and command-line...
997 */
998
999 optstr = escape_options(num_options, options);
1000
1001 snprintf(content_type, sizeof(content_type), "CONTENT_TYPE=%s/%s",
1002 srctype->super, srctype->type);
1003 snprintf(cups_datadir, sizeof(cups_datadir), "CUPS_DATADIR=%s", DataDir);
1004 snprintf(cups_fontpath, sizeof(cups_fontpath), "CUPS_FONTPATH=%s", FontPath);
1005 snprintf(cups_serverbin, sizeof(cups_serverbin), "CUPS_SERVERBIN=%s",
1006 ServerBin);
1007 snprintf(cups_serverroot, sizeof(cups_serverroot), "CUPS_SERVERROOT=%s",
1008 ServerRoot);
1009 language = cupsLangDefault();
1010 snprintf(lang, sizeof(lang), "LANG=%s.UTF8", language->language);
1011 snprintf(path, sizeof(path), "PATH=%s", Path);
1012 if (ppdfile)
1013 snprintf(ppd, sizeof(ppd), "PPD=%s", ppdfile);
1014 else if ((temp = getenv("PPD")) != NULL)
1015 snprintf(ppd, sizeof(ppd), "PPD=%s", temp);
1016 else
1017 #ifdef __APPLE__
1018 if (!access("/System/Library/Frameworks/ApplicationServices.framework/"
1019 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
1020 "Resources/English.lproj/Generic.ppd", 0))
1021 strlcpy(ppd, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
1022 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
1023 "Resources/English.lproj/Generic.ppd", sizeof(ppd));
1024 else
1025 strlcpy(ppd, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
1026 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
1027 "Resources/Generic.ppd", sizeof(ppd));
1028 #else
1029 snprintf(ppd, sizeof(ppd), "PPD=%s/model/laserjet.ppd", DataDir);
1030 #endif /* __APPLE__ */
1031 snprintf(rip_max_cache, sizeof(rip_max_cache), "RIP_MAX_CACHE=%s", RIPCache);
1032 snprintf(userenv, sizeof(userenv), "USER=%s", user);
1033
1034 if (printer &&
1035 (dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, printer, NULL)) != NULL)
1036 {
1037 if ((temp = cupsGetOption("printer-info", dest->num_options,
1038 dest->options)) != NULL)
1039 snprintf(printer_info, sizeof(printer_info), "PRINTER_INFO=%s", temp);
1040 else
1041 snprintf(printer_info, sizeof(printer_info), "PRINTER_INFO=%s", printer);
1042
1043 if ((temp = cupsGetOption("printer-location", dest->num_options,
1044 dest->options)) != NULL)
1045 snprintf(printer_location, sizeof(printer_location),
1046 "PRINTER_LOCATION=%s", temp);
1047 else
1048 strlcpy(printer_location, "PRINTER_LOCATION=Unknown",
1049 sizeof(printer_location));
1050 }
1051 else
1052 {
1053 snprintf(printer_info, sizeof(printer_info), "PRINTER_INFO=%s",
1054 printer ? printer : "Unknown");
1055 strlcpy(printer_location, "PRINTER_LOCATION=Unknown",
1056 sizeof(printer_location));
1057 }
1058
1059 snprintf(printer_name, sizeof(printer_name), "PRINTER=%s",
1060 printer ? printer : "Unknown");
1061
1062 argv[0] = (char *)printer;
1063 argv[1] = "1";
1064 argv[2] = user;
1065 argv[3] = title;
1066 argv[4] = cupsGetOption("copies", num_options, options);
1067 argv[5] = optstr;
1068 argv[6] = infile;
1069 argv[7] = NULL;
1070
1071 if (!argv[4])
1072 argv[4] = "1";
1073
1074 envp[0] = "<CFProcessPath>";
1075 envp[1] = content_type;
1076 envp[2] = cups_datadir;
1077 envp[3] = cups_fontpath;
1078 envp[4] = cups_serverbin;
1079 envp[5] = cups_serverroot;
1080 envp[6] = lang;
1081 envp[7] = path;
1082 envp[8] = ppd;
1083 envp[9] = printer_info;
1084 envp[10] = printer_location;
1085 envp[11] = printer_name;
1086 envp[12] = rip_max_cache;
1087 envp[13] = userenv;
1088 envp[14] = "CHARSET=utf-8";
1089 if (final_content_type[0])
1090 {
1091 envp[15] = final_content_type;
1092 envp[16] = NULL;
1093 }
1094 else
1095 envp[15] = NULL;
1096
1097 for (i = 0; argv[i]; i ++)
1098 fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
1099
1100 for (i = 0; envp[i]; i ++)
1101 fprintf(stderr, "DEBUG: envp[%d]=\"%s\"\n", i, envp[i]);
1102
1103 /*
1104 * Execute all of the filters...
1105 */
1106
1107 pids = cupsArrayNew((cups_array_func_t)compare_pids, NULL);
1108 current = 0;
1109 filterfds[0][0] = -1;
1110 filterfds[0][1] = -1;
1111 filterfds[1][0] = -1;
1112 filterfds[1][1] = -1;
1113
1114 if (!infile)
1115 filterfds[0][0] = 0;
1116
1117 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
1118 filter;
1119 filter = next, current = 1 - current)
1120 {
1121 next = (mime_filter_t *)cupsArrayNext(filters);
1122
1123 if (filter->filter[0] == '/')
1124 strlcpy(program, filter->filter, sizeof(program));
1125 else
1126 snprintf(program, sizeof(program), "%s/filter/%s", ServerBin,
1127 filter->filter);
1128
1129 if (filterfds[!current][1] > 1)
1130 {
1131 close(filterfds[1 - current][0]);
1132 close(filterfds[1 - current][1]);
1133
1134 filterfds[1 - current][0] = -1;
1135 filterfds[1 - current][0] = -1;
1136 }
1137
1138 if (next)
1139 open_pipe(filterfds[1 - current]);
1140 else if (outfile)
1141 {
1142 filterfds[1 - current][1] = open(outfile, O_CREAT | O_TRUNC | O_WRONLY,
1143 0666);
1144
1145 if (filterfds[1 - current][1] < 0)
1146 fprintf(stderr, "ERROR: Unable to create \"%s\" - %s\n", outfile,
1147 strerror(errno));
1148 }
1149 else
1150 filterfds[1 - current][1] = 1;
1151
1152 pid = exec_filter(program, (char **)argv, (char **)envp,
1153 filterfds[current][0], filterfds[1 - current][1]);
1154
1155 if (pid > 0)
1156 {
1157 fprintf(stderr, "INFO: %s (PID %d) started.\n", filter->filter, pid);
1158
1159 filter->cost = pid;
1160 cupsArrayAdd(pids, filter);
1161 }
1162 else
1163 break;
1164
1165 argv[6] = NULL;
1166 }
1167
1168 /*
1169 * Close remaining pipes...
1170 */
1171
1172 if (filterfds[0][1] > 1)
1173 {
1174 close(filterfds[0][0]);
1175 close(filterfds[0][1]);
1176 }
1177
1178 if (filterfds[1][1] > 1)
1179 {
1180 close(filterfds[1][0]);
1181 close(filterfds[1][1]);
1182 }
1183
1184 /*
1185 * Wait for the children to exit...
1186 */
1187
1188 retval = 0;
1189
1190 while (cupsArrayCount(pids) > 0)
1191 {
1192 if ((pid = wait(&status)) < 0)
1193 continue;
1194
1195 key.cost = pid;
1196 if ((filter = (mime_filter_t *)cupsArrayFind(pids, &key)) != NULL)
1197 {
1198 cupsArrayRemove(pids, filter);
1199
1200 if (status)
1201 {
1202 if (WIFEXITED(status))
1203 fprintf(stderr, "ERROR: %s (PID %d) stopped with status %d\n",
1204 filter->filter, pid, WEXITSTATUS(status));
1205 else
1206 fprintf(stderr, "ERROR: %s (PID %d) crashed on signal %d\n",
1207 filter->filter, pid, WTERMSIG(status));
1208
1209 retval = 1;
1210 }
1211 else
1212 fprintf(stderr, "INFO: %s (PID %d) exited with no errors.\n",
1213 filter->filter, pid);
1214 }
1215 }
1216
1217 cupsArrayDelete(pids);
1218
1219 return (retval);
1220 }
1221
1222
1223 /*
1224 * 'get_job_file()' - Get the specified job file.
1225 */
1226
1227 static void
1228 get_job_file(const char *job) /* I - Job ID */
1229 {
1230 long jobid, /* Job ID */
1231 docnum; /* Document number */
1232 const char *jobptr; /* Pointer into job ID string */
1233 char uri[1024]; /* job-uri */
1234 http_t *http; /* Connection to server */
1235 ipp_t *request; /* Request data */
1236 int tempfd; /* Temporary file */
1237
1238
1239 /*
1240 * Get the job ID and document number, if any...
1241 */
1242
1243 if ((jobptr = strrchr(job, '-')) != NULL)
1244 jobptr ++;
1245 else
1246 jobptr = job;
1247
1248 jobid = strtol(jobptr, (char **)&jobptr, 10);
1249
1250 if (*jobptr == ',')
1251 docnum = strtol(jobptr + 1, NULL, 10);
1252 else
1253 docnum = 1;
1254
1255 if (jobid < 1 || jobid > INT_MAX)
1256 {
1257 _cupsLangPrintf(stderr, _("cupsfilter: Invalid job ID %d."), (int)jobid);
1258 exit(1);
1259 }
1260
1261 if (docnum < 1 || docnum > INT_MAX)
1262 {
1263 _cupsLangPrintf(stderr, _("cupsfilter: Invalid document number %d."),
1264 (int)docnum);
1265 exit(1);
1266 }
1267
1268 /*
1269 * Ask the server for the document file...
1270 */
1271
1272 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
1273 cupsEncryption())) == NULL)
1274 {
1275 _cupsLangPrintf(stderr, _("%s: Unable to connect to server."),
1276 "cupsfilter");
1277 exit(1);
1278 }
1279
1280 request = ippNewRequest(CUPS_GET_DOCUMENT);
1281
1282 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", (int)jobid);
1283
1284 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
1285 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "document-number",
1286 (int)docnum);
1287
1288 if ((tempfd = cupsTempFd(TempFile, sizeof(TempFile))) == -1)
1289 {
1290 _cupsLangPrintError("ERROR", _("Unable to create temporary file"));
1291 httpClose(http);
1292 exit(1);
1293 }
1294
1295 signal(SIGTERM, sighandler);
1296
1297 ippDelete(cupsDoIORequest(http, request, "/", -1, tempfd));
1298
1299 close(tempfd);
1300
1301 httpClose(http);
1302
1303 if (cupsLastError() != IPP_OK)
1304 {
1305 _cupsLangPrintf(stderr, _("cupsfilter: Unable to get job file - %s"),
1306 cupsLastErrorString());
1307 unlink(TempFile);
1308 exit(1);
1309 }
1310 }
1311
1312
1313 /*
1314 * 'open_pipe()' - Create a pipe which is closed on exec.
1315 */
1316
1317 static int /* O - 0 on success, -1 on error */
1318 open_pipe(int *fds) /* O - Pipe file descriptors (2) */
1319 {
1320 /*
1321 * Create the pipe...
1322 */
1323
1324 if (pipe(fds))
1325 {
1326 fds[0] = -1;
1327 fds[1] = -1;
1328
1329 return (-1);
1330 }
1331
1332 /*
1333 * Set the "close on exec" flag on each end of the pipe...
1334 */
1335
1336 if (fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC))
1337 {
1338 close(fds[0]);
1339 close(fds[1]);
1340
1341 fds[0] = -1;
1342 fds[1] = -1;
1343
1344 return (-1);
1345 }
1346
1347 if (fcntl(fds[1], F_SETFD, fcntl(fds[1], F_GETFD) | FD_CLOEXEC))
1348 {
1349 close(fds[0]);
1350 close(fds[1]);
1351
1352 fds[0] = -1;
1353 fds[1] = -1;
1354
1355 return (-1);
1356 }
1357
1358 /*
1359 * Return 0 indicating success...
1360 */
1361
1362 return (0);
1363 }
1364
1365
1366 /*
1367 * 'read_cups_files_conf()' - Read the cups-files.conf file to get the filter settings.
1368 */
1369
1370 static int /* O - 0 on success, 1 on error */
1371 read_cups_files_conf(
1372 const char *filename) /* I - File to read */
1373 {
1374 cups_file_t *fp; /* cups-files.conf file */
1375 const char *temp; /* Temporary string */
1376 char line[1024], /* Line from file */
1377 *ptr; /* Pointer into line */
1378 int linenum; /* Current line number */
1379
1380
1381 if ((temp = getenv("CUPS_DATADIR")) != NULL)
1382 set_string(&DataDir, temp);
1383 else
1384 set_string(&DataDir, CUPS_DATADIR);
1385
1386 if ((temp = getenv("CUPS_FONTPATH")) != NULL)
1387 set_string(&FontPath, temp);
1388 else
1389 set_string(&FontPath, CUPS_FONTPATH);
1390
1391 set_string(&RIPCache, "128m");
1392
1393 if ((temp = getenv("CUPS_SERVERBIN")) != NULL)
1394 set_string(&ServerBin, temp);
1395 else
1396 set_string(&ServerBin, CUPS_SERVERBIN);
1397
1398 strlcpy(line, filename, sizeof(line));
1399 if ((ptr = strrchr(line, '/')) != NULL)
1400 *ptr = '\0';
1401 else
1402 getcwd(line, sizeof(line));
1403
1404 set_string(&ServerRoot, line);
1405
1406 if ((fp = cupsFileOpen(filename, "r")) != NULL)
1407 {
1408 linenum = 0;
1409
1410 while (cupsFileGetConf(fp, line, sizeof(line), &ptr, &linenum))
1411 {
1412 if (!_cups_strcasecmp(line, "DataDir"))
1413 set_string(&DataDir, ptr);
1414 else if (!_cups_strcasecmp(line, "FontPath"))
1415 set_string(&FontPath, ptr);
1416 else if (!_cups_strcasecmp(line, "RIPCache"))
1417 set_string(&RIPCache, ptr);
1418 else if (!_cups_strcasecmp(line, "ServerBin"))
1419 set_string(&ServerBin, ptr);
1420 else if (!_cups_strcasecmp(line, "ServerRoot"))
1421 set_string(&ServerRoot, ptr);
1422 }
1423
1424 cupsFileClose(fp);
1425 }
1426
1427 snprintf(line, sizeof(line), "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR ":/bin:/usr/bin", ServerBin);
1428 set_string(&Path, line);
1429
1430 return (0);
1431 }
1432
1433
1434 /*
1435 * 'set_string()' - Copy and set a string.
1436 */
1437
1438 static void
1439 set_string(char **s, /* O - Copy of string */
1440 const char *val) /* I - String to copy */
1441 {
1442 if (*s)
1443 free(*s);
1444
1445 *s = strdup(val);
1446 }
1447
1448
1449 /*
1450 * 'sighandler()' - Signal catcher for when we print from stdin...
1451 */
1452
1453 static void
1454 sighandler(int s) /* I - Signal number */
1455 {
1456 /*
1457 * Remove the temporary file we're using to print a job file...
1458 */
1459
1460 if (TempFile[0])
1461 unlink(TempFile);
1462
1463 /*
1464 * Exit...
1465 */
1466
1467 exit(s);
1468 }
1469
1470
1471 /*
1472 * 'usage()' - Show program usage...
1473 */
1474
1475 static void
1476 usage(const char *opt) /* I - Incorrect option, if any */
1477 {
1478 if (opt)
1479 _cupsLangPrintf(stderr, _("%s: Unknown option \"%c\"."), "cupsfilter", *opt);
1480
1481 _cupsLangPuts(stdout, _("Usage: cupsfilter [ options ] [ -- ] filename"));
1482 _cupsLangPuts(stdout, _("Options:"));
1483 _cupsLangPuts(stdout, _(" --list-filters List filters that will be used."));
1484 _cupsLangPuts(stdout, _(" -D Remove the input file when finished."));
1485 _cupsLangPuts(stdout, _(" -P filename.ppd Set PPD file."));
1486 _cupsLangPuts(stdout, _(" -U username Specify username."));
1487 _cupsLangPuts(stdout, _(" -c cups-files.conf Set cups-files.conf file to use."));
1488 _cupsLangPuts(stdout, _(" -d printer Use the named printer."));
1489 _cupsLangPuts(stdout, _(" -e Use every filter from the PPD file."));
1490 _cupsLangPuts(stdout, _(" -i mime/type Set input MIME type (otherwise auto-typed)."));
1491 _cupsLangPuts(stdout, _(" -j job-id[,N] Filter file N from the specified job (default is file 1)."));
1492 _cupsLangPuts(stdout, _(" -m mime/type Set output MIME type (otherwise application/pdf)."));
1493 _cupsLangPuts(stdout, _(" -n copies Set number of copies."));
1494 _cupsLangPuts(stdout, _(" -o name=value Set option(s)."));
1495 _cupsLangPuts(stdout, _(" -p filename.ppd Set PPD file."));
1496 _cupsLangPuts(stdout, _(" -t title Set title."));
1497 _cupsLangPuts(stdout, _(" -u Remove the PPD file when finished."));
1498
1499 exit(1);
1500 }
1501
1502
1503 /*
1504 * End of "$Id$".
1505 */