/*
- * "$Id: cupsfilter.c 7952 2008-09-17 00:56:20Z mike $"
+ * "$Id$"
*
* Filtering program for CUPS.
*
- * Copyright 2007-2011 by Apple Inc.
+ * Copyright 2007-2013 by Apple Inc.
* Copyright 1997-2006 by Easy Software Products, all rights reserved.
*
* These coded instructions, statements, and computer programs are the
*
* Contents:
*
- * main() - Main entry for the test program.
- * compare_pids() - Compare two filter PIDs...
- * escape_options() - Convert an options array to a string.
- * exec_filter() - Execute a single filter.
- * exec_filters() - Execute filters for the given file and options.
- * get_job_file() - Get the specified job file.
- * open_pipe() - Create a pipe which is closed on exec.
- * read_cupsd_conf() - Read the cupsd.conf file to get the filter settings.
- * set_string() - Copy and set a string.
- * usage() - Show program usage...
+ * main() - Main entry for the test program.
+ * add_printer_filter() - Add a single filters from a PPD file.
+ * add_printer_filters() - Add filters from a PPD file.
+ * check_cb() - Callback function for _cupsFileCheck.
+ * compare_pids() - Compare two filter PIDs...
+ * escape_options() - Convert an options array to a string.
+ * exec_filter() - Execute a single filter.
+ * exec_filters() - Execute filters for the given file and options.
+ * get_job_file() - Get the specified job file.
+ * open_pipe() - Create a pipe which is closed on exec.
+ * read_cupsd_conf() - Read the cupsd.conf file to get the filter
+ * settings.
+ * set_string() - Copy and set a string.
+ * sighandler() - Signal catcher for when we print from stdin...
+ * usage() - Show program usage...
*/
/*
*/
#include <cups/cups-private.h>
+#include <cups/file-private.h>
+#include <cups/ppd-private.h>
#include "mime.h"
#include <limits.h>
#include <unistd.h>
#include <sys/wait.h>
#if defined(__APPLE__)
# include <libgen.h>
-#endif /* __APPLE__ */
+#endif /* __APPLE__ */
/*
mime_t *mime, const char *printer,
const char *ppdfile,
mime_type_t **prefilter_type);
+static void check_cb(void *context, _cups_fc_result_t result,
+ const char *message);
static int compare_pids(mime_filter_t *a, mime_filter_t *b);
static char *escape_options(int num_options, cups_option_t *options);
static int exec_filter(const char *filter, char **argv,
static int read_cupsd_conf(const char *filename);
static void set_string(char **s, const char *val);
static void sighandler(int sig);
-static void usage(const char *command, const char *opt);
+static void usage(const char *opt) __attribute__((noreturn));
/*
if (i < argc && !infile)
infile = argv[i];
else
- usage(command, opt);
+ usage(opt);
break;
case 'a' : /* Specify option... */
if (i < argc)
num_options = cupsParseOptions(argv[i], num_options, &options);
else
- usage(command, opt);
+ usage(opt);
break;
case 'c' : /* Specify cupsd.conf file location... */
strlcpy(cupsdconf, argv[i], sizeof(cupsdconf));
}
else
- usage(command, opt);
+ usage(opt);
break;
case 'd' : /* Specify the real printer name */
if (i < argc)
printer = argv[i];
else
- usage(command, opt);
+ usage(opt);
break;
case 'D' : /* Delete input file after conversion */
if (i < argc && !infile)
infile = argv[i];
else
- usage(command, opt);
+ usage(opt);
break;
case 'i' : /* Specify source MIME type... */
if (i < argc)
{
if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2)
- usage(command, opt);
+ usage(opt);
srctype = argv[i];
}
else
- usage(command, opt);
+ usage(opt);
break;
case 'j' : /* Get job file or specify destination MIME type... */
infile = TempFile;
}
else
- usage(command, opt);
+ usage(opt);
break;
}
if (i < argc)
{
if (sscanf(argv[i], "%15[^/]/%255s", super, type) != 2)
- usage(command, opt);
+ usage(opt);
dsttype = argv[i];
}
else
- usage(command, opt);
+ usage(opt);
break;
case 'n' : /* Specify number of copies... */
num_options = cupsAddOption("copies", argv[i], num_options,
&options);
else
- usage(command, opt);
+ usage(opt);
break;
case 'o' : /* Specify option(s) or output filename */
if (!strcmp(command, "convert"))
{
if (outfile)
- usage(command, NULL);
+ usage(NULL);
else
outfile = argv[i];
}
&options);
}
else
- usage(command, opt);
+ usage(opt);
break;
case 'p' : /* Specify PPD file... */
if (i < argc)
ppdfile = argv[i];
else
- usage(command, opt);
+ usage(opt);
break;
case 't' : /* Specify title... */
if (i < argc)
title = argv[i];
else
- usage(command, opt);
+ usage(opt);
break;
case 'u' : /* Delete PPD file after conversion */
- removeinfile = 1;
+ removeppd = 1;
break;
case 'U' : /* Specify username... */
if (i < argc)
user = argv[i];
else
- usage(command, opt);
+ usage(opt);
break;
default : /* Something we don't understand... */
- usage(command, opt);
+ usage(opt);
break;
}
}
if (strcmp(command, "convert"))
infile = argv[i];
else
- {
- _cupsLangPuts(stderr,
- _("convert: Use the -f option to specify a file to "
- "convert."));
- usage(command, NULL);
- }
+ usage(NULL);
}
else
{
_cupsLangPuts(stderr,
_("cupsfilter: Only one filename can be specified."));
- usage(command, NULL);
+ usage(NULL);
}
if (!infile && !srctype)
- usage(command, NULL);
+ usage(NULL);
if (!title)
{
return (1);
}
+ prefilter_type = NULL;
+
if (all_filters)
- {
printer_type = add_printer_filters(command, mime, printer, ppdfile,
&prefilter_type);
- }
else
- {
printer_type = mimeType(mime, "application", "vnd.cups-postscript");
- prefilter_type = NULL;
- }
/*
* Get the source and destination types...
if (srctype)
{
+ /* sscanf return value already checked above */
sscanf(srctype, "%15[^/]/%255s", super, type);
if ((src = mimeType(mime, super, type)) == NULL)
{
return (1);
}
+ /* sscanf return value already checked above */
sscanf(dsttype, "%15[^/]/%255s", super, type);
- if (!strcasecmp(super, "printer"))
+ if (!_cups_strcasecmp(super, "printer"))
dst = printer_type;
else if ((dst = mimeType(mime, super, type)) == NULL)
{
{
char super[MIME_MAX_SUPER], /* Super-type for filter */
type[MIME_MAX_TYPE], /* Type for filter */
+ dsuper[MIME_MAX_SUPER], /* Destination super-type for filter */
+ dtype[MIME_MAX_TYPE], /* Destination type for filter */
+ dest[MIME_MAX_SUPER + MIME_MAX_TYPE + 2],
+ /* Destination super/type */
program[1024]; /* Program/filter name */
int cost; /* Cost of filter */
- mime_type_t *temptype; /* MIME type looping var */
- char filename[1024]; /* Full filter filename */
+ size_t maxsize = 0; /* Maximum supported file size */
+ mime_type_t *temptype, /* MIME type looping var */
+ *desttype; /* Destination MIME type */
+ mime_filter_t *filterptr; /* MIME filter */
/*
- * Parse the filter string; it should be in the following format:
+ * Parse the filter string; it should be in one of the following formats:
*
- * super/type cost program
+ * source/type cost program
+ * source/type cost maxsize(nnnn) program
+ * source/type dest/type cost program
+ * source/type dest/type cost maxsize(nnnn) program
*/
- if (sscanf(filter, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super, type, &cost,
- program) != 4)
+ if (sscanf(filter, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
+ super, type, dsuper, dtype, &cost, program) == 6)
+ {
+ snprintf(dest, sizeof(dest), "%s/%s/%s", filtertype->type, dsuper, dtype);
+
+ if ((desttype = mimeType(mime, "printer", dest)) == NULL)
+ desttype = mimeAddType(mime, "printer", dest);
+ }
+ else
+ {
+ if (sscanf(filter, "%15[^/]/%255s%d%*[ \t]%1023[^\n]", super, type, &cost,
+ program) == 4)
+ {
+ desttype = filtertype;
+ }
+ else
+ {
+ _cupsLangPrintf(stderr, _("%s: Invalid filter string \"%s\"."), command,
+ filter);
+ return;
+ }
+ }
+
+ if (!strncmp(program, "maxsize(", 8))
{
- _cupsLangPrintf(stderr, _("%s: Invalid filter string \"%s\"."), command,
- filter);
- return;
+ char *ptr; /* Pointer into maxsize(nnnn) program */
+
+ maxsize = strtoll(program + 8, &ptr, 10);
+
+ if (*ptr != ')')
+ {
+ printf("testmime: Invalid filter string \"%s\".\n", filter);
+ return;
+ }
+
+ ptr ++;
+ while (_cups_isspace(*ptr))
+ ptr ++;
+
+ _cups_strcpy(program, ptr);
}
/*
if (strcmp(program, "-"))
{
+ char filename[1024]; /* Full path to program */
+
if (program[0] == '/')
strlcpy(filename, program, sizeof(filename));
else
snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
- if (access(filename, X_OK))
- {
- _cupsLangPrintf(stderr, _("%s: Filter \"%s\" not available: %s"),
- command, program, strerror(errno));
+ if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_PROGRAM, !geteuid(), check_cb,
+ (void *)command))
return;
- }
}
/*
for (temptype = mimeFirstType(mime);
temptype;
temptype = mimeNextType(mime))
- if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
- !strcasecmp(temptype->super, super)) &&
- (type[0] == '*' || !strcasecmp(temptype->type, type)))
- mimeAddFilter(mime, temptype, filtertype, cost, program);
+ if (((super[0] == '*' && _cups_strcasecmp(temptype->super, "printer")) ||
+ !_cups_strcasecmp(temptype->super, super)) &&
+ (type[0] == '*' || !_cups_strcasecmp(temptype->type, type)))
+ {
+ if (desttype != filtertype)
+ {
+ filterptr = mimeAddFilter(mime, temptype, desttype, cost, program);
+
+ if (!mimeFilterLookup(mime, desttype, filtertype))
+ mimeAddFilter(mime, desttype, filtertype, 0, "-");
+ }
+ else
+ filterptr = mimeAddFilter(mime, temptype, filtertype, cost, program);
+
+ if (filterptr)
+ filterptr->maxsize = maxsize;
+ }
}
const char *ppdfile, /* I - PPD file */
mime_type_t **prefilter_type) /* O - Prefilter type */
{
- int i; /* Looping var */
- mime_type_t *printer_type; /* Printer MIME type */
- ppd_file_t *ppd; /* PPD file data */
- ppd_attr_t *ppdattr; /* Current prefilter */
+ ppd_file_t *ppd; /* PPD file data */
+ _ppd_cache_t *pc; /* Cache data for PPD */
+ const char *value; /* Filter definition value */
+ mime_type_t *printer_type; /* Printer filter type */
- *prefilter_type = NULL;
-
- if ((ppd = ppdOpenFile(ppdfile)) == NULL)
+ if ((ppd = _ppdOpenFile(ppdfile, _PPD_LOCALIZATION_NONE)) == NULL)
{
- ppd_status_t status; /* PPD load status */
+ ppd_status_t status; /* PPD load status */
+ int linenum; /* Line number */
- status = ppdLastError(&i);
+ status = ppdLastError(&linenum);
_cupsLangPrintf(stderr, _("%s: Unable to open PPD file: %s on line %d."),
- command, ppdErrorString(status), i);
+ command, ppdErrorString(status), linenum);
return (NULL);
}
- printer_type = mimeAddType(mime, "printer", printer);
+ pc = _ppdCacheCreateWithPPD(ppd);
+ if (!pc)
+ return (NULL);
- if (ppd->num_filters > 0)
+ printer_type = mimeAddType(mime, "printer", printer);
+ *prefilter_type = NULL;
+
+ if (pc->filters)
{
- for (i = 0; i < ppd->num_filters; i ++)
- add_printer_filter(command, mime, printer_type, ppd->filters[i]);
+ for (value = (const char *)cupsArrayFirst(pc->filters);
+ value;
+ value = (const char *)cupsArrayNext(pc->filters))
+ add_printer_filter(command, mime, printer_type, value);
}
else
{
add_printer_filter(command, mime, printer_type,
- "application/vnd.cups-command 0 commandtops");
+ "application/vnd.cups-raw 0 -");
add_printer_filter(command, mime, printer_type,
"application/vnd.cups-postscript 0 -");
}
- if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
+ if (pc->prefilters)
{
*prefilter_type = mimeAddType(mime, "prefilter", printer);
- for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
- if (ppdattr->value)
- add_printer_filter(command, mime, *prefilter_type, ppdattr->value);
+ for (value = (const char *)cupsArrayFirst(pc->prefilters);
+ value;
+ value = (const char *)cupsArrayNext(pc->prefilters))
+ add_printer_filter(command, mime, *prefilter_type, value);
}
- else
- *prefilter_type = NULL;
return (printer_type);
}
+/*
+ * 'check_cb()' - Callback function for _cupsFileCheck.
+ */
+
+static void
+check_cb(void *context, /* I - Context (command name) */
+ _cups_fc_result_t result, /* I - Result of check */
+ const char *message) /* I - Localized message */
+{
+ (void)result;
+
+ _cupsLangPrintf(stderr, _("%s: %s"), (char *)context, message);
+}
+
+
/*
* 'compare_pids()' - Compare two filter PIDs...
*/
if (sptr > s)
*sptr++ = ' ';
- strcpy(sptr, option->name);
+ strlcpy(sptr, option->name, bytes - (sptr - s));
sptr += strlen(sptr);
*sptr++ = '=';
/*
- * Add special voodoo magic for MacOS X - this allows MacOS X
+ * Add special voodoo magic for MacOS X - this allows MacOS X
* programs to access their bundle resources properly...
*/
{
int i; /* Looping var */
const char *argv[8], /* Command-line arguments */
- *envp[15], /* Environment variables */
+ *envp[17], /* Environment variables */
*temp; /* Temporary string */
char *optstr, /* Filter options */
content_type[1024], /* CONTENT_TYPE */
cups_fontpath[1024], /* CUPS_FONTPATH */
cups_serverbin[1024], /* CUPS_SERVERBIN */
cups_serverroot[1024], /* CUPS_SERVERROOT */
+ final_content_type[1024] = "",
+ /* FINAL_CONTENT_TYPE */
lang[1024], /* LANG */
path[1024], /* PATH */
ppd[1024], /* PPD */
cups_dest_t *dest; /* Destination information */
+ /*
+ * Figure out the final content type...
+ */
+
+ for (filter = (mime_filter_t *)cupsArrayLast(filters);
+ filter && filter->dst;
+ filter = (mime_filter_t *)cupsArrayPrev(filters))
+ if (strcmp(filter->dst->super, "printer"))
+ break;
+
+ if (filter && filter->dst)
+ {
+ const char *ptr; /* Pointer in type name */
+
+ if ((ptr = strchr(filter->dst->type, '/')) != NULL)
+ snprintf(final_content_type, sizeof(final_content_type),
+ "FINAL_CONTENT_TYPE=%s", ptr + 1);
+ else
+ snprintf(final_content_type, sizeof(final_content_type),
+ "FINAL_CONTENT_TYPE=%s/%s", filter->dst->super,
+ filter->dst->type);
+ }
+
+ /*
+ * Remove NULL ("-") filters...
+ */
+
+ for (filter = (mime_filter_t *)cupsArrayFirst(filters);
+ filter;
+ filter = (mime_filter_t *)cupsArrayNext(filters))
+ if (!strcmp(filter->filter, "-"))
+ cupsArrayRemove(filters, filter);
+
/*
* Setup the filter environment and command-line...
*/
envp[11] = printer_name;
envp[12] = rip_max_cache;
envp[13] = userenv;
- envp[14] = NULL;
+ envp[14] = "CHARSET=utf-8";
+ if (final_content_type[0])
+ {
+ envp[15] = final_content_type;
+ envp[16] = NULL;
+ }
+ else
+ envp[15] = NULL;
for (i = 0; argv[i]; i ++)
fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
else
set_string(&FontPath, CUPS_FONTPATH);
- set_string(&RIPCache, "8m");
+ set_string(&RIPCache, "128m");
if ((temp = getenv("CUPS_SERVERBIN")) != NULL)
set_string(&ServerBin, temp);
while (cupsFileGetConf(fp, line, sizeof(line), &ptr, &linenum))
{
- if (!strcasecmp(line, "DataDir"))
+ if (!_cups_strcasecmp(line, "DataDir"))
set_string(&DataDir, ptr);
- else if (!strcasecmp(line, "FontPath"))
+ else if (!_cups_strcasecmp(line, "FontPath"))
set_string(&FontPath, ptr);
- else if (!strcasecmp(line, "RIPCache"))
+ else if (!_cups_strcasecmp(line, "RIPCache"))
set_string(&RIPCache, ptr);
- else if (!strcasecmp(line, "ServerBin"))
+ else if (!_cups_strcasecmp(line, "ServerBin"))
set_string(&ServerBin, ptr);
- else if (!strcasecmp(line, "ServerRoot"))
+ else if (!_cups_strcasecmp(line, "ServerRoot"))
set_string(&ServerRoot, ptr);
}
*/
static void
-usage(const char *command, /* I - Command name */
- const char *opt) /* I - Incorrect option, if any */
+usage(const char *opt) /* I - Incorrect option, if any */
{
if (opt)
- _cupsLangPrintf(stderr, _("%s: Unknown option \"%c\"."), command, *opt);
-
- if (!strcmp(command, "cupsfilter"))
- {
- _cupsLangPuts(stdout, _("Usage: cupsfilter [ options ] filename"));
- _cupsLangPuts(stdout, _("Options:"));
- _cupsLangPuts(stdout, _(" -D Remove the input file "
- "when finished."));
- _cupsLangPuts(stdout, _(" -P filename.ppd Set PPD file."));
- _cupsLangPuts(stdout, _(" -U username Set username for job."));
- _cupsLangPuts(stdout, _(" -c cupsd.conf Set cupsd.conf file to "
- "use."));
- _cupsLangPuts(stdout, _(" -d printer Use the named "
- "printer."));
- _cupsLangPuts(stdout, _(" -e Use every filter from "
- "the PPD file."));
- _cupsLangPuts(stdout, _(" -i mime/type Set input MIME type "
- "(otherwise auto-typed)."));
- _cupsLangPuts(stdout, _(" -j job-id[,N] Filter file N from the "
- "specified job (default is file 1)."));
- _cupsLangPuts(stdout, _(" -m mime/type Set output MIME type "
- "(otherwise application/pdf)."));
- _cupsLangPuts(stdout, _(" -n copies Set number of copies."));
- _cupsLangPuts(stdout, _(" -o name=value Set option(s)."));
- _cupsLangPuts(stdout, _(" -p filename.ppd Set PPD file."));
- _cupsLangPuts(stdout, _(" -t title Set title."));
- _cupsLangPuts(stdout, _(" -u Remove the PPD file "
- "when finished."));
- }
- else
- {
- _cupsLangPuts(stdout, _("Usage: convert [ options ]"));
- _cupsLangPuts(stdout, _("Options:"));
- _cupsLangPuts(stdout, _(" -D Remove the input file "
- "when finished."));
- _cupsLangPuts(stdout, _(" -J title Set title."));
- _cupsLangPuts(stdout, _(" -P filename.ppd Set PPD file."));
- _cupsLangPuts(stdout, _(" -U username Set username for job."));
- _cupsLangPuts(stdout, _(" -a 'name=value ...' Set option(s)."));
- _cupsLangPuts(stdout, _(" -c copies Set number of copies."));
- _cupsLangPuts(stdout, _(" -d printer Use the named "
- "printer."));
- _cupsLangPuts(stdout, _(" -e Use every filter from "
- "the PPD file."));
- _cupsLangPuts(stdout, _(" -f filename Set file to be "
- "converted (otherwise stdin)."));
- _cupsLangPuts(stdout, _(" -i mime/type Set input MIME type "
- "(otherwise auto-typed)."));
- _cupsLangPuts(stdout, _(" -j mime/type Set output MIME type "
- "(otherwise application/pdf)."));
- _cupsLangPuts(stdout, _(" -o filename Set file to be "
- "generated (otherwise stdout)."));
- _cupsLangPuts(stdout, _(" -u Remove the PPD file "
- "when finished."));
- }
+ _cupsLangPrintf(stderr, _("%s: Unknown option \"%c\"."), "cupsfilter",
+ *opt);
+
+ _cupsLangPuts(stdout, _("Usage: cupsfilter [ options ] filename"));
+ _cupsLangPuts(stdout, _("Options:"));
+ _cupsLangPuts(stdout, _(" -D Remove the input file "
+ "when finished."));
+ _cupsLangPuts(stdout, _(" -P filename.ppd Set PPD file."));
+ _cupsLangPuts(stdout, _(" -U username Specify username."));
+ _cupsLangPuts(stdout, _(" -c cupsd.conf Set cupsd.conf file to "
+ "use."));
+ _cupsLangPuts(stdout, _(" -d printer Use the named "
+ "printer."));
+ _cupsLangPuts(stdout, _(" -e Use every filter from "
+ "the PPD file."));
+ _cupsLangPuts(stdout, _(" -i mime/type Set input MIME type "
+ "(otherwise auto-typed)."));
+ _cupsLangPuts(stdout, _(" -j job-id[,N] Filter file N from the "
+ "specified job (default is file 1)."));
+ _cupsLangPuts(stdout, _(" -m mime/type Set output MIME type "
+ "(otherwise application/pdf)."));
+ _cupsLangPuts(stdout, _(" -n copies Set number of copies."));
+ _cupsLangPuts(stdout, _(" -o name=value Set option(s)."));
+ _cupsLangPuts(stdout, _(" -p filename.ppd Set PPD file."));
+ _cupsLangPuts(stdout, _(" -t title Set title."));
+ _cupsLangPuts(stdout, _(" -u Remove the PPD file "
+ "when finished."));
exit(1);
}
/*
- * End of "$Id: cupsfilter.c 7952 2008-09-17 00:56:20Z mike $".
+ * End of "$Id$".
*/