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