2 * TBCP port monitor for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1993-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include <cups/cups-private.h>
28 static char *psgets(char *buf, size_t *bytes, FILE *fp);
29 static ssize_t pswrite(const char *buf, size_t bytes);
33 * 'main()' - Main entry...
36 int /* O - Exit status */
37 main(int argc, /* I - Number of command-line args */
38 char *argv[]) /* I - Command-line arguments */
40 FILE *fp; /* File to print */
41 int copies; /* Number of copies left */
42 char line[1024]; /* Line/buffer from stream/file */
43 size_t linelen; /* Length of line */
44 ppd_file_t *ppd; /* PPD file */
48 * Check command-line...
51 if (argc < 6 || argc > 7)
53 _cupsLangPrintf(stderr,
54 _("Usage: %s job-id user title copies options [file]"),
66 copies = atoi(argv[4]);
67 fp = fopen(argv[6], "rb");
77 * Open the PPD file as needed...
80 ppd = ppdOpenFile(getenv("PPD"));
83 * Copy the print file to stdout...
90 if (ppd && ppd->jcl_begin)
91 fputs(ppd->jcl_begin, stdout);
92 if (ppd && ppd->jcl_ps)
93 fputs(ppd->jcl_ps, stdout);
95 if (!ppd || ppd->language_level == 1)
98 * Use setsoftwareiomode for BCP mode...
101 puts("%!PS-Adobe-3.0 ExitServer");
102 puts("%%Title: (BCP - Level 1)");
103 puts("%%EndComments");
104 puts("%%BeginExitServer: 0");
105 puts("serverdict begin 0 exitserver");
106 puts("%%EndExitServer");
107 puts("statusdict begin");
108 puts("/setsoftwareiomode known {100 setsoftwareiomode}");
115 * Use setdevparams for BCP mode...
118 puts("%!PS-Adobe-3.0");
119 puts("%%Title: (BCP - Level 2)");
120 puts("%%EndComments");
121 puts("currentsysparams");
122 puts("/CurInputDevice 2 copy known {");
124 puts("<</Protocol /Binary>> setdevparams");
131 if (ppd && ppd->jcl_end)
132 fputs(ppd->jcl_end, stdout);
133 else if (!ppd || ppd->num_filters == 0)
137 * Loop until we see end-of-file...
142 linelen = sizeof(line);
143 if (psgets(line, &linelen, fp) == NULL)
146 while (pswrite(line, linelen) > 0);
156 * 'psgets()' - Get a line from a file.
160 * This function differs from the gets() function in that it
161 * handles any combination of CR, LF, or CR LF to end input
165 static char * /* O - String or NULL if EOF */
166 psgets(char *buf, /* I - Buffer to read into */
167 size_t *bytes, /* IO - Length of buffer */
168 FILE *fp) /* I - File to read from */
170 char *bufptr; /* Pointer into buffer */
171 int ch; /* Character from file */
172 size_t len; /* Max length of string */
179 while ((size_t)(bufptr - buf) < len)
181 if ((ch = getc(fp)) == EOF)
187 * Got a CR; see if there is a LF as well...
192 if (ch != EOF && ch != '\n')
194 ungetc(ch, fp); /* Nope, save it for later... */
204 *bufptr++ = (char)ch;
208 * Add a trailing newline if it is there...
211 if (ch == '\n' || ch == '\r')
213 if ((size_t)(bufptr - buf) < len)
214 *bufptr++ = (char)ch;
220 * Nul-terminate the string and return it (or NULL for EOF).
224 *bytes = (size_t)(bufptr - buf);
226 if (ch == EOF && bufptr == buf)
234 * 'pswrite()' - Write data from a file.
237 static ssize_t /* O - Number of bytes written */
238 pswrite(const char *buf, /* I - Buffer to write */
239 size_t bytes) /* I - Bytes to write */
241 size_t count; /* Remaining bytes */
244 for (count = bytes; count > 0; count --, buf ++)
247 case 0x04 : /* CTRL-D */
251 * Don't quote the last CTRL-D...
258 case 0x01 : /* CTRL-A */
259 case 0x03 : /* CTRL-C */
260 case 0x05 : /* CTRL-E */
261 case 0x11 : /* CTRL-Q */
262 case 0x13 : /* CTRL-S */
263 case 0x14 : /* CTRL-T */
264 case 0x1c : /* CTRL-\ */
265 if (putchar(0x01) < 0)
267 if (putchar(*buf ^ 0x40) < 0)
272 if (putchar(*buf) < 0)
277 return ((ssize_t)bytes);