/*
- * "$Id: cupsfilter.c 6816 2007-08-20 20:16:00Z mike $"
+ * "$Id: cupsfilter.c 6879 2007-08-29 20:26:50Z mike $"
*
* CUPS filtering program for the Common UNIX Printing System (CUPS).
*
* 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.
#include <errno.h>
#include "mime.h"
#include <stdlib.h>
+#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
+#include <signal.h>
#include <sys/wait.h>
#if defined(__APPLE__)
# include <libgen.h>
/* CUPS_SERVERROOT environment variable */
static char *RIPCache = NULL;
/* RIP_CACHE environment variable */
+static char TempFile[1024] = "";
+ /* Temporary file */
/*
const char *printer, const char *user,
const char *title, int num_options,
cups_option_t *options);
+static void get_job_file(const char *job);
static int open_pipe(int *fds);
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);
usage(command, opt);
break;
- case 'j' : /* Specify destination MIME type... */
+ case 'j' : /* Get job file or specify destination MIME type... */
+ if (strcmp(command, "convert"))
+ {
+ i ++;
+ if (i < argc)
+ {
+ get_job_file(argv[i]);
+ infile = TempFile;
+ }
+ else
+ usage(command, opt);
+
+ break;
+ }
+
case 'm' : /* Specify destination MIME type... */
i ++;
if (i < argc)
* Remove files as needed, then exit...
*/
+ if (TempFile[0])
+ unlink(TempFile);
+
if (removeppd && ppdfile)
unlink(ppdfile);
}
+/*
+ * 'get_job_file()' - Get the specified job file.
+ */
+
+static void
+get_job_file(const char *job) /* I - Job ID */
+{
+ long jobid, /* Job ID */
+ docnum; /* Document number */
+ const char *jobptr; /* Pointer into job ID string */
+ char uri[1024]; /* job-uri */
+ http_t *http; /* Connection to server */
+ ipp_t *request; /* Request data */
+ int tempfd; /* Temporary file */
+
+
+ /*
+ * Get the job ID and document number, if any...
+ */
+
+ if ((jobptr = strrchr(job, '-')) != NULL)
+ jobptr ++;
+ else
+ jobptr = job;
+
+ jobid = strtol(jobptr, (char **)&jobptr, 10);
+
+ if (*jobptr == ',')
+ docnum = strtol(jobptr + 1, NULL, 10);
+ else
+ docnum = 1;
+
+ if (jobid < 1 || jobid > INT_MAX)
+ {
+ _cupsLangPrintf(stderr, _("cupsfilter: Invalid job ID %d!\n"), (int)jobid);
+ exit(1);
+ }
+
+ if (docnum < 1 || docnum > INT_MAX)
+ {
+ _cupsLangPrintf(stderr, _("cupsfilter: Invalid document number %d!\n"),
+ (int)docnum);
+ exit(1);
+ }
+
+ /*
+ * Ask the server for the document file...
+ */
+
+ if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
+ cupsEncryption())) == NULL)
+ {
+ _cupsLangPrintf(stderr, _("%s: Unable to connect to server\n"),
+ "cupsfilter");
+ exit(1);
+ }
+
+ request = ippNewRequest(CUPS_GET_DOCUMENT);
+
+ snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", (int)jobid);
+
+ ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
+ ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "document-number",
+ (int)docnum);
+
+ if ((tempfd = cupsTempFd(TempFile, sizeof(TempFile))) == -1)
+ {
+ _cupsLangPrintf(stderr,
+ _("cupsfilter: Unable to create temporary file: %s\n"),
+ strerror(errno));
+ httpClose(http);
+ exit(1);
+ }
+
+ signal(SIGTERM, sighandler);
+
+ ippDelete(cupsDoIORequest(http, request, "/", -1, tempfd));
+
+ close(tempfd);
+
+ httpClose(http);
+
+ if (cupsLastError() != IPP_OK)
+ {
+ _cupsLangPrintf(stderr, _("cupsfilter: Unable to get job file - %s\n"),
+ cupsLastErrorString());
+ unlink(TempFile);
+ exit(1);
+ }
+}
+
+
/*
* 'open_pipe()' - Create a pipe which is closed on exec.
*/
-int /* O - 0 on success, -1 on error */
+static int /* O - 0 on success, -1 on error */
open_pipe(int *fds) /* O - Pipe file descriptors (2) */
{
/*
}
+/*
+ * 'sighandler()' - Signal catcher for when we print from stdin...
+ */
+
+static void
+sighandler(int s) /* I - Signal number */
+{
+ /*
+ * Remove the temporary file we're using to print a job file...
+ */
+
+ if (TempFile[0])
+ unlink(TempFile);
+
+ /*
+ * Exit...
+ */
+
+ exit(s);
+}
+
+
/*
* 'usage()' - Show program usage...
*/
"Options:\n"
"\n"
" -c cupsd.conf Set cupsd.conf file to use\n"
+ " -j job-id[,N] Filter file N from the specified job (default is file 1)\n"
" -n copies Set number of copies\n"
" -o name=value Set option(s)\n"
" -p filename.ppd Set PPD file\n"
/*
- * End of "$Id: cupsfilter.c 6816 2007-08-20 20:16:00Z mike $".
+ * End of "$Id: cupsfilter.c 6879 2007-08-29 20:26:50Z mike $".
*/