]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/cupsfilter.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / scheduler / cupsfilter.c
CommitLineData
bc44d920 1/*
b19ccc9e 2 * "$Id: cupsfilter.c 7952 2008-09-17 00:56:20Z mike $"
bc44d920 3 *
4 * CUPS filtering program for the Common UNIX Printing System (CUPS).
5 *
91c84a35 6 * Copyright 2007-2008 by Apple Inc.
bc44d920 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 * Contents:
16 *
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.
2e4ff8af 22 * get_job_file() - Get the specified job file.
bc44d920 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...
27 */
28
29/*
30 * Include necessary headers...
31 */
32
33#include <cups/cups.h>
34#include <cups/i18n.h>
35#include <cups/string.h>
36#include <errno.h>
37#include "mime.h"
38#include <stdlib.h>
2e4ff8af 39#include <limits.h>
bc44d920 40#include <unistd.h>
41#include <fcntl.h>
2e4ff8af 42#include <signal.h>
bc44d920 43#include <sys/wait.h>
44#if defined(__APPLE__)
45# include <libgen.h>
46#endif /* __APPLE__ */
47
48
49/*
50 * Local globals...
51 */
52
53static char *DataDir = NULL;/* CUPS_DATADIR environment variable */
54static char *FontPath = NULL;
55 /* CUPS_FONTPATH environment variable */
56static mime_filter_t GZIPFilter = /* gziptoany filter */
57{
58 NULL, /* Source type */
59 NULL, /* Destination type */
60 0, /* Cost */
61 "gziptoany" /* Filter program to run */
62};
63static char *Path = NULL; /* PATH environment variable */
64static char *ServerBin = NULL;
65 /* CUPS_SERVERBIN environment variable */
66static char *ServerRoot = NULL;
67 /* CUPS_SERVERROOT environment variable */
68static char *RIPCache = NULL;
69 /* RIP_CACHE environment variable */
2e4ff8af
MS
70static char TempFile[1024] = "";
71 /* Temporary file */
bc44d920 72
73
74/*
75 * Local functions...
76 */
77
b19ccc9e
MS
78static void add_printer_filter(const char *command, mime_t *mime,
79 mime_type_t *printer_type,
80 const char *filter);
81static mime_type_t *add_printer_filters(const char *command,
82 mime_t *mime, const char *printer,
83 const char *ppdfile,
84 mime_type_t **prefilter_type);
85static int compare_pids(mime_filter_t *a, mime_filter_t *b);
86static char *escape_options(int num_options, cups_option_t *options);
87static int exec_filter(const char *filter, char **argv,
88 char **envp, int infd, int outfd);
89static 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);
94static void get_job_file(const char *job);
95static int open_pipe(int *fds);
96static int read_cupsd_conf(const char *filename);
97static void set_string(char **s, const char *val);
98static void sighandler(int sig);
99static void usage(const char *command, const char *opt);
bc44d920 100
101
102/*
103 * 'main()' - Main entry for the test program.
104 */
105
106int /* O - Exit status */
107main(int argc, /* I - Number of command-line args */
108 char *argv[]) /* I - Command-line arguments */
109{
110 int i; /* Looping vars */
cc0d019f 111 const char *command, /* Command name */
b19ccc9e
MS
112 *opt, /* Current option */
113 *printer; /* Printer name */
114 mime_type_t *printer_type, /* Printer MIME type */
115 *prefilter_type; /* Printer prefilter MIME type */
cc0d019f
MS
116 char *srctype, /* Source type */
117 *dsttype, /* Destination type */
118 super[MIME_MAX_SUPER], /* Super-type name */
bc44d920 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 */
dd1abb6b 123 char mimedir[1024]; /* MIME directory */
cc0d019f
MS
124 char *infile, /* File to filter */
125 *outfile; /* File to create */
bc44d920 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 */
cc0d019f
MS
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 */
bc44d920 139
140
141 /*
142 * Setup defaults...
143 */
144
cc0d019f
MS
145 if ((command = strrchr(argv[0], '/')) != NULL)
146 command ++;
147 else
148 command = argv[0];
149
b19ccc9e 150 printer = !strcmp(command, "convert") ? "tofile" : "cupsfilter";
cc0d019f
MS
151 mime = NULL;
152 srctype = NULL;
91c84a35 153 compression = 0;
cc0d019f
MS
154 dsttype = "application/pdf";
155 infile = NULL;
156 outfile = NULL;
157 num_options = 0;
158 options = NULL;
159 ppdfile = NULL;
160 title = NULL;
161 user = cupsUser();
162 removeppd = 0;
163 removeinfile = 0;
bc44d920 164
165 if ((server_root = getenv("CUPS_SERVERROOT")) == NULL)
166 server_root = CUPS_SERVERROOT;
167
168 snprintf(cupsdconf, sizeof(cupsdconf), "%s/cupsd.conf", server_root);
169
170 /*
171 * Process command-line arguments...
172 */
173
174 _cupsSetLocale(argv);
175
176 for (i = 1; i < argc; i ++)
177 if (argv[i][0] == '-')
178 {
179 for (opt = argv[i] + 1; *opt; opt ++)
180 switch (*opt)
181 {
182 case '-' : /* Next argument is a filename... */
183 i ++;
cc0d019f
MS
184 if (i < argc && !infile)
185 infile = argv[i];
bc44d920 186 else
cc0d019f
MS
187 usage(command, opt);
188 break;
189
190 case 'a' : /* Specify option... */
191 i ++;
192 if (i < argc)
193 num_options = cupsParseOptions(argv[i], num_options, &options);
194 else
195 usage(command, opt);
bc44d920 196 break;
197
198 case 'c' : /* Specify cupsd.conf file location... */
199 i ++;
200 if (i < argc)
cc0d019f
MS
201 {
202 if (!strcmp(command, "convert"))
203 num_options = cupsAddOption("copies", argv[i], num_options,
204 &options);
205 else
206 strlcpy(cupsdconf, argv[i], sizeof(cupsdconf));
207 }
208 else
209 usage(command, opt);
210 break;
211
212 case 'D' : /* Delete input file after conversion */
213 removeinfile = 1;
214 break;
215
216 case 'f' : /* Specify input file... */
217 i ++;
218 if (i < argc && !infile)
219 infile = argv[i];
220 else
221 usage(command, opt);
222 break;
223
224 case 'i' : /* Specify source MIME type... */
225 i ++;
226 if (i < argc)
227 {
228 if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2)
229 usage(command, opt);
230
231 srctype = argv[i];
232 }
bc44d920 233 else
cc0d019f 234 usage(command, opt);
bc44d920 235 break;
236
2e4ff8af
MS
237 case 'j' : /* Get job file or specify destination MIME type... */
238 if (strcmp(command, "convert"))
239 {
240 i ++;
241 if (i < argc)
242 {
243 get_job_file(argv[i]);
244 infile = TempFile;
245 }
246 else
247 usage(command, opt);
248
249 break;
250 }
251
bc44d920 252 case 'm' : /* Specify destination MIME type... */
253 i ++;
254 if (i < argc)
255 {
256 if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2)
cc0d019f
MS
257 usage(command, opt);
258
259 dsttype = argv[i];
bc44d920 260 }
261 else
cc0d019f 262 usage(command, opt);
bc44d920 263 break;
264
265 case 'n' : /* Specify number of copies... */
266 i ++;
267 if (i < argc)
268 num_options = cupsAddOption("copies", argv[i], num_options,
269 &options);
270 else
cc0d019f 271 usage(command, opt);
bc44d920 272 break;
273
cc0d019f 274 case 'o' : /* Specify option(s) or output filename */
bc44d920 275 i ++;
276 if (i < argc)
cc0d019f
MS
277 {
278 if (!strcmp(command, "convert"))
279 {
280 if (outfile)
281 usage(command, NULL);
282 else
283 outfile = argv[i];
284 }
285 else
286 num_options = cupsParseOptions(argv[i], num_options,
287 &options);
288 }
bc44d920 289 else
cc0d019f 290 usage(command, opt);
bc44d920 291 break;
292
293 case 'p' : /* Specify PPD file... */
cc0d019f 294 case 'P' : /* Specify PPD file... */
bc44d920 295 i ++;
296 if (i < argc)
297 ppdfile = argv[i];
298 else
cc0d019f 299 usage(command, opt);
bc44d920 300 break;
301
cc0d019f
MS
302 case 't' : /* Specify title... */
303 case 'J' : /* Specify title... */
bc44d920 304 i ++;
305 if (i < argc)
306 title = argv[i];
307 else
cc0d019f
MS
308 usage(command, opt);
309 break;
310
311 case 'u' : /* Delete PPD file after conversion */
312 removeinfile = 1;
313 break;
314
315 case 'U' : /* Specify username... */
316 i ++;
317 if (i < argc)
318 user = argv[i];
319 else
320 usage(command, opt);
bc44d920 321 break;
322
323 default : /* Something we don't understand... */
cc0d019f 324 usage(command, opt);
bc44d920 325 break;
326 }
327 }
cc0d019f
MS
328 else if (!infile)
329 {
330 if (strcmp(command, "convert"))
331 infile = argv[i];
332 else
333 {
334 _cupsLangPuts(stderr,
335 _("convert: Use the -f option to specify a file to "
336 "convert.\n"));
337 usage(command, NULL);
338 }
339 }
bc44d920 340 else
341 {
342 _cupsLangPuts(stderr,
343 _("cupsfilter: Only one filename can be specified!\n"));
cc0d019f 344 usage(command, NULL);
bc44d920 345 }
346
cc0d019f
MS
347 if (!infile && !srctype)
348 usage(command, NULL);
bc44d920 349
350 if (!title)
351 {
cc0d019f
MS
352 if (!infile)
353 title = "(stdin)";
354 else if ((title = strrchr(infile, '/')) != NULL)
bc44d920 355 title ++;
356 else
cc0d019f 357 title = infile;
bc44d920 358 }
359
360 /*
361 * Load the cupsd.conf file and create the MIME database...
362 */
363
364 if (read_cupsd_conf(cupsdconf))
365 return (1);
366
75bd9771
MS
367 snprintf(mimedir, sizeof(mimedir), "%s/mime", DataDir);
368
369 mime = mimeLoadTypes(NULL, mimedir);
370 mime = mimeLoadTypes(mime, ServerRoot);
371 mime = mimeLoadFilters(mime, mimedir, Path);
372 mime = mimeLoadFilters(mime, ServerRoot, Path);
373
374 if (!mime)
bc44d920 375 {
376 _cupsLangPrintf(stderr,
75bd9771
MS
377 _("%s: Unable to read MIME database from \"%s\" or "
378 "\"%s\"!\n"),
379 command, mimedir, ServerRoot);
bc44d920 380 return (1);
381 }
382
b19ccc9e
MS
383 printer_type = add_printer_filters(command, mime, printer, ppdfile,
384 &prefilter_type);
385
bc44d920 386 /*
387 * Get the source and destination types...
388 */
389
cc0d019f
MS
390 if (srctype)
391 {
392 sscanf(srctype, "%15[^/]/%255s", super, type);
393 if ((src = mimeType(mime, super, type)) == NULL)
394 {
395 _cupsLangPrintf(stderr,
396 _("%s: Unknown source MIME type %s/%s!\n"),
397 command, super, type);
398 return (1);
399 }
400 }
401 else if ((src = mimeFileType(mime, infile, infile, &compression)) == NULL)
bc44d920 402 {
403 _cupsLangPrintf(stderr,
cc0d019f
MS
404 _("%s: Unable to determine MIME type of \"%s\"!\n"),
405 command, infile);
bc44d920 406 return (1);
407 }
408
cc0d019f 409 sscanf(dsttype, "%15[^/]/%255s", super, type);
b19ccc9e
MS
410 if (!strcasecmp(super, "printer"))
411 dst = printer_type;
412 else if ((dst = mimeType(mime, super, type)) == NULL)
bc44d920 413 {
414 _cupsLangPrintf(stderr,
cc0d019f
MS
415 _("%s: Unknown destination MIME type %s/%s!\n"),
416 command, super, type);
bc44d920 417 return (1);
418 }
419
420 /*
421 * Figure out how to filter the file...
422 */
423
424 if (src == dst)
425 {
426 /*
427 * Special case - no filtering needed...
428 */
429
430 filters = cupsArrayNew(NULL, NULL);
431 cupsArrayAdd(filters, &GZIPFilter);
432 }
433 else if ((filters = mimeFilter(mime, src, dst, &cost)) == NULL)
434 {
435 _cupsLangPrintf(stderr,
cc0d019f
MS
436 _("%s: No filter to convert from %s/%s to %s/%s!\n"),
437 command, src->super, src->type, dst->super, dst->type);
bc44d920 438 return (1);
439 }
440 else if (compression)
441 cupsArrayInsert(filters, &GZIPFilter);
442
b19ccc9e
MS
443 if (prefilter_type)
444 {
445 /*
446 * Add pre-filters...
447 */
448
449 mime_filter_t *filter, /* Current filter */
450 *prefilter; /* Current pre-filter */
451 cups_array_t *prefilters = cupsArrayNew(NULL, NULL);
452 /* New filters array */
453
454
455 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
456 filter;
457 filter = (mime_filter_t *)cupsArrayNext(filters))
458 {
459 if ((prefilter = mimeFilterLookup(mime, filter->src, prefilter_type)))
460 cupsArrayAdd(prefilters, prefilter);
461
462 cupsArrayAdd(prefilters, filter);
463 }
464
465 cupsArrayDelete(filters);
466 filters = prefilters;
467 }
468
bc44d920 469 /*
470 * Do it!
471 */
472
b19ccc9e
MS
473 status = exec_filters(filters, infile, outfile, ppdfile, printer, user,
474 title, num_options, options);
cc0d019f
MS
475
476 /*
477 * Remove files as needed, then exit...
478 */
479
2e4ff8af
MS
480 if (TempFile[0])
481 unlink(TempFile);
482
cc0d019f
MS
483 if (removeppd && ppdfile)
484 unlink(ppdfile);
485
486 if (removeinfile && infile)
487 unlink(infile);
488
489 return (status);
bc44d920 490}
491
492
b19ccc9e
MS
493/*
494 * 'add_printer_filter()' - Add a single filters from a PPD file.
495 */
496
497static void
498add_printer_filter(
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 */
503{
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 */
510
511
512 /*
513 * Parse the filter string; it should be in the following format:
514 *
515 * super/type cost program
516 */
517
518 if (sscanf(filter, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super, type, &cost,
519 program) != 4)
520 {
521 _cupsLangPrintf(stderr, _("%s: Invalid filter string \"%s\"\n"), command,
522 filter);
523 return;
524 }
525
526 /*
527 * See if the filter program exists; if not, stop the printer and flag
528 * the error!
529 */
530
531 if (strcmp(program, "-"))
532 {
533 if (program[0] == '/')
534 strlcpy(filename, program, sizeof(filename));
535 else
536 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
537
538 if (access(filename, X_OK))
539 {
540 _cupsLangPrintf(stderr, _("%s: Filter \"%s\" not available: %s\n"),
541 command, program, strerror(errno));
542 return;
543 }
544 }
545
546 /*
547 * Add the filter to the MIME database, supporting wildcards as needed...
548 */
549
550 for (temptype = mimeFirstType(mime);
551 temptype;
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);
557}
558
559
560/*
561 * 'add_printer_filters()' - Add filters from a PPD file.
562 */
563
564static mime_type_t * /* O - Printer type or NULL on error */
565add_printer_filters(
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 */
571{
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 */
576
577
578 if ((ppd = ppdOpenFile(ppdfile)) == NULL)
579 {
580 ppd_status_t status; /* PPD load status */
581
582 status = ppdLastError(&i);
583 _cupsLangPrintf(stderr, _("%s: Unable to open PPD file: %s on line %d\n"),
584 command, ppdErrorString(status), i);
585 return (NULL);
586 }
587
588 printer_type = mimeAddType(mime, "printer", printer);
589
590 if (ppd->num_filters > 0)
591 {
592 for (i = 0; i < ppd->num_filters; i ++)
593 add_printer_filter(command, mime, printer_type, ppd->filters[i]);
594 }
595 else
596 {
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 -");
601 }
602
603 if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
604 {
605 *prefilter_type = mimeAddType(mime, "prefilter", printer);
606
607 for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
608 if (ppdattr->value)
609 add_printer_filter(command, mime, *prefilter_type, ppdattr->value);
610 }
611 else
612 *prefilter_type = NULL;
613
614 return (printer_type);
615}
616
617
bc44d920 618/*
619 * 'compare_pids()' - Compare two filter PIDs...
620 */
621
622static int /* O - Result of comparison */
623compare_pids(mime_filter_t *a, /* I - First filter */
624 mime_filter_t *b) /* I - Second filter */
625{
626 /*
627 * Because we're particularly lazy, we store the process ID in the "cost"
628 * variable...
629 */
630
631 return (a->cost - b->cost);
632}
633
634
635/*
636 * 'escape_options()' - Convert an options array to a string.
637 */
638
639static char * /* O - Option string */
640escape_options(
641 int num_options, /* I - Number of options */
642 cups_option_t *options) /* I - Options */
643{
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 */
650
651
652 /*
653 * Figure out the worst-case number of bytes we need for the option string.
654 */
655
656 for (i = num_options, option = options, bytes = 1; i > 0; i --, option ++)
657 bytes += 2 * (strlen(option->name) + strlen(option->value)) + 2;
658
91c84a35
MS
659 if ((s = malloc(bytes)) == NULL)
660 return (NULL);
bc44d920 661
662 /*
663 * Copy the options to the string...
664 */
665
666 for (i = num_options, option = options, sptr = s; i > 0; i --, option ++)
667 {
668 if (!strcmp(option->name, "copies"))
669 continue;
670
671 if (sptr > s)
672 *sptr++ = ' ';
673
674 strcpy(sptr, option->name);
675 sptr += strlen(sptr);
676 *sptr++ = '=';
677
678 for (vptr = option->value; *vptr;)
679 {
680 if (strchr("\\ \t\n", *vptr))
681 *sptr++ = '\\';
682
683 *sptr++ = *vptr++;
684 }
685 }
686
687 *sptr = '\0';
688
bc44d920 689 return (s);
690}
691
692
693/*
694 * 'exec_filter()' - Execute a single filter.
695 */
696
697static int /* O - Process ID or -1 on error */
698exec_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 */
703{
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 */
709
710
711 /*
712 * Add special voodoo magic for MacOS X - this allows MacOS X
713 * programs to access their bundle resources properly...
714 */
715
716 if ((linkbytes = readlink(filter, linkpath, sizeof(linkpath) - 1)) > 0)
717 {
718 /*
719 * Yes, this is a symlink to the actual program, nul-terminate and
720 * use it...
721 */
722
723 linkpath[linkbytes] = '\0';
724
725 if (linkpath[0] == '/')
726 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s",
727 linkpath);
728 else
729 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s/%s",
730 dirname((char *)filter), linkpath);
731 }
732 else
733 snprintf(processPath, sizeof(processPath), "CFProcessPath=%s", filter);
734
735 envp[0] = processPath; /* Replace <CFProcessPath> string */
736#endif /* __APPLE__ */
737
738 if ((pid = fork()) == 0)
739 {
740 /*
741 * Child process goes here...
742 *
743 * Update stdin/stdout/stderr as needed...
744 */
745
746 if (infd != 0)
747 {
748 close(0);
749 if (infd > 0)
750 dup(infd);
751 else
752 open("/dev/null", O_RDONLY);
753 }
754
755 if (outfd != 1)
756 {
757 close(1);
758 if (outfd > 0)
759 dup(outfd);
760 else
761 open("/dev/null", O_WRONLY);
762 }
763
764 close(3);
765 open("/dev/null", O_RDWR);
766 fcntl(3, F_SETFL, O_NDELAY);
767
768 close(4);
769 open("/dev/null", O_RDWR);
770 fcntl(4, F_SETFL, O_NDELAY);
771
772 /*
773 * Execute command...
774 */
775
776 execve(filter, argv, envp);
777
778 perror(filter);
779
780 exit(errno);
781 }
782
783 return (pid);
784}
785
786
787/*
788 * 'exec_filters()' - Execute filters for the given file and options.
789 */
790
791static int /* O - 0 on success, 1 on error */
792exec_filters(cups_array_t *filters, /* I - Array of filters to run */
cc0d019f
MS
793 const char *infile, /* I - File to filter */
794 const char *outfile, /* I - File to create */
bc44d920 795 const char *ppdfile, /* I - PPD file, if any */
db1f069b 796 const char *printer, /* I - Printer name */
cc0d019f 797 const char *user, /* I - Username */
bc44d920 798 const char *title, /* I - Job title */
799 int num_options, /* I - Number of filter options */
800 cups_option_t *options) /* I - Filter options */
801{
cc0d019f 802 int i; /* Looping var */
bc44d920 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 */
813 ppd[1024], /* PPD */
814 rip_cache[1024], /* RIP_CACHE */
cc0d019f 815 userenv[1024], /* USER */
bc44d920 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 */
cc0d019f 826 cups_lang_t *language; /* Current language */
bc44d920 827
828
829 /*
830 * Setup the filter environment and command-line...
831 */
832
833 optstr = escape_options(num_options, options);
834
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",
838 ServerBin);
839 snprintf(cups_serverroot, sizeof(cups_serverroot), "CUPS_SERVERROOT=%s",
840 ServerRoot);
cc0d019f
MS
841 language = cupsLangDefault();
842 snprintf(lang, sizeof(lang), "LANG=%s.UTF8", language->language);
bc44d920 843 snprintf(path, sizeof(path), "PATH=%s", Path);
844 if (ppdfile)
845 snprintf(ppd, sizeof(ppd), "PPD=%s", ppdfile);
846 else if ((temp = getenv("PPD")) != NULL)
847 snprintf(ppd, sizeof(ppd), "PPD=%s", temp);
848 else
cc0d019f 849#ifdef __APPLE__
db1f069b
MS
850 if (!access("/System/Library/Frameworks/ApplicationServices.framework/"
851 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
852 "Resources/English.lproj/Generic.ppd", 0))
cc0d019f
MS
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));
db1f069b
MS
856 else
857 strlcpy(ppd, "PPD=/System/Library/Frameworks/ApplicationServices.framework/"
858 "Versions/A/Frameworks/PrintCore.framework/Versions/A/"
859 "Resources/Generic.ppd", sizeof(ppd));
cc0d019f 860#else
bc44d920 861 snprintf(ppd, sizeof(ppd), "PPD=%s/model/laserjet.ppd", DataDir);
cc0d019f 862#endif /* __APPLE__ */
bc44d920 863 snprintf(rip_cache, sizeof(rip_cache), "RIP_CACHE=%s", RIPCache);
cc0d019f 864 snprintf(userenv, sizeof(userenv), "USER=%s", user);
bc44d920 865
db1f069b
MS
866 argv[0] = (char *)printer;
867 argv[1] = "1";
cc0d019f 868 argv[2] = user;
bc44d920 869 argv[3] = title;
870 argv[4] = cupsGetOption("copies", num_options, options);
871 argv[5] = optstr;
cc0d019f 872 argv[6] = infile;
bc44d920 873 argv[7] = NULL;
874
875 if (!argv[4])
876 argv[4] = "1";
877
878 envp[0] = "<CFProcessPath>";
879 envp[1] = cups_datadir;
880 envp[2] = cups_fontpath;
881 envp[3] = cups_serverbin;
882 envp[4] = cups_serverroot;
883 envp[5] = lang;
884 envp[6] = path;
885 envp[7] = ppd;
886 envp[8] = rip_cache;
cc0d019f 887 envp[9] = userenv;
bc44d920 888 envp[10] = NULL;
889
cc0d019f
MS
890 for (i = 0; argv[i]; i ++)
891 fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
892
893 for (i = 0; envp[i]; i ++)
894 fprintf(stderr, "DEBUG: envp[%d]=\"%s\"\n", i, envp[i]);
895
bc44d920 896 /*
897 * Execute all of the filters...
898 */
899
900 pids = cupsArrayNew((cups_array_func_t)compare_pids, NULL);
901 current = 0;
902 filterfds[0][0] = -1;
903 filterfds[0][1] = -1;
904 filterfds[1][0] = -1;
905 filterfds[1][1] = -1;
906
cc0d019f
MS
907 if (!infile)
908 filterfds[0][0] = 0;
909
bc44d920 910 for (filter = (mime_filter_t *)cupsArrayFirst(filters);
911 filter;
912 filter = next, current = 1 - current)
913 {
914 next = (mime_filter_t *)cupsArrayNext(filters);
915
916 if (filter->filter[0] == '/')
917 strlcpy(program, filter->filter, sizeof(program));
918 else
919 snprintf(program, sizeof(program), "%s/filter/%s", ServerBin,
920 filter->filter);
921
922 if (filterfds[!current][1] > 1)
923 {
924 close(filterfds[1 - current][0]);
925 close(filterfds[1 - current][1]);
926
927 filterfds[1 - current][0] = -1;
928 filterfds[1 - current][0] = -1;
929 }
930
931 if (next)
932 open_pipe(filterfds[1 - current]);
cc0d019f
MS
933 else if (outfile)
934 {
935 filterfds[1 - current][1] = open(outfile, O_CREAT | O_TRUNC | O_WRONLY,
936 0666);
937
938 if (filterfds[1 - current][1] < 0)
939 fprintf(stderr, "ERROR: Unable to create \"%s\" - %s\n", outfile,
940 strerror(errno));
941 }
bc44d920 942 else
943 filterfds[1 - current][1] = 1;
944
945 pid = exec_filter(program, (char **)argv, (char **)envp,
946 filterfds[current][0], filterfds[1 - current][1]);
947
948 if (pid > 0)
949 {
950 fprintf(stderr, "INFO: %s (PID %d) started.\n", filter->filter, pid);
951
952 filter->cost = pid;
953 cupsArrayAdd(pids, filter);
954 }
955 else
956 break;
957
958 argv[6] = NULL;
959 }
960
961 /*
962 * Close remaining pipes...
963 */
964
965 if (filterfds[0][1] > 1)
966 {
967 close(filterfds[0][0]);
968 close(filterfds[0][1]);
969 }
970
971 if (filterfds[1][1] > 1)
972 {
973 close(filterfds[1][0]);
974 close(filterfds[1][1]);
975 }
976
977 /*
978 * Wait for the children to exit...
979 */
980
981 retval = 0;
982
983 while (cupsArrayCount(pids) > 0)
984 {
985 if ((pid = wait(&status)) < 0)
986 continue;
987
988 key.cost = pid;
989 if ((filter = (mime_filter_t *)cupsArrayFind(pids, &key)) != NULL)
990 {
991 cupsArrayRemove(pids, filter);
992
993 if (status)
994 {
995 if (WIFEXITED(status))
996 fprintf(stderr, "ERROR: %s (PID %d) stopped with status %d!\n",
997 filter->filter, pid, WEXITSTATUS(status));
998 else
999 fprintf(stderr, "ERROR: %s (PID %d) crashed on signal %d!\n",
1000 filter->filter, pid, WTERMSIG(status));
1001
1002 retval = 1;
1003 }
1004 else
1005 fprintf(stderr, "INFO: %s (PID %d) exited with no errors.\n",
1006 filter->filter, pid);
1007 }
1008 }
1009
91c84a35
MS
1010 cupsArrayDelete(pids);
1011
bc44d920 1012 return (retval);
1013}
1014
1015
2e4ff8af
MS
1016/*
1017 * 'get_job_file()' - Get the specified job file.
1018 */
1019
1020static void
1021get_job_file(const char *job) /* I - Job ID */
1022{
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 */
1030
1031
1032 /*
1033 * Get the job ID and document number, if any...
1034 */
1035
1036 if ((jobptr = strrchr(job, '-')) != NULL)
1037 jobptr ++;
1038 else
1039 jobptr = job;
1040
1041 jobid = strtol(jobptr, (char **)&jobptr, 10);
1042
1043 if (*jobptr == ',')
1044 docnum = strtol(jobptr + 1, NULL, 10);
1045 else
1046 docnum = 1;
1047
1048 if (jobid < 1 || jobid > INT_MAX)
1049 {
1050 _cupsLangPrintf(stderr, _("cupsfilter: Invalid job ID %d!\n"), (int)jobid);
1051 exit(1);
1052 }
1053
1054 if (docnum < 1 || docnum > INT_MAX)
1055 {
1056 _cupsLangPrintf(stderr, _("cupsfilter: Invalid document number %d!\n"),
1057 (int)docnum);
1058 exit(1);
1059 }
1060
1061 /*
1062 * Ask the server for the document file...
1063 */
1064
1065 if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
1066 cupsEncryption())) == NULL)
1067 {
1068 _cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"),
1069 "cupsfilter");
1070 exit(1);
1071 }
1072
1073 request = ippNewRequest(CUPS_GET_DOCUMENT);
1074
1075 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", (int)jobid);
1076
1077 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
1078 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "document-number",
1079 (int)docnum);
1080
1081 if ((tempfd = cupsTempFd(TempFile, sizeof(TempFile))) == -1)
1082 {
1083 _cupsLangPrintf(stderr,
1084 _("cupsfilter: Unable to create temporary file: %s\n"),
1085 strerror(errno));
1086 httpClose(http);
1087 exit(1);
1088 }
1089
1090 signal(SIGTERM, sighandler);
1091
1092 ippDelete(cupsDoIORequest(http, request, "/", -1, tempfd));
1093
1094 close(tempfd);
1095
1096 httpClose(http);
1097
1098 if (cupsLastError() != IPP_OK)
1099 {
1100 _cupsLangPrintf(stderr, _("cupsfilter: Unable to get job file - %s\n"),
1101 cupsLastErrorString());
1102 unlink(TempFile);
1103 exit(1);
1104 }
1105}
1106
1107
bc44d920 1108/*
1109 * 'open_pipe()' - Create a pipe which is closed on exec.
1110 */
1111
2e4ff8af 1112static int /* O - 0 on success, -1 on error */
bc44d920 1113open_pipe(int *fds) /* O - Pipe file descriptors (2) */
1114{
1115 /*
1116 * Create the pipe...
1117 */
1118
1119 if (pipe(fds))
1120 {
1121 fds[0] = -1;
1122 fds[1] = -1;
1123
1124 return (-1);
1125 }
1126
1127 /*
1128 * Set the "close on exec" flag on each end of the pipe...
1129 */
1130
1131 if (fcntl(fds[0], F_SETFD, fcntl(fds[0], F_GETFD) | FD_CLOEXEC))
1132 {
1133 close(fds[0]);
1134 close(fds[1]);
1135
1136 fds[0] = -1;
1137 fds[1] = -1;
1138
1139 return (-1);
1140 }
1141
1142 if (fcntl(fds[1], F_SETFD, fcntl(fds[1], F_GETFD) | FD_CLOEXEC))
1143 {
1144 close(fds[0]);
1145 close(fds[1]);
1146
1147 fds[0] = -1;
1148 fds[1] = -1;
1149
1150 return (-1);
1151 }
1152
1153 /*
1154 * Return 0 indicating success...
1155 */
1156
1157 return (0);
1158}
1159
1160
1161/*
1162 * 'read_cupsd_conf()' - Read the cupsd.conf file to get the filter settings.
1163 */
1164
1165static int /* O - 0 on success, 1 on error */
1166read_cupsd_conf(const char *filename) /* I - File to read */
1167{
cc0d019f 1168 cups_file_t *fp; /* cupsd.conf file */
bc44d920 1169 const char *temp; /* Temporary string */
1170 char line[1024], /* Line from file */
1171 *ptr; /* Pointer into line */
cc0d019f 1172 int linenum; /* Current line number */
bc44d920 1173
1174
1175 if ((temp = getenv("CUPS_DATADIR")) != NULL)
1176 set_string(&DataDir, temp);
1177 else
1178 set_string(&DataDir, CUPS_DATADIR);
1179
1180 if ((temp = getenv("CUPS_FONTPATH")) != NULL)
1181 set_string(&FontPath, temp);
1182 else
1183 set_string(&FontPath, CUPS_FONTPATH);
1184
cc0d019f
MS
1185 set_string(&RIPCache, "8m");
1186
bc44d920 1187 if ((temp = getenv("CUPS_SERVERBIN")) != NULL)
1188 set_string(&ServerBin, temp);
1189 else
1190 set_string(&ServerBin, CUPS_SERVERBIN);
1191
1192 strlcpy(line, filename, sizeof(line));
1193 if ((ptr = strrchr(line, '/')) != NULL)
1194 *ptr = '\0';
1195 else
1196 getcwd(line, sizeof(line));
1197
1198 set_string(&ServerRoot, line);
1199
cc0d019f
MS
1200 if ((fp = cupsFileOpen(filename, "r")) != NULL)
1201 {
1202 linenum = 0;
1203
1204 while (cupsFileGetConf(fp, line, sizeof(line), &ptr, &linenum))
1205 {
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);
1216 }
1217
1218 cupsFileClose(fp);
1219 }
1220
bc44d920 1221 snprintf(line, sizeof(line),
06d4e77b 1222 "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR ":/bin:/usr/bin",
bc44d920 1223 ServerBin);
1224 set_string(&Path, line);
1225
1226 return (0);
1227}
1228
1229
1230/*
1231 * 'set_string()' - Copy and set a string.
1232 */
1233
1234static void
1235set_string(char **s, /* O - Copy of string */
1236 const char *val) /* I - String to copy */
1237{
1238 if (*s)
1239 free(*s);
1240
1241 *s = strdup(val);
1242}
1243
1244
2e4ff8af
MS
1245/*
1246 * 'sighandler()' - Signal catcher for when we print from stdin...
1247 */
1248
1249static void
1250sighandler(int s) /* I - Signal number */
1251{
1252 /*
1253 * Remove the temporary file we're using to print a job file...
1254 */
1255
1256 if (TempFile[0])
1257 unlink(TempFile);
1258
1259 /*
1260 * Exit...
1261 */
1262
1263 exit(s);
1264}
1265
1266
bc44d920 1267/*
1268 * 'usage()' - Show program usage...
1269 */
1270
1271static void
cc0d019f
MS
1272usage(const char *command, /* I - Command name */
1273 const char *opt) /* I - Incorrect option, if any */
bc44d920 1274{
1275 if (opt)
cc0d019f
MS
1276 _cupsLangPrintf(stderr, _("%s: Unknown option '%c'!\n"), command, *opt);
1277
1278 if (!strcmp(command, "cupsfilter"))
1279 _cupsLangPuts(stdout,
1280 _("Usage: cupsfilter -m mime/type [ options ] filename\n"
1281 "\n"
1282 "Options:\n"
1283 "\n"
1284 " -c cupsd.conf Set cupsd.conf file to use\n"
2e4ff8af 1285 " -j job-id[,N] Filter file N from the specified job (default is file 1)\n"
cc0d019f
MS
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"));
1290 else
1291 _cupsLangPuts(stdout,
1292 _("Usage: convert [ options ]\n"
1293 "\n"
1294 "Options:\n"
1295 "\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"));
bc44d920 1307
1308 exit(1);
1309}
1310
1311
1312/*
b19ccc9e 1313 * End of "$Id: cupsfilter.c 7952 2008-09-17 00:56:20Z mike $".
bc44d920 1314 */